Game programming techniques – Asteroids movement.

 

By Michael Smith

1st May 2014


Asteroids Movement

When I say Asteroids movement, I mean Asteroids the game – the ship movement. Not lumps of rock floating through space style movement.

So the Asteroid game has a little ship that moves on a 2D plane, usually the x,y plane.  The ship is moved by pressing the thrust key and velocity is added to the direction the ship is facing. There is a drag factor (yeah in space smartarse, this game was made in the 80′s – back then no-one had  invented physics yet. Oh and FYI the music in the 80′s was wayyyyyyy cooler than any decade since.) that slows the ship if thrust isn’t pressed.

Things we need to know to be able move our ship and emulate Asteroids movement:

  • The angle in which the ship is pointing – we’ll call this Ang
  • The thrust – we’ll call this thrust ;)
  • The x velocity of the ship (vel.x)
  • The y velocity of the ship (vel.y)
  • The x position of the ship (pos.x)
  • The y position of the ship (pos.y)

All code in this explanation is in Pseudo Code – Pseudo from the Greek ‘Pse’ which means ‘talk out of your’ and ‘udo’ which is Greek for what French would call derrière.  <– A Pseudo Factoid.

At the end I have written a short example in Unity using c#

First, the ships angle – ang.  Pseudo code:

Float ang=0
game loop
{
     if left pressed {ang+=(experiment with value)}
     else if right pressed {ang-=(experiment with value)}
     rotate sprite/object
}

Now the thrust, again in Pseudo Code:

float thrustMax = 0.5 // we need a maximum thrust, experiment with this value until you get it right.
float thrustInc =0.05 // the amount we want to increment the thrust by, again experiment.
float thrust=0
game loop
{
     ...left/right code...
     if thrust key pressed {thrust=addThrust(thrust+thrustInc)}
}

function float addThrust(t)
{
     if (t>thrustMax)t=thrustMax}
     return t
}


Now the velocities of the ship. We have the thrust value, but we need to break that down into the amount of thrust to add to the x vector and the amount of thrust to add to the y vector. Yay for the right angled triangle:

Asteroids movement

We know the hypotenuse (thrust) and we know theta(angle of the ship), so with some basic trig we can calculate the adjacent(x vector thrust) and the opposite (y vector thrust).

If your chosen language uses degrees for the trig functions then fine.  If it use radians, which most languages do, then you need to turn your ang variable from degrees to radians.  To do this just multiply by Pi/180.

Pseudo Code:

vector2 pos=new vector2(middle of screen) // vector2 is just a type of variable with 2 floats - x + y
vector2 vel=new vector2(0,0)
game loop
{
    ...left right code...
    ...thrust code...
    vel=addVel(vel,thrust,ang)
    pos=posShip(pos,vel)
}
function vector2 addVel(vector2 v,float h,float th)
{
    //change th to radians if need to:
    th=th*pi/180
    v.x+=cos(th)*h
    v.y+=sin(th)*h
    v=v*0.95// drag (experiment with this figure)   
    return v
}
function vector2 posShip(vector2 p,vector2 v)
{
    p+=v
    if (p.y>top of screen){p.y=bottom of screen}
    else if (p.y<bottom of screen){p.y=top of screen}
    if (p.x>screen right) {p.x=screen left}
    else if (p.x<screen left) {p.x=screen right}
    position sprite/object
    return p
}

Now then, I did a little test in unity, just to ensure the code works.  Ooooh a video:

And there you go, hope you found it useful. If you’d like an e-mail whenever I post my narcissistic ramblings interesting articles, then please subscribe to my blog (top of page, right hand panel).

“To part is the lot of all mankind. The world is a scene of constant leave-taking, and the hands that grasp in cordial greeting today, are doomed ere long to unite for the the last time, when the quivering lips pronounce the word – ‘Farewell” – R.M. Ballantyne 

2 thoughts on “Game programming techniques – Asteroids movement.