We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › stupid angle problem
Page Index Toggle Pages: 1
stupid angle problem (Read 1042 times)
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.
Re: stupid angle problem
Reply #1 - Jun 1st, 2006, 7:58am
 
use this in move(){}

x += sin(angle)*speed;
y += cos(angle)*speed;

regards
-seltar
Re: stupid angle problem
Reply #2 - Jun 1st, 2006, 9:28am
 
thanks a ton... that took care of most of it... now i just need to get it to coast in the original direction of acceleration regardless of angle until the ship accelerates again...

...if that makes any sense Smiley


it's late...
Re: stupid angle problem
Reply #3 - Jun 1st, 2006, 11:58am
 
Shouldn't that be

x += sin(angle)*speed;
y -= cos(angle)*speed;
Re: stupid angle problem
Reply #4 - Jun 1st, 2006, 6:49pm
 
fixed that already... still trying to get around the issue of the ship not moving in a direction until it's thrusting in that direction
Re: stupid angle problem
Reply #5 - Jun 1st, 2006, 7:21pm
 
Code:

float x = 150; //starting position x axis
float y = 200; //starting position y axis
float speed = 0; //speed of the ship
float drag = .994; //amount of drag or friction on ship
float mangle, fangle; //moving angle, floating 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(fangle); //rotate the ship according to the floating angle
noFill(); //no fill on the ship (wireframe)
stroke(255); //color of wireframe
ship(); //drawing the ship
popMatrix();
translate(x, y);
}

void ship()
{
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
}

void keyPressed() {
keysPressed[keyCode] = true;
}

void keyReleased() {
keysPressed[keyCode] = false;
}

void move()
{
x += sin(mangle)*speed;
y -= cos(mangle)*speed;
speed = speed * drag;
}

void wrap()
{
if(y < - 8){ //top screen test
y = height + 8; //wrap ship to bottom
}else if(y > height + 8){ //bottom screen test
y = - 8; //wrap ship to top
}

if(x < - 8){ //left screen test
x = width + 8; //wrap ship to right
}else if(x > width + 8){ //right screen test
x = - 8; //wrap ship to left
}
}

void keyState()
{
if(keysPressed[UP]){
speed += .125;
mangle = fangle; //set the moving angle to be what the floating angle is set to be
}
if(keysPressed[LEFT]){
if(keysPressed[UP]){
mangle -= .0625;
fangle = mangle; //if up is pressed, set floating angle to be mangle, so there's no twitching
}else{
fangle -= .0625;
}

}
if(keysPressed[RIGHT]){
if(keysPressed[UP]){
mangle += .0625;
fangle = mangle; //if up is pressed, set floating angle to be mangle, so there's no twitching
}else{
fangle += .0625;
}
}
}
Re: stupid angle problem
Reply #6 - Jun 1st, 2006, 7:54pm
 
ah yes... thank you much! you've helped me get closer Smiley
Page Index Toggle Pages: 1