i need to know exactly when two objects are not colliding anymore.
So I used the endContact callback.
But this callback seems to trigger one step too late.
Here is the code of the PhysicsCollisionCallbacks tutorial.
I modified it to see the steps.
You can go step by step by hitting the "space" key.
At step 3, the ball is ordered to go up and we update the world.
We can see that the ball is not touching but the endContact is not triggered.
At step 4, the endContact is triggered but too late, the ball is in the air since two steps.
Is this an issue or am I missing something ?
In that case, is there a way to know if the ball is not colliding just after the applyForce and the word:update ?
Thank you for your help !!
Code: Select all
function love.load()
world = love.physics.newWorld(0, 200, true)
world:setCallbacks(beginContact, endContact, preSolve, postSolve)
ball = {}
ball.b = love.physics.newBody(world, 400,325, "dynamic")
ball.b:setMass(10)
ball.s = love.physics.newCircleShape(50)
ball.f = love.physics.newFixture(ball.b, ball.s)
ball.f:setRestitution(0.4)
ball.f:setUserData("Ball")
static = {}
static.b = love.physics.newBody(world, 400,400, "static")
static.s = love.physics.newRectangleShape(200,50)
static.f = love.physics.newFixture(static.b, static.s)
static.f:setUserData("Block")
text = "" -- we'll use this to put info text on the screen later
keySpaceReleased = true -- Space key released ?
step = 0 --Step of the simulation
end
function love.update(dt)
if love.keyboard.isDown("escape") then
--Exit
love.event.quit()
end
if love.keyboard.isDown("space") and keySpaceReleased == true then
step = step + 1
text = text .. "\n\nStep: "..step
keySpaceReleased=false
if step == 3 then
--Step 3 : Ball go up
text = text .. "\nOrder to go up"
ball.b:applyForce(0, -500000)
end
text = text .. "\nUpdate world"
world:update(dt)
elseif love.keyboard.isDown("space") == false and keySpaceReleased == false then
keySpaceReleased=true -- Space key is released
end
if string.len(text) > 768 then -- cleanup when 'text' gets too long
text = ""
end
end
function love.draw()
love.graphics.circle("line", ball.b:getX(),ball.b:getY(), ball.s:getRadius(), 20)
love.graphics.polygon("line", static.b:getWorldPoints(static.s:getPoints()))
love.graphics.print(text, 10, 10)
end
function beginContact(a, b, coll)
x,y = coll:getNormal()
text = text.."\n"..a:getUserData().." colliding with "..b:getUserData().." with a vector normal of: "..x..", "..y
end
function endContact(a, b, coll)
text = text.."\n"..a:getUserData().." uncolliding with "..b:getUserData()
end
function preSolve(a, b, coll)
text = text.."\n"..a:getUserData().." touching "..b:getUserData()
end
function postSolve(a, b, coll, normalimpulse, tangentimpulse)
end