Page 1 of 1

Help With Body Contact Behavior

Posted: Mon Nov 04, 2019 3:45 am
by Baggef
I don't really understand how this is working, but I noticed that I can jump before I hit the ground. How I'm detecting the ground is a sensor fixture that tests for the user data in the floor. If you press "p", you can see the hitboxes. The small box (the sensor) below the player (the purple square) isn't coming into contact with the ground before the player jumps again. I would think that the frame the sensor is coming into contact with the floor isn't being rendered, but I don't really know that much about how this works so if anyone can tell me whats happening or how to fix it it would be greatly appreciated!

Re: Help With Body Contact Behavior

Posted: Mon Nov 04, 2019 7:09 am
by ivan
Your isGrounded check is incorrect. First off, you need to check for contact:isTouching() and it helps to check the collision normal too. The collision normal will show you if the player is actually being pushed "up":
https://2dengine.com/?p=box2d#Is_the_bo ... ng_an_axis?

Re: Help With Body Contact Behavior

Posted: Mon Nov 04, 2019 9:31 am
by pgimeno
I think what is happening, apart from what ivan is saying, is that without vsync, it's very unlikely that the frame where the player is touching the ground is shown at all, because it only happens during a very short instant.

I tried enabling vsync and it made the player be shown touching the ground.

Edit: An alternative if you absolutely want to keep vsync off, is to disable jumping when the player touches the ground, and re-enable it with a timer that lasts for the duration of a typical frame, say 1/60 or so.

Re: Help With Body Contact Behavior

Posted: Tue Nov 05, 2019 4:54 pm
by Baggef
Thanks for the help. I ended up going with bump.lua mainly because the wiki kept telling me to, and I'm not exactly making a physics simulator. I found it to be a lot more intuitive and easy to understand. What I ended up going with was the collision normal, which in bump.lua looks like this:

Code: Select all

-- len being the length of cols (the collisions) 
for i=1,len do
	local other = cols[i].other
	pc.grounded = (other.isGround and cols[i].normal.x == 0 and cols[i].normal.y == -1)
end
	
if len == 0 then
	pc.grounded = false
end
This also made the "air hopping" less apparent and it's at a point that I can live with it.

Re: Help With Body Contact Behavior

Posted: Tue Nov 05, 2019 6:59 pm
by ivan
Box2D is a very powerful library, although its API can be a little overwhelming.
Also, note that you should be especially careful with checks like x == 0 or y == -1.
With floating point numbers (like collision normals) this check won't work as expected.
Plus, the player could be standing on a slope in which case "y" won't be -1.

Re: Help With Body Contact Behavior

Posted: Tue Nov 05, 2019 9:45 pm
by raidho36
ivan wrote: Tue Nov 05, 2019 6:59 pm Also, note that you should be especially careful with checks like x == 0 or y == -1.
With floating point numbers (like collision normals) this check won't work as expected.
More specifically, floating point numbers have limited number of decimal places to work with, so pretty much every math operation is guaranteed to produce tiny errors in the last digit, so a floating point number is never exactly equal to any specific value unless you set it to that specific value. One notable exception is that double-precision floating point numbers (the ones used in Lua) can store integers exactly, and integer-producing operations will never result in slightly-off-integer value. Double floats are 64 bits wide but you only have 53 bits of integer space to work with.

Re: Help With Body Contact Behavior

Posted: Tue Nov 05, 2019 10:05 pm
by pgimeno

Re: Help With Body Contact Behavior

Posted: Wed Nov 06, 2019 3:17 am
by ivan
pgimeno wrote: Tue Nov 05, 2019 10:05 pm I don't think bump will return anything other than 0, 1 or -1.
Good catch there. Still I don't recommend == for non integers.