Search code examples
iossprite-kitskspritenodeskphysicscontact

How to set minimum collision angle for SKSpriteNode


How can I set a minimum collision angle for an SKSpriteNode object so that when it collides with any other sprite, its reversed angle is always greater than or equal to this angle - either negative or positive - ?

My objective is to prevent deadlock collision between two parallel edges - for example -, so that the ball won't oscillate between them forever.


Solution

  • Finally, I have found a solution - may be not the best but it works fine -.

    When the sprite collides with other one, I find the sprite velocity angle from the equation:

    angle = atan(dy/dx)

    and check the angle value. If it is less than the min one, I set the angle to the min value and then find the new velocity dy from the equation:

    dy_new = dx * tan(new_angle)

    and find the dx_new from the equation:

    dx_new = sqrt(pow(dx, 2) + pow(dy, 2) - pow(dx_new, 2));

    This means that the speed magnitude is the same but only the direction is changed. This is based on the vectors equations:

    speed = sqrt(pow(dx, 2) + pow(dy, 2)); and
    angle = atan(dy/dx);

    Here is the link of the velocity vectors mathematics: http://faculty.wwu.edu/vawter/PhysicsNet/Topics/Vectors/TheVelocityVector.html