EndContact has wrong normal?...[SOLVED]

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

EndContact has wrong normal?...[SOLVED]

Post by sunsflower »

I was trying to use contact:getNormal() and it returned some weird results such as x=4.5268153724432e-021, y=0 which is always the result whatever the endcontact callback is called from..I tried to jump, to touch a wall and leave it and the results were quite the same..
here is end contact function:

Code: Select all

local function endContactFunc(fixa, fixb, contact)
    local nx, ny = contact:getNormal()
    g.db:log(nx, ny)

    local ud1 = fixa:getBody():getUserData()
    local ud2 = fixb:getBody():getUserData()
    if ud1 then
        if fixb:isSensor() then
            ud1:onuntriggered(fixb:getBody(), contact, ud2)
        else
            ud1:onuncollided(fixb:getBody(), contact, ud2)
        end
    end
    if ud2 then
        if fixa:isSensor() then
            ud2:onuntriggered(fixa:getBody(), contact, ud1)
        else
            ud2:onuncollided(fixa:getBody(), contact, ud1)
        end
    end
end
here is how I set it

Code: Select all

    
g.physworld:setCallbacks(beginContactFunc, endContactFunc)
does anyone know what's the problem?
thanks!
Last edited by sunsflower on Wed Sep 07, 2016 4:44 am, edited 1 time in total.
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: EndContact has wrong normal?...

Post by ivan »

Hello and welcome to the forums.

Assuming that your log function works as expected,
I would say that the "EndContact" callback is not a good place to get the collision normal.
"EndContact" occurs when the two fixtures are separated so
it's not a good callback to use if you need the collision normal
(perhaps that's why you are getting a near-zero result).
You probably want to switch to "PostSolve".
"PostSolve" is called after Box2D has resolved the collision
and provides you with info, including the normal.
"PreSolve" is used if you want to "disable" a particular collision.
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: EndContact has wrong normal?...

Post by sunsflower »

oh, I'm not aware of that before...I'm trying to decrease the jump counter of player when it leaves some platform.
thanks a lot!
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: EndContact has wrong normal?...

Post by ivan »

Collision normals are applied for every collision in sub-steps.
So it's not very efficient or useful to check each normal
and every collision in the simulation.

What you want to do is: once per frame, iterate all player contacts
and check "if the body is pushed along an axis":
http://2dengine.com/doc/gs_physics6.html
If the body is being pushed UP (or along the axis of gravity) then
the player must be standing on top of something.
This should work with moving platforms too.

Then you store that result until the next frame...
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: EndContact has wrong normal?...

Post by sunsflower »

thanks, and the link is very useful indeed!
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: EndContact has wrong normal?...[SOLVED]

Post by sunsflower »

no..I think we still need world callbacks much because using getContactList will miss sensor fixtures... :crazy:
User avatar
ivan
Party member
Posts: 1915
Joined: Fri Mar 07, 2008 1:39 pm
Contact:

Re: EndContact has wrong normal?...[SOLVED]

Post by ivan »

Sensor fixtures behave like "ghosts" and can pass through other fixtures.
Sensor fixtures don't have a collision normal - even if they do, it will never be used because collisions with sensors are never resolved.
When iterating the contact list, you may also need to look at:
https://love2d.org/wiki/Contact:isTouching
sunsflower
Prole
Posts: 18
Joined: Mon Jan 04, 2016 10:36 am

Re: EndContact has wrong normal?...[SOLVED]

Post by sunsflower »

indeed. thanks much for your help!
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 4 guests