greyskale
YaBB Newbies
Offline
Posts: 36
stupid angle problem
Jun 1st , 2006, 1:24am
i'm playing with a simple idea of a ship moving around in space ala "asteroids". the problem is, i can't remember how to calculate angles to make the ship accelerate in its forward direction. here's what i have so far... suggestions appreciated. int top = 0; //top of window int bottom = 400; //bottom of window int left = 0; //left of window int right = 300; //right of window float x = 150; //starting position x axis float y = 200; //starting position y axis float speed = 0; //speed of the ship float drag = .98; //amount of drag or friction on ship float angle; boolean[] keysPressed = new boolean[128]; void setup() { size(300, 400); //uh... window size } void draw() { keyState(); move(); wrap(); background(0); //background color pushMatrix(); translate(x, y); rotate(angle); noFill(); //no fill on the ship (wireframe) stroke(255); //color of wireframe beginShape(POLYGON); //start of shape draw for ship vertex(-1, -4); vertex(1, -4); vertex(3, -6); vertex(3, -8); vertex(5, 2); vertex(7, 4); vertex(4, 7); vertex(1, 5); vertex(-1, 5); vertex(-4, 7); vertex(-7, 4); vertex(-5, 2); vertex(-3, -8); vertex(-3, -6); endShape(); //end of shape draw for ship popMatrix(); translate(x, y); } void keyPressed() { keysPressed[keyCode] = true; } void keyReleased() { keysPressed[keyCode] = false; } void move() { y = y - speed; speed = speed * drag; } void wrap() { if(y < top - 8){ //top screen test y = bottom + 8; //wrap ship to bottom } if(y > bottom + 8){ //bottom screen test y = top - 8; //wrap ship to top } } void keyState() { if(keysPressed[UP]){ speed += .125; } if(keysPressed[LEFT]){ angle -= .0625; } if(keysPressed[RIGHT]){ angle += .0625; } } i know i only have the y axis set up for movement right now, but the angle calculation and how to apply it escapes me at the moment.