Apply a force in an opposite direction

Hi,

I have an item that hits an object, I’d like to apply a force in the opposite direction, so for example if the item hits the object on a 45 degree angle, I would like it to get an extra boost of 45 degrees in the opposite direction.

I know I can use the applyLinearImpulse but how would this work??

Thanks! [import]uid: 72726 topic_id: 15752 reply_id: 315752[/import]

vx, vy = item:getLinearVelocity()

item:applyLinearImpulse(-vx, -vy)

The timing is important bc if it’s already bouncing off when you get LinearVelocity it will probably throw the item in a different direction. You may need to use preCollision event. Not sure.
[import]uid: 40137 topic_id: 15752 reply_id: 58176[/import]

I tried this, the force of the ball was around 200 when it hit… I monitored this and it was about right, but when it applies the same 200 in reverse, it flys of the screen as if its just been hit very hard!

I only want a small impulse so at the minute i’ve just put it to applyLinearImpulse(4,4), this works ok until the object hits it from the opposite direction in which case it goes back towards itself lol. [import]uid: 72726 topic_id: 15752 reply_id: 58182[/import]

Maybe I misunderstood what you’re trying to do.

Just reread your original post. If your object impacts at a 45 degree angle, the angle of the bounce is going to be affected by the object’s shape, friction, whether or not it’s spinning, etc. Are you wanting it to bounce back in the direction it came from? Or add a jolt of speed to whichever direction it naturally bounces off? [import]uid: 40137 topic_id: 15752 reply_id: 58193[/import]

I want to add a jolt of speed to whichever direction if naturally bounces off, at the minute it is bouncing off a rectangle, but that will change to a circle once the graphics are complete. [import]uid: 72726 topic_id: 15752 reply_id: 58196[/import]

Oh, OK.

local boost = function(obj)  
local boostFactor = 1.1  
  
local vx, vy = obj:getLinearVelocity()  
  
obj:setLinearVelocity(vx\*boostFactor, vy\*boostFactor)  
  
end   

From your collision handler call:

timer.performWithDelay(someLengthOfTimeInMilliseconds, function() boost(myObject) end, 1)  

You will have to experiment with the length of delay, but probably 1 would do it. You want the object going in the new direction and then set the velocity directly instead of using applyLinearImpulse. A boostFactor of 1.1 will give you 10% more velocity, 1.2 will give you 20% more, etc.

[import]uid: 40137 topic_id: 15752 reply_id: 58217[/import]

That’s a nice clean way of doing it and one way I hadn’t even thought about. This should do the job perfectly. Thanks for this. [import]uid: 80968 topic_id: 15752 reply_id: 58233[/import]