Physics : endContact callback triggering too late ?

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
agaetis
Prole
Posts: 3
Joined: Sun Jan 07, 2018 9:12 pm

Physics : endContact callback triggering too late ?

Post by agaetis »

Hello,

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

User avatar
erasio
Party member
Posts: 118
Joined: Wed Mar 15, 2017 8:52 am
Location: Germany

Re: Physics : endContact callback triggering too late ?

Post by erasio »

It appears that for complexity reasons the collision code is run before simulation. Resulting in contact updates happening one step (or update) after modifications.
An easy fix would be substepping which doesn't just help with the contact update but also with general simulation precision (which is better the smaller the time step is):

Code: Select all

text = text .. "\nUpdate world"
world:update(dt/2)
world:update(dt/2)
agaetis
Prole
Posts: 3
Joined: Sun Jan 07, 2018 9:12 pm

Re: Physics : endContact callback triggering too late ?

Post by agaetis »

Thanks Erasio.
I didn't know Box2D had this limitation.
I will use this fix, especially if it increases the precision.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 64 guests