Impulse with velocity

Hey,

I have a ball, once it hits an object the ball repositions with the same velocity as it hit the object. Right
now i have ball that falls from a square then hits a second square, therefore repositions it self to where it first fell from with the same velocity. The problem is that at first the ball is dynamic and that is ok, but once it touches the second square and repositions i’d like to have a linear impulse times the velocity. The ball gets the velocity as it touches the second square when it reappears, i just need to have an impulse that is the same speed of when the ball touches the second square. Can any one help adding a linear impulse times the velocity to this? Thanks a lot! I’m sorry if i made it confusing! Try it out to see what i mean. *just to confirm it, at the moment a ball hits a square then falls down dynamically with the same velocity. I just wanted the ball to have an impulse with the same velocity.* Thanks again.

The code:

[code]

local physics = require(“physics”)
physics.start()
physics.setGravity(0,5)

local portal1 = display.newRect(150, 100, 50, 50)
portal1:setReferencePoint(display.CenterReferencePoint)
local portal2 = display.newRect(150, 400, 50, 50)
portal2:setReferencePoint(display.CenterReferencePoint)
portal2.id = “portal2”

local ball = display.newCircle(175, 200, 20)
ball:setFillColor(255,0,0)
local ball2 = display.newCircle(0, 0, 20)
ball2:setFillColor(255,0,0)
ball2.isVisible = false
ball2.x = portal1.x
ball2.y = portal1.y

– PHYSICS OBJECTS –
physics.addBody(portal1, “static”, {isSensor=true})
physics.addBody(portal2, “static”, {isSensor=true})
physics.addBody(ball, “dynamic”, {radius=20})
physics.addBody(ball2, “static”, {radius=20})

local onCollision = function(event, self)
local speedX, speedY = ball:getLinearVelocity()

if event.phase == “began” then
if event.other.id == “portal2” then
ball2.bodyType = “dynamic”
ball2:getLinearVelocity(speedX, speedY)
ball2.isVisible = true
if ball.x == portal2.x then
ball.isVisible = false
end
end
elseif event.phase == “ended” then
ball:removeSelf()
end
end

ball:addEventListener(“collision”, onCollision)
[import]uid: 23689 topic_id: 14933 reply_id: 314933[/import]

If you change line 31 to “get” from “set” that should carry over the same speed as the ball was moving when it hit the second square :slight_smile: [import]uid: 52491 topic_id: 14933 reply_id: 55176[/import]

Hey,

Thanks for you reply but line 31 is “get”.

I wanted to add something like, ball2:applyLinearImpulse(0,0.2,ball2.x, ball2.y). But instead replace that with “speedX”, “speedY”? So that theres an impulse the same as the current speed of the ball when it hits the square. Thank you. [import]uid: 23689 topic_id: 14933 reply_id: 55223[/import]

Apologies! I mistyped that. Change it to SET on line 31, it will then pop out at the same speed it was traveling the moment it hit the lower square :slight_smile:

Try that out :slight_smile: [import]uid: 52491 topic_id: 14933 reply_id: 55286[/import]