Page 1 of 1

EndContact has wrong normal?...[SOLVED]

Posted: Tue Sep 06, 2016 1:47 am
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!

Re: EndContact has wrong normal?...

Posted: Tue Sep 06, 2016 7:32 am
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.

Re: EndContact has wrong normal?...

Posted: Tue Sep 06, 2016 11:04 am
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!

Re: EndContact has wrong normal?...

Posted: Tue Sep 06, 2016 11:18 am
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...

Re: EndContact has wrong normal?...

Posted: Wed Sep 07, 2016 4:44 am
by sunsflower
thanks, and the link is very useful indeed!

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

Posted: Tue Sep 13, 2016 12:57 pm
by sunsflower
no..I think we still need world callbacks much because using getContactList will miss sensor fixtures... :crazy:

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

Posted: Tue Sep 13, 2016 5:29 pm
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

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

Posted: Wed Sep 14, 2016 8:23 am
by sunsflower
indeed. thanks much for your help!