Rotate object post collision

I am hoping someone here will be able to point me in the right direction. I have an bullet that is fired and will bounce around the screen. When it collides with something and changes directions, I want the object to rotate so that it is still facing the “forward” direction. Obviously physics handles the movement and bouncing between walls, but the image is always facing the same direction.

I have tried a few different ideas, mainly using the getAngleTo function from the beebegames class, but nothing seems to work quite right. The angle will always be a little off and thus the image wont rotate in the right direction.

I basically need to figure out a way to determine the angle the bullet will impact the wall or the angle is headed after the collision. Any ideas? [import]uid: 48867 topic_id: 11190 reply_id: 311190[/import]

you can call this function in your collision handler function with a delay (to let bullet start travelling in new direction before it is called)

WARNING: Untested Code
[lua]–let’s assume your bullet object is called, well… bullet

function alignBullet()
local vx, vy = bullet:getLinearVelocity()
local angle = math.deg(math.atan(vy/vx))
bullet.rotation = angle

end
–in your collision handler use following line

timer.performWithDelay(50,alignBullet)[/lua]
[import]uid: 48521 topic_id: 11190 reply_id: 40639[/import]

That worked perfect!! I completely forgot about the getLinearVelocity function. The only issue I had was that if the bullet was traveling from right to left, it would be at the correct angle, but facing the wrong direction. I easily fixed that by checking that the x component of velocity was > 0. Thank you so much for you help!! [import]uid: 48867 topic_id: 11190 reply_id: 40672[/import]