Search code examples
luacollision-detectioncoronasdk

object move making certain angle


I have a physics body (Balloons) and I am applying an impulse on collision.

What I want is that when the "player" collides with the balloons then it should move up (which is working pretty good), but it should take some angle according to the shape of the balloons when hit. It shouldn't always move vertically up.

It should move straight up only when hit at the topmost part of the balloons. In other cases it should move up with some angle. How can I do that?


Solution

  • From what I think your asking, here's one way to do it:

    You can find the angle of impact from the positions of your two objects (your player and the balloon). So you've got balloon.x, balloon.y, player.x, player.y.

    -- Two sides of a triangle opp and adj to the angle
    sideO = balloon.y-player.y  -- the opposite side
    sideA = balloon.x-player.x  -- the adjacent side
    
    -- To get the angle
    angle = math.atan(sideO/sideA)
    

    This may be backwards from what you want. If it is just try player.x-balloon.x instead (and same for y).

    Here's where I got the math. Also, the lua math library