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 › The moving origin
Page Index Toggle Pages: 1
The moving origin? (Read 561 times)
The moving origin?
Jan 30th, 2009, 12:56am
 
I've been working on a version of asteroids as a way to test out the programming language, but I've hit a bit of a snag.

Since it's important that the angle of the ship change (so that it might fire at incoming asteroids) I attempted to use the rotate function to turn the ship on a keypress. I managed to get it working to a certain degree, but translating the origin is no way to make a game with lots of moving parts.

So my question is this: Is it possible to keep the origin at a fixed location, and still have my ship rotate, while sticking to the mouse's current position?
Re: The moving origin?
Reply #1 - Jan 30th, 2009, 4:39am
 
not sure if i got it right. but did you use pushMatrix() and popMatrix()?
Re: The moving origin?
Reply #2 - Jan 30th, 2009, 6:44am
 
No. I used the rotate() function. If push and pop would work, would you know how to apply that to a triangle?
Re: The moving origin?
Reply #3 - Jan 30th, 2009, 11:42am
 
rotate is right, but it has to be called between push and popMatrix() like all the tranformatios/rotatens...
so you more and rotate the origin only for your spaceshop, the rest is untouched...

check this one

void setup(){
 size(400,400);
 smooth();
 frameRate(23);
 stroke(255);
 noFill();
}

void draw(){
 background(0);





 pushMatrix();
 translate(width/2,height/2);
 rotate(radians(frameCount));
 beginShape();
 vertex(-10,-20);
 vertex(0,0);
 vertex(10,-20);
 vertex(0,-15);
 endShape(CLOSE);
 popMatrix();

 ellipse(30,040,20,20);
 ellipse(200,100,40,40);
 ellipse(30,200,30,30);
 ellipse(100,400,20,20);
 ellipse(150,300,20,20);
 ellipse(300,300,20,20);

}
Re: The moving origin?
Reply #4 - Jan 30th, 2009, 10:40pm
 
Wow, thanks. That DID help. I applied my own code to it and now I have a rotating ship that moves with the mouse. It rotates on the tip of the ship, which is kinda wierd, but acceptable.

I have a question though: I wrote up a laser function, basing the starting point of the laser at the tip of ship, and using calculations to fire along the ship's trajectory.
Unfortunately...It seems to just travel from the original origin to the bottom right of the screen. What's wrong with this?

int angle = 0;
float lazcurX = mouseX;
float lazcurY = mouseY;
int i = 0;
float traj = ((180 + angle)*3.14)/180 ;
void setup(){
 size(400,400);
 smooth();
 frameRate(23);
 stroke(255);
 noFill();  
}
//This adjusts the angle of the ship
void keyPressed() {
  if (key == CODED) {
    if (keyCode == LEFT) {
     rotate(radians(angle +5));
    angle = angle +5;
    }
    if (keyCode == RIGHT) {
      rotate(radians(angle -5));
      angle = angle -5;
    }}}
 //The Laser Function
 //The general Idea here is to add the slope, generated by the angle of the ship, to fire the lazer
// Currently, the laser is firing from the origin for some reason. Im assuming this is because Im translating the origin to the mouse pos.
 void laser() {
   //while (i<80){
 stroke(222,255,90);
     line(lazcurX, lazcurY, lazcurX + traj, lazcurY + traj);
lazcurX = lazcurX + traj;
lazcurY = lazcurY + traj;
//i = i + 5;
 }
 // Fire the laser  with a mouse click  
   
void draw(){
 background(0);
noCursor();

//Draws the ship, using push and pop matrix to adjust the origin for the ship alone
noFill();
stroke(255);
 pushMatrix();  
 translate(mouseX,mouseY);
 rotate(radians(angle));
 beginShape();
 vertex(-10,-20);
 vertex(0,0);
 vertex(+10,-20);
 vertex(0,-15);
 endShape(CLOSE);
 popMatrix();

//Asteroids, Stationary for now
fill(177);
noStroke();
ellipse(30,040,20,20);
 ellipse(200,100,40,40);
 ellipse(30,200,30,30);
 ellipse(100,400,20,20);
 ellipse(150,300,20,20);
 ellipse(300,300,20,20);

}
void mousePressed() {
      laser();
    }
Re: The moving origin?
Reply #5 - Jan 30th, 2009, 11:22pm
 
fixing the ship is not a big problem :
 beginShape();
 vertex(-10,-10);
 vertex(0,10);
 vertex(+10,-10);
 vertex(0,-05);
 endShape(CLOSE);

but i guess you need to creat some classes to get it work. You will need colussion detection and so on... so even if you get it work and draw the laser, you need to check if a asteroid is hit or not... Dan Shiffmann just wrote a good new tutorial on OOP, i guess this would be a good start.
Page Index Toggle Pages: 1