Moving an Object at any Angle

Started by
3 comments, last by Sean_Seanston 13 years, 2 months ago
I'm no good with angles really, but this morning I was thinking and I thought the problem of rotating an object, say 16 degrees, and have it travel along that path seemed easier than I first thought.

Tell me if I'm right or if I completely misread the situation ;)

With a speed of 5, going straight up (0 degrees) would mean an x velocity of 0 and a y velocity of 5. To maintain the same speed, the sum of the velocities will of course have to always equal 5.
Now, I figured 45 degrees was easy, just rotate the sprite 45 degrees and then set xVel to speed/2 and yVel to speed/2. Any other angle scared me since I thought I'd be dealing with cosin and who knows what else.

Then I figured... if I know 45... can't I just divide to get anything I want? I'd always thought calculating such a path would be more difficult but is it really this easy:

If 45 from vertical means xVel = speed/2 and yVel = speed/2, then 1 degree must mean that xVel = (speed/2)x(1/45) and yVel must be speed * (1 - xVel)?

Am I right?

So using a speed of 5, that would mean a 7 degree angle would give an xVel of (5/2)*(1/45) = 0.06 (rounded) and a yVel of 0.94?

Maybe that should be obvious, but I tend to be terrified of dealing with angles and I only just tried to work it out now B)
Advertisement

With a speed of 5, going straight up (0 degrees) would mean an x velocity of 0 and a y velocity of 5.

Although it's just a convention, 0 degrees/radians is generally taken to point along the (positive) x axis rather than the y axis.

To maintain the same speed, the sum of the velocities will of course have to always equal 5.[/quote]
That assumption is probably what's throwing you off.

The Pythagorean theorem states that given a right triangle, the square of the length of the hypotenuse is equal to the sum of the squares of the lengths of the other two sides of the triangle. In this case, the hypotenuse is the speed, and the other two sides are the x and y velocities.

Let 'x' and 'y' be the x and y velocities. You're proposing that:

x + y = 5

However, the actual relationship is:

x^2 + y^2 = 25

At a 45-degree angle we have:

x^2 + y^2 = 25
x = y

Solving:

x^2 + x^2 = 25
2x^2 = 25
x^2 = 25/2
x = y = ~3.54

And clearly:

3.54 + 3.54 != 5

So, the assumption that the sum of the component velocities must equal the speed is wrong.

If 45 from vertical means xVel = speed/2 and yVel = speed/2, then 1 degree must mean that xVel = (speed/2)x(1/45) and yVel must be speed * (1 - xVel)?[/quote]
Even if your initial assumption were correct, the above wouldn't work out correctly because x and y change nonlinearly as the angle changes linearly.

Short answer: You can compute the component velocities given an angle and a speed using trig, as follows (assuming 0 degrees/radians corresponds to the positive x axis):

x = cos(angle) * speed
y = sin(angle) * speed
I once tried to avoid sin and cos, and got bad results. But then I tried it, and was amazed with all the cool functions I can do :D

From my experiments angle 0 starts at the right and rotates clockwise.

Cos(0) will return a 1.0
Sin(0) will return a 0.0

If you multiplied those with your velocity, you will go right and not change you y axis.

There are 360 degrees in a circle. But Cos and sin use radians, every modern programming language I have seen provides an easy way to convert degrees into radians. Toradians is the common one.

Angle = toradians(45)
Cos(angle) returns 0.707106
sin(angle) returns 0.707106

As you can see it is the same number. When you multiply those with your velocity, you will move the same distance x and y.

Andle = toradians(70)
Cos(angle) returns 0.342020
Sin(angle) returns 0.939692

As you can see here, sin returns a bigger number then cos because our angle is closer to straight up.

You can keep going with this too. A number between 90 and 180 will give a negative number. Keep going all the way to 360. You don't have to stop there though.

Another function I found really useful is the atan2 function. It returns an angle in radians from the difference of two given points.

Good luck

I once tried to avoid sin and cos, and got bad results. But then I tried it, and was amazed with all the cool functions I can do :D

From my experiments angle 0 starts at the right and rotates clockwise.

Cos(0) will return a 1.0
Sin(0) will return a 0.0



Be careful. A clockwise rotation is actually negative, assuming the Z axis is pointing out of the clock. A positive rotation on the Z axis is actually counter clockwise. Look at the unit circle for confirmation. Regardless of handedness, a positive rotation of 90deg on the Z axis will rotate X towards Y, and Y towards -X.

That assumption is probably what's throwing you off.

The Pythagorean theorem states that given a right triangle, the square of the length of the hypotenuse is equal to the sum of the squares of the lengths of the other two sides of the triangle. In this case, the hypotenuse is the speed, and the other two sides are the x and y velocities.


Oh yeah, that's right... damnit. I knew there had to be something wrong with where I was going, I just couldn't see where :P

Even if your initial assumption were correct, the above wouldn't work out correctly because x and y change nonlinearly as the angle changes linearly.


I see. I really need to learn proper trigonometry...

Short answer: You can compute the component velocities given an angle and a speed using trig, as follows (assuming 0 degrees/radians corresponds to the positive x axis):

x = cos(angle) * speed
y = sin(angle) * speed


Nice, that doesn't seem too complicated.

Thanks all. I'll see if I can get this to work when it comes to implementation...

This topic is closed to new replies.

Advertisement