Asteroids Ship Movement

| | August 10, 2015

I have read source code of asteroids game. I want to know why when updating the ship’s position in X, and Y Axis, we must write it in sin and cosine of the current angle. Is it angular velocity ? why we can’t use linear velocity and update the position by a linear velocity?

2 Responses to “Asteroids Ship Movement”

  1. Look at the unit circle below, [cos(t), sin(t)] simply gives you a 2D unit vector for an angle of t. For any direction between 0 and 2PI (0* and 360*) just feed cos/sin the correct angle. Want to go west? Use t=PI radians (or 180* degrees), this gives you the unit vector [cos(PI), sin(PI] or [-1,0].

    unit circle

  2. Think of it as shooting a cannon.

    When you shoot a cannon parallel with the ground with a given force, it is guaranteed to go horizontally and not vertically. This is because we apply force in this direction. However, if we were to apply a linear velocity to both the ‘X’ and ‘Y’ components, this would create a constant speed which would look silly.

    For Asteroids, you want to make sure a force is applied in the right direction. Otherwise, what would happen is your asteroid was pointing directly east or west? Would you still want it to move at the same direction each time? While pushing Right, you want the computed magnitudes of each components velocity to be correct.

Leave a Reply