Directional collision for platform jumping (SOLVED)

For our endless runner project I need my character to be able to jump on platforms. I would like to make it possible for the character to jump on the platform from any position, so I would like the collision to work only when the character is falling down onto platform. Is there a better approach than filtering out collision handing when the vertical velocity of the character is positive (exclude collision handling when the character is still going up)?

That seems to be the most straightforward way. There are a bunch of special cases to deal with but is there a reason why you’re not happy with that solution?

@sicher There’s nothing wrong with it. I simply saw directional colliders work out-of-the box in different engines and thought I may be missing something in Defold. Thanks!

Is there a tutorial or document anywhere that talks more in detail about this? I’m currently working on something with the same, but I’m rather new to dealing with collisions in defold. I’ve searched the forums and only a couple posts popped up including this one.

Right now I have my character popping up through the platforms if they’re still in contact when the velocity changes… I imagine I may not want to do this velocity check on the on_message call, or I’m sure I’m missing more logic inside the response handler.

function on_message(self, message_id, message, sender)
	-- check if we received a contact point message
	if message_id == msg_contact_point_response then
		-- check that the object is something we consider geometry
		if message.group == group_geometry then
			handle_geometry_contact(self, message.normal, message.distance)
		end
		if message.group == group_platform and self.velocity.y < 0 then
			handle_platform_contact(self, message.normal, message.distance)
		end
	end
end

My issue points that this guy warns about in his blog post (this post is linked in the platformer tutorial)

One-way platforms are platforms that you can step on, but you can also jump through them. In other words, they count as an obstacle if you’re already on top of them, but are otherwise traversable. That sentence is the key to understanding their behavior. The algorithm changes as follows:

  • On the x axis, the tile is never an obstacle
  • On the y axis, the tile is only an obstacle if, prior to the movement, the player was entirely above it (that is, bottom-most coordinate of player was at least one pixel above top-most coordinate of one-way platform). To check for this, you will probably want to store the original player position before doing any stepping.
    It might be tempting to have it act as an obstacle if the player’s y speed is positive (that is, if the player is falling), but this behavior is wrong: it’s possible for the player to jump so he overlaps the platform, but then falls down again without having his feet reach the platform. In that case, he should still fall through.

I don’t really know where to come up with the code logic to accommodate those two bullet points, being so new to Defold and manual collision detection logic. Maybe it’s a lack of understanding of normals and vectors, but I don’t get how to tell when they are contacting on the x axis vs y axis. And how to know if the bottom most pixel has cleared the top most pixel of the platform etc.

Any tips?

My suggestion is to keep track of how the first contact with a platform happened. If the contact came from the sides or below you could set a flag and as long as that flag is set you don’t resolve collisions with platforms. If the flag isn’t set and the contact comes from above you resolve the collision in the same way as ground contact.

I’ve created an example of this, based on the platformer code from the getting started tutorial:

CODE: https://github.com/britzl/publicexamples/tree/master/examples/one_way_platforms
HTML5: http://britzl.github.io/publicexamples/one_way_platforms/index.html

2 Likes

Thanks for the example @britzl! This will help a lot.

Hi! i have the same bug when testing @britzl one_way_ platforms example.
Is somebody solved the issue ?

Which bug specifically?

One-way-platforms-2020-11-03-11

the player randomly falls in the middle of the platform.
I think it’s related to the value of the normal vector

Ah, I’ve updated the example, please try the new version.

The problem was that the physics shape was a box, and the box collision was sometimes detected as a horizontal collision which caused the player to fall through as if passing through a one way platform. The player shape now consists of two spheres.

You can enable physics debug to see what’s going on: https://defold.com/manuals/debugging-game-logic/#debugging-problems-with-physics

3 Likes

Thx you @britzl.
It works!

1 Like