Rotate Points in 2D Space, Compute Thrust and Bullet Direction or....

"Asteroids Math" by PhilVaz

This simple PHP game utility rotates a 2D point around the origin (x=0, y=0) using the formula

xnew = x * cos(angle) - y * sin(angle)
ynew = y * cos(angle) + x * sin(angle)

and displays all the newly rotated positions. The value of angle is supplied in degrees -- the program converts the angle to radians to compute sin/cos. This can be used to pre-compute all the rotation positions of a ship in space (e.g. for an asteroids clone). You can input either the angle of rotation, or the total number of angle positions required. You can also have each rotated point rounded up to the nearest whole integer.

Also available is to pre-compute x/y thrust and x/y bullet movement in the correct direction of the object (e.g. a ship in space). These are computed using the formula

xthrust = xthrust + ( sin(angle) * THRUST_SPEED )
ythrust = ythrust + ( -cos(angle) * THRUST_SPEED )

xbullet = sin(angle) * BULLET_SPEED
ybullet = -cos(angle) * BULLET_SPEED

Point = ( xy)

Point = ( , )

Angle = degrees or Rotate Positions = angle positions (360 or less)

THRUST_SPEED Constant = (e.g. between 0 and 10)

BULLET_SPEED Constant = (e.g. between 0 and 20)

Round Up Point?

Back to VazGames.com