I am having some trouble with resolving collisions. I have a collisions table that collisions are pushed to whenever the script receive’s a contact_point_response message. When the object moves, it travels at a velocity that begins at its initial velocity and is lerped to the velocityGoal, to give a smoother transition. Upon colliding with something, I want it to bounce off at an angle corresponding to the angle that it entered at. The script I have below works for some cases, but other times it passes straight through the collision. Any help? Thanks!
for i = 1, #collisions do
local collisionNorm = collisions[i]["normal"]
local collisionDist = collisions[i]["distance"]
if collisionDist > 0 then
if (velocity.x > 0 and collisionNorm.x < 0) or (velocity.x < 0 and collisionNorm.x > 0) then
velocity.x = velocity.x * -1
velocityGoal.x = velocityGoal.x * -1
end
if (velocity.y > 0 and collisionNorm.y < 0) or (velocity.y < 0 and collisionNorm.y > 0) then
velocity.y = velocity.y * -1
velocityGoal.y = velocityGoal.y * -1
end
local newpos = selfPos + collisionNorm * collisionDist
go.set_position(newpos)
end
end
--clears the collision array
collisions = {}