Make the object move in the circle (asteroids game)

So basically you want an Asteroids ship. Why don’t people just say that?

(Hmm… maybe they don’t realize that this is what they’re making??)

class Ship {
  PVector p, v;
  float a;
  Ship() {
    p = new PVector( width/2, height/2 );
    v = new PVector( 0, 0 );
  }
  void draw() {
    simulate();
    render();
  }
  void simulate() {
    if ( key_down[2] ) { // LEFT
      a-=0.1;
    }
    if ( key_down[3] ) { // RIGHT
      a+=0.1;
    }
    a %= TWO_PI;
    if ( key_down[0] ) { // UP
      v.x+=.1*cos(a);
      v.y+=.1*sin(a);
    }
    p.x+=v.x;
    p.y+=v.y;
  }
  void render() {
    fill(255);
    translate(p.x, p.y);
    rotate(a);//map(millis()%7000,0,7000,0,TWO_PI));
    noStroke();
    quad(20, 0, -20, 10, -10, 0, -20, -10);
  }
}

Ship ship;

void setup() {
  size(1000, 1000);
  ship = new Ship();
}

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


char[] tracked_keys = { UP, DOWN, LEFT, RIGHT };
boolean[] key_down = { false, false, false, false };

void keyPressed() {
  keyTrack(true);
}

void keyReleased() {
  keyTrack(false);
}

void keyTrack(boolean b) {
  for ( int i = 0; i < tracked_keys.length; i++) {
    if ( keyCode == tracked_keys[i] ) {
      key_down[i] = b;
    }
  }
}
1 Like