Help With Body Contact Behavior

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

Help With Body Contact Behavior

Post 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!
Attachments
playercontroller.love
(3.93 KiB) Downloaded 112 times
gaeem defelpmint is maiy pashin
Itch: Freakman
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help With Body Contact Behavior

Post 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?
User avatar
pgimeno
Party member
Posts: 3548
Joined: Sun Oct 18, 2015 2:58 pm

Re: Help With Body Contact Behavior

Post 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.
User avatar
Baggef
Prole
Posts: 20
Joined: Fri Aug 23, 2019 7:30 pm
Location: USA
Contact:

Re: Help With Body Contact Behavior

Post 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.
gaeem defelpmint is maiy pashin
Itch: Freakman
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help With Body Contact Behavior

Post 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.
User avatar
raidho36
Party member
Posts: 2063
Joined: Mon Jun 17, 2013 12:00 pm

Re: Help With Body Contact Behavior

Post 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.
User avatar
ivan
Party member
Posts: 1911
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: Help With Body Contact Behavior

Post 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.
Post Reply

Who is online

Users browsing this forum: No registered users and 82 guests