Jump to content

Sprite direction


zelcard
 Share

Recommended Posts

Hello guys its me again with another noob question.

I have a rotating circle and when player sprite collides with the circle they both start rotating together, the thing I want is when the button is pressed the player sprite jumps away from circle. So how do I make player sprite always facing direction from the circle and not in the circle. Please check out the example below:

4bBe39R.png

The red arrow is player sprite that is rotating around the black cricle, number one example is how it works now and example 2 is how I need it to work. I need player sprite to "turn its back" to the circle no matter from which side they collide. Thanks for your help and sorry for the lack of information, I know this may be very confusing but I really dont have idea how to put this question right, but I gave it a try.

Link to comment
Share on other sites

Let us assume C the center of your black circle, and P the center of the player.

You need first to compute the vector CP from C to P. CP = P - C

Given this vector CP, you need to normalize it to a unit vector, CPu by divinding each coordinate by its length.

Then, you can the compute the angle alpha of this vector with the help of the JavaScript built-in trigonometric functions Math.acos() and Math.asin() or more simply with Math.atan2(CP.y, CP.x) which doesn't even require to normalize CP before.

You can finally assign your sprite's rotation attribute this angle alpha, with some constant given the initial orientation of your sprite.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...