Wow, lot of answers... to say they don't understand the topic... People like to increase their message count, don't they?
juststickman wrote:
_x += Math.cos(Math.atan2(ship._y-this._y, .ship._x-this._x)*-1)*5
_y -= Math.sin(Math.atan2(ship._y-this._y, ship._x-this._x)*-1)*5
Why did that work?
I thought you were supposed to use sin for x and cos for y...
No, that's the reverse, cos is the horizontal projection of the angle and sin is the vertical one. See the diagrams in Wikipedia's Trigonometric functions for example.
Let's break down these formulae:
- ship._x - this._x is the horizontal distance between the ship and the player.
- ship._y - this._y is the vertical distance between the ship and the player.
- atan2 is used to compute an angle between these two objects. Not sure why it is inverted (*-1). Even less for y which is inverted then subtracted...
- Thus, cos of this angle is near zero when the objects are one above the other, and near 1 when they are (nearly) on the same horizontal axis. And that's the reverse for sin.
- So, multiplied by 5, it gives move units in the right direction.
Last edited by Phi_Lho (2010-04-27 07:57:51)
Offline
Oh I just went absent minded for a few days ^.^"
EDIT: just one thing then... how come it's the other way with scratch?
Last edited by juststickman (2010-04-27 11:12:40)
Offline
juststickman wrote:
Oh I just went absent minded for a few days ^.^"
EDIT: just one thing then... how come it's the other way with scratch?
...its not.
I like how everyone ignored my answer, and then said that Phi Lo's was the best, even though mine said the exact same thing. No Offense Phi Lo.
Offline
lol, sorry, only taking Algebra 1.

Offline
joeisawesome wrote:
lol, sorry, only taking Algebra 1.
Me too.
That's why I was confused.
Offline