Solve 2 mask object when colliding


I have 2 mask objects next to each other. When I move the player by clicking, it lags at the edge of those objects.
This is my code

if message_id == hash("contact_point_response") then
		if message.group == hash("bar") then
			go.set_position(go.get_position() + message.distance * message.normal)
		end	
	end	

Are you using a box shape for the player? Change to a sphere. That will make it much less likely that you get stuck like in the movie you shared.

1 Like

Not entirely on topic, but love the visual style!

@britzl I changed to sphere and it look funny. I want to player slips smoothly between the bars

Have you enabled physics debug in game.project? This will draw the physics shapes and maybe give you a clue. Please give it a try and share another gif.

@britzl I don’t know the editor has this feature. It is helpful.

Ok, so in the gaps between the shapes we can see that you are getting pushed back. What’s your game going to look like? Can you have one large shape spanning several pieces of ground? Or maybe ignore the horizontal component of the collision if the vertical is greater? It depends on your levels really.

@britzl My game is an endless runner with bars go up and down. When user touches the screen, the player will move forward horizontally. But the current bar that player is standing on has to be next to the next bar. I want the player slips smoothly from a bar to another. If the next bar is higher or lower than the current bar, user will lose. That why I test bars next to each other and move the player

Ok, and what if you set the horizontal component to 0 if less than the vertical.

What do you mean?

I mean something like this (untested):

if message_id == hash("contact_point_response") then
	if message.group == hash("bar") then
		local normal = message.normal
		if math.abs(normal.y) > math.abs(normal.x) then
			normal.x = 0
		end
		go.set_position(go.get_position() + message.distance * normal)
	end	
end

@britzl the player are not getting pushed back now. But it is still standing higher than bars. Can I use this code to fix it? By the way, could you explain why you have to compare normal.x and normal.y? I do not understand it

if message.distance > 0 then
      -- First, project the accumulated correction onto
      -- the penetration vector
      local proj = vmath.project(self.correction, message.normal * message.distance)
      if proj < 1 then
        -- Only care for projections that does not overshoot.
        local comp = (message.distance - message.distance * proj) * message.normal
        -- Apply compensation
        go.set_position(go.get_position() + comp)
        -- Accumulate correction done
        self.correction = self.correction + comp
      end
    end