2
votes

I'm not too great at math but what I am trying to accomplish is rotating my sprite to the direction of a vector impulse that is applied to it. My game is in 2D

If I tap below my sprite it goes upwards, if I tap below my sprite and to the right, it goes upwards and to the left, etc. etc.

I am doing this by just applying a normalized vector (unit vector?) impulse to my sprite which i got from the difference in position between the sprite and my tap.

let x = (ball.position.x - location.x)
let y = (ball.position.y - location.y)

let vectorLength = sqrt((x*x) + (y*y))
// Ignore constants
let unitVector = CGVector(dx: (x/vectorLength) * 25, dy: (y/vectorLength) * 50)

ball.physicsBody!.applyImpulse(unitVector)

Now what I would like to accomplish is the ability to rotate the sprite in the direction of that vector.

So for example if my sprite is traveling up and to the left at lets say a 45 degree angle, how can I rotate the sprite counterclockwise 45 degrees using the vector so that it appears to be facing the facing the direction its traveling?

1

1 Answers

3
votes

something like sprite.zRotation = atan(unitVector.dy, unitVector.dx)?