Concept for moving in a direction for ship in game asteroids

I want to recreate asteroids game but don’t know how to program ship moving in the direction of it’s aim.

Hi Arsenii, Probably the best way to understand your issue is to post your code.

1 Like

I want my ship to move in the direction that it aims but all my variants do not work at all.Thank you in advance.

spaceShip ship;
boolean left = false;
boolean right = false;
boolean forward = false;
float maxSpeed =  0.25;
float accelL = -0.45;
float accelR = 0.45;
float accelF = -0.05;
float inercia;
void setup() {
  size(1200, 700);
  ship = new spaceShip();
}



void draw() {
  background(0);
  ship.render();
  ship.edges();
}

void keyPressed() {
  if (keyCode == RIGHT) {
    ship.turn(accelR);
  }
  if (keyCode == LEFT) {
    ship.turn(accelL);
  }
  if (keyCode == UP) {
    for (float i = 0.1; i < maxSpeed; i+=0.05) {
      accelF -= i; 
      ship.forward(accelF);
    }
    forward = true;
    accelF =-0.05;
  }
}
class spaceShip {
  PVector pos = new PVector (width/2, height/2);
  float r = 10;
  float heading = 0;
  spaceShip() {
  }

   void render() {
    translate(pos.x, pos.y);
    noFill();
    stroke(255);
    rotate(heading);
    triangle(-r, r, r, r, 0, -r);
  }
  void turn(float angle) {
    heading += angle;
  }
  void forward(float yspeed) {
    float radians = (float)Math.PI * heading / 180;
    pos.y += Math.sin(radians) * yspeed;
    pos.y  -= Math.cos(radians) *yspeed;
  }
  void edges() {
    if (pos.x > width)
      pos.x = 0;
    if (pos.x < 0)
      pos.x = width;
    if (pos.y > height)
      pos.y = 0;
    if (pos.y < 0)
      pos.y = height;
  }
}
2 Likes

I’m a novice myself, but I notice your forward() method is only translating the y coordinate. If you want the ship to move with the heading, i.e. ‘move in the direction of its aim’, you’ll need to do something with the x coordinate.

  void forward(float yspeed) {
    float myRadians = (float)Math.PI * heading / 180;
    pos.y += Math.sin(myRadians) * yspeed;
    pos.y -= Math.cos(myRadians) *yspeed;
  }

I hope someone else can be of more help.

EDIT - In the code above, I changed your variable name radians to myRadians because radians is a built-in function in Processing. https://processing.org/reference/radians_.html

1 Like

@timAllan, you are correct. Set the following line

pos.y -= Math.cos(myRadians) *yspeed;

to

pos.x += Math.cos(myRadians) *yspeed;

Also, @Arsenii, it appears that you are using both radians and degrees in your sketch.
The values accelR and accelL are quite small for degrees.

Your overall logic is sound but your ship will have to spin a lot to orient to the correct direction and that would make the pilot dizzy. :wink:

3 Likes