To check if the ball is in contact with the ground (or whatever) you need to define the callback functions for object collisions
g = true
w = love.physics.newWorld(0, 10, true)
w:setCallbacks(beginContact, endContact, nil, nil) --[[Define o nome da funçao a ser
executa quando contato é estabelecido entre dois objetos e quando o mesmo termina]]
--Definimos agora a funcao
function beginContact (a, b, ev) --Objeto(fixture) a e b e evento de colisão
g = true
end
function endContact(a, b, ev)
g=false
end
--Modificamos o tratamento de teclas
function love.keypressed( key )
if key == "w" and g then
print("Teste")
objects.ball.body:applyForce(0, -300)
end
end
In this way, the force will only be applied when g = true (ball in contact with the ground), note that the objects that participated in the collision event were not checked, if there are other objects it will be necessary to implement this verification. >
This implementation can be done using the setUserData () and getUserData () methods of the table returned by love.physics.newFixture ()
bola.fixture:setUserData("Bola")
chao.fixture:setUserData("Chao")
function beginContact (a, b, ev)
if a:getUserData() == "Bola"and b:getUserData() == "Chao" or
a:getUserData() == "Chao" and b:getUserData() == "Bola" then
g = true
end
end
Example