[Resolved] Linear Impulse and Collision

I am trying to make it so that whenever my ball which is effected by gravity hits the floor it will bounce back up. I currently am using the following code which allows the ball to bounce once but then after the first bounce it stops. I believe the problem is gravity and that once the first linear impulse is applied, the second cannot be because gravity from the ball falling back down prevents the second linear impulse. I think a timer that delays the linear impulse might help this but I do not know how to make one. Here is my code so far. Any help would be great thanks.
local physics = require( “physics” )
physics.start()

local rect = display.newRect(0, 300, 480, 4)
physics.addBody(rect, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor = false})

local circle = display.newCircle(50, 250, 10)
physics.addBody(circle, “dynamic”, {density = 1.0, friction = 0, bounce = 0, radius = 10, isSensor = false})

rect.myName = “rect”
circle.myName = “circle”

circle:addEventListener(“collision”, circle)

function circle:collision (event)
if event.other.myName == “rect” then
circle:applyLinearImpulse(0, -1.998, circle.x, circle.y)
end
end
[import]uid: 126017 topic_id: 22996 reply_id: 322996[/import]

Moved to Developer Support - please leave the Feature Request forum just for feature requests :wink:

Now, you would be better off using setLinearVelocity for consistency. Try this;

[lua]local physics = require( “physics” )
physics.start()

local rect = display.newRect(0, 300, 480, 4)
physics.addBody(rect, “static”, {density = 1.0, friction = 0, bounce = 0, isSensor = false})

local circle = display.newCircle(50, 250, 10)
physics.addBody(circle, “dynamic”, {density = 1.0, friction = 0, bounce = 0, radius = 10, isSensor = false})

rect.myName = “rect”
circle.myName = “circle”

circle:addEventListener(“collision”, circle)

function circle:collision (event)
if event.other.myName == “rect” then
if event.phase == “began” then
circle:setLinearVelocity(0, -200)
end
end
end[/lua] [import]uid: 52491 topic_id: 22996 reply_id: 91895[/import]

Thank you very much Peach, Also thank you very much for your tutorials:) [import]uid: 126017 topic_id: 22996 reply_id: 91966[/import]

No worries at all :slight_smile:

By the way, if in the future you could use < lua > and < /lua > tags around your code would be appreciated; is easier to read :wink:

Peach :slight_smile: [import]uid: 52491 topic_id: 22996 reply_id: 92121[/import]