# Rotating Sprites to Face Movement Direction in Scene browsing

• posted
0

So I am making a game, and I am having touch controls (obviously) for the game. I have it to where the spaceship will fly to the location that you tap on. But I can't figure out how to make it rotate to face it's direction of movement.

Any help?

• posted
0

are you having trouble figuring out rotation in general? Or specifically rotating to point at a specific location?

• posted
1

@AtomBombed If you post code from your failed attempt I am sure someone will help you out. Without the specifics of the situation I can only give you the hint that math.atan2() is a very handy function.

• posted
0

Here, I'll try to explain it to someone like me who hasn't taken pre-calculus yet 😉. I'm in 9th grade Geometry, I have no idea where you are but I think you are young.

Scene works on a cartesian coordinate system. This describes points in space with (x, y) coordinates. A very useful thing for tasks like this is a polar coordinate system. A polar coordinate system describes points like (r, θ).

What does this mean? r represents a radius, and θ represents an angle. Think of it like everything being on circles. Take the polar point (3, 60). First, we can make an imaginary circle around the origin with a radius of 3. Then, go 60 degrees up on it. That's your point. See the dot on the green arm on this illustration of how it works: If you convert the point of the touch to polar coordinates with the center at the spaceship, that will give you the angle that the spaceship needs to face.

Here's some code. Given the coordinates of your spaceship, `a`, and the coordinates of the touch, `b`, this will return the angle which `a` needs to face in order to point at `b`. This angle will be so that 0 degrees is pointing straight left.

``````import math

def angle(a,b):
"""What angle point 'a' needs in order to face point 'b' """
x1,y1=a
x2,y2=b

def cartToPol(x,y):
"""Convert cartesian coordinates to polar coordinates"""
radius = math.sqrt(x**2 + y**2) #Pythagorean theorem, a**2+b**2=c**2
theta = math.atan2(y,x) #Wikipedia told me to do this. Don't ask why. This seems to give a theta between pi and negative pi
theta += math.pi #Now it's between 0 and 2pi, which is radians
#theta *= 180/math.pi # node.rotation is in radians. To get degrees, uncomment this.

#0 degrees points straight left.

return cartToPol(x2-x1,y2-y1)
``````

• posted
1

@Webmaster4o I'm actually a 9th grader, too. You'd be surprised. I learn fast. Thanks for the post.

• posted
0

@AtomBombed as a curious person, you can learn a lot by reading https://en.wikipedia.org/wiki/Polar_coordinate_system. It's pretty clear. I was able to understand just by ignoring everything that I didn't understand 😆

• posted
1

@Webmaster4o alright thanks. Appreciated!

Internal error.

Oops! Looks like something went wrong!