3

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.

Stefan
  • 5,203
  • 8
  • 27
  • 51
Amani Elsaed
  • 1,558
  • 2
  • 21
  • 36
  • One temporary solution I have found is checking the sprite's velocity when it collides and if it is less than minimum value, apply impulse on the sprite with large values to avoid deadlock. I have also tried to set the dx and dy of the velocity, but this changed the sprite speed significanlty, which I didn't want. – Amani Elsaed Nov 18 '15 at 18:22

1 Answers1

2

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

Amani Elsaed
  • 1,558
  • 2
  • 21
  • 36