To check if the ball is in contact with the ground (or whatever) you need to define the callback functions for the 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
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 verified, in case 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
It would be interesting to know the mass of the ball, to see if it resists the movement due to an unfavorable mass/force ratio. In time: my knowledge in
Lua
are few... It would not be the case to tryobjects.ball.body:applyForce(0, 300)
(signal switching)?– Oralista de Sistemas
First of all thank you very much for the issues in my question, I’m new around here. The problem was the mass when I applied a higher force (in this case -2000), the ball actually jumped. The signal has to be negative because as the ball goes up the value of y decreases.
– BCasaleiro