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 › 3D Space simulator
Page Index Toggle Pages: 1
3D Space simulator (Read 1265 times)
3D Space simulator
Feb 9th, 2006, 7:15pm
 
Hi im trying to develop a 3D space sim using processing. i have already developed a model that the player will be able to fly around. the problem is actually moving it.

im having trouble with turning the ship. I am fairly new to processing and am having major difficulty with this!

i know i have to use a combination of translate and rotate commands to make the ship move but im unsure how to implement them. i have tried multiple methods all to no avail.

I would really appriciate some help help on this.
A code example would be excellent

Thanks Graham!
Re: 3D Space simulator
Reply #1 - Feb 10th, 2006, 10:57pm
 
Have you had a look at pushMatrix and popMatrix?
http://processing.org/reference/pushMatrix_.html
http://processing.org/reference/popMatrix_.html

I don't know whether you're stuck on those or just on moving something in space, so I've made an example that hopefully covers both:

float radius; // radius of orbit
float angle; // current angle (in degrees)
float speed; // angle to increment orbit by each loop
float rotation; // rotation of the scene

void setup () {
 size(300,300,P3D);
 radius = 100;
 angle = 0;
 speed = 0.5;
 rotation = radians(0);
}

void draw () {
 background(255);

 // this translate isn't push/popped
 // so it'll apply generally (i.e. to the whole scene)
 translate(width/2,height/2,0); // puts the cartesian point 0,0,0 in the middle of the window
 rotation += radians(0.1);
 rotateX(rotation); // rotates the whole scene on the x axis

 // calculate new position of the box
 float xpos = sin(radians(angle))*radius;
 float ypos = cos(radians(angle))*radius;
 angle += speed;

 // this translate has pushMatrix and popMatrix before and after it
 // so it only applies to the created box
 pushMatrix();
 fill(123);
 translate(xpos,ypos,0);
 rotateZ(radians(angle)*-1); // orient the cube to its orbit
 box(20,20,20);
 popMatrix();

 // this cirlce is stationary in space
 noFill();
 ellipse(0,0,200,200);

}
Re: 3D Space simulator
Reply #2 - Feb 14th, 2006, 1:20pm
 
Ok ive been studying the code you showed me and i think i have the idea of it (im still having trouble understanding SIN and COS) i now wish add some user input when the user hold the left KEY down the object (box in this case) should rotate. when they press the up key the box should move in the direction that it is facing.

any ideas on how to achive this. ive had some success but the box will not rotate corectly as it moves around the circle

import processing.opengl.*;

float radius; // radius of orbit
float angle; // current angle (in degrees)
float speed; // angle to increment orbit by each loop
float rotation; // rotation of the scene
float xpos;
float ypos;



void setup () {
 size(900,900,OPENGL);
 radius = 100;
 angle = 0;
 speed = 0.5;
 rotation = radians(0);
}

void draw ()
{
 int turnT = 0;
 background(255);
 translate(width/2,height/2,0);
 if(keyPressed)
 {
   if(keyCode  == LEFT)
   {
     xpos = sin(radians(angle))*radius;
     ypos = cos(radians(angle))*radius;  
     angle += speed;
  }
   else if(keyCode == UP)
   {
     ypos += speed;
     turnT = 1;
   }
 }
 pushMatrix();
 fill(123);
 
 rotateZ(radians(angle)*-1);
 translate(xpos,ypos,0);
   
 box(20,20,20);
 popMatrix();
 
 noFill();
 ellipse(0,0,200,200);
}

any help you can give would be much appriciated

thanks Graham
Re: 3D Space simulator
Reply #3 - Feb 16th, 2006, 8:26am
 
I'm not sure what you want to do with the up key. How do you know which way a box is facing?

If you want to sort of give it a front and a back, and move it forwards whatever direction it's facing in, then you're probably best off looking at vectors, which is a whole new kettle of wolves.

As for Cos and Sin, if you need to know about trigonometry, start here:
http://en.wikipedia.org/wiki/Trigonometry#About_trigonometry

Then this which hopefully explains what's going on:
http://redcrayon.do.sapo.pt/orebeeeet.png

The cube rotating correctly can be sorted by switching round your rotate and translate:

import processing.opengl.*;

float radius; // radius of orbit  
float angle; // current angle (in degrees)  
float speed; // angle to increment orbit by each loop  
float rotation; // rotation of the scene  
float xpos;
float ypos;



void setup () {  
 size(900,900,OPENGL);  
 radius = 100;  
 angle = 0;  
 speed = 0.5;  
 rotation = radians(0);  
}  

void draw ()  
{  
 int turnT = 0;
 background(255);  
 translate(width/2,height/2,0);
 if(keyPressed)
 {
   if(keyCode  == LEFT)
    {
xpos = sin(radians(angle))*radius;  
ypos = cos(radians(angle))*radius;  
angle += speed;
  }
   else if(keyCode == UP)
   {
ypos += speed;
turnT = 1;
   }
 }
 pushMatrix();  
 fill(123);  
 translate(xpos,ypos,0);  
 rotateZ(radians(angle)*-1);
 box(20,20,20);  
 popMatrix();  
 
 noFill();  
 ellipse(0,0,200,200);  
}

Re: 3D Space simulator
Reply #4 - Feb 16th, 2006, 11:40am
 
Ok So ive tried again using a different approach but its till does not work

now when i wish to turn the cube a translate out say 100 start the rotation, translate back and then draw the box and it moves around in a circle the size of the circle the cube follows depends on the speed of the box

below is the code ive used. its still not fully working however

any ideas on how to fix it?

import processing.opengl.*;
float X = 0;
float XM = 0;
float Y = 0;
float YM = 0;
float YRot = 0;
float ZRot = 0;
float Z = 0;
float ZM = 0;
float XL = 0;
float YL = 0;
float TY = 0;
float XX = 0;
float YY = 0;
void setup()
{
 size(1280, 1024, OPENGL);
 noStroke();
 framerate(30);
}

void draw()
{
 scale(.5);
 background(0);
 lights();
 translate(width,height);
 fill(255,255,0);
 sphere(30);
 fill(255,0,255);
 
 if(keyPressed)
 {
     movement();
 }
 else
 {
 
 
   translate(-XX,YY,Z);
   rotateZ(ZRot);
   fill(255);
   sphere(10);
   translate(XX,YY,Z);
   
    Y = Y+YM;
   translate(X,Y,Z);
 }
 box(80);
}

void movement()
{
 if(key == '-')
 {
   if(YM < 8)
   {
     YM = YM+1;
     
   }
   translate(-XX,YY,Z);
   rotateZ(ZRot);
   fill(255);
   sphere(10);
   translate(XX,YY,Z);
   
    Y = Y+YM;
   translate(X,Y,Z);
 }
 else if(key == '=')
 {
   if(YM > -8)
   {
     YM = YM-1;
     
   }
   translate(-XX,YY,Z);
   rotateZ(ZRot);
   fill(255);
   sphere(10);
   translate(XX,YY,Z);
   
    Y = Y+YM;
   translate(X,Y,Z);
 }
 else if(key == 's')
 {
   YM = 0;
   translate(-XX,YY,Z);
   rotateZ(ZRot);
   fill(255);
   sphere(10);
   translate(XX,YY,Z);
   
    Y = Y+YM;
   translate(X,Y,Z);
 }
 else if(keyCode == LEFT)
 {
    turn(-0.01, 100);
 }
 else if(keyCode == RIGHT)
 {
   turn(+0.01,-100);
 }
}
void turn(float turnDifer, float turnDir)
{  
 
 translate(turnDir*YM,Y,Z);
 rotateZ(ZRot);
 fill(255);
 sphere(10);
 ZRot = ZRot+ turnDifer;
 translate(-turnDir*YM,YY,Z);
 XX = -turnDir*YM;
 
}

void keyReleased()
{
 if(key == 'q')
 {
 print("q");
 }
 else if(keyCode == RIGHT)
 {
  print("LEFT");
 
  Y =0;
 }
 else if(keyCode == LEFT)
 {
  print("RIGHT");
 Y =0;
 }
}
Re: 3D Space simulator
Reply #5 - Mar 2nd, 2006, 6:53pm
 
I mentioned above im trying to develop a 3d space sim. i have done a decent amount of work trying to get this program functioning correctly. i have now encoutered a problem and was wondering if anybody here could possibly help me out

i have the Ship (cube in this case as the ship requires many Shapes) moving around the screen.The + and - keys make the box move forward and backward and the UP, DOWN, LEFT and RIGHT keys control its direction. the problem is that the cube will only move correctly once its aligned (parallel) to one of the axis;

EG when the program starts you can accelerate and then push the up and down keys and its responds as it should but as soon as you push left or right it goes all over the place.

this also happens if you (at the start of the program) push left or right after accelerating. but the once you push Up or down it goes out of control

Please help because there is no way im gonna be able to figure this out on my own.

Thanks again Graham.

I have included the code below.

import processing.opengl.*;

float godY;
float godX;
float godZ;
float godXrot;
float godYrot;
float godZrot;
float speed;
float angleX;
float angleY;
float angleZ;
int planeMovement = 0;
float xpos,ypos,zpos;

Vector[] corners;

void setup()
{
 size(1280,1024,OPENGL);
 godX = 0;
 godY = 0;
 godZ = 0;
 //----------
 godXrot = 0;
 godYrot = 0;
 godZrot = 0;
 //----------
 speed = 0.1;
 //----------
 xpos = 0;
 ypos = 0;
 zpos = 0;
 //----------
 angleZ = 180;
 angleY = 0;
 angleX = 0;
 speed = 0.0;
 //----------
}

void draw()
{
 scale(.5);
 stroke(0);
 lights();
 background(0);
 translate(width,height,0);
 POR();
 pushMatrix();
 noStroke();
 translate(500,300,-200);
 fill(255,0,255);
 sphere(40);
 translate(-1100, -500, 300);
 fill(255,255,0);
 sphere(60);
 popMatrix();
 fill(0);
 
 if(planeMovement == 0)
 {
    moveX();
 }
 else
 {
    moveZ();
 }
 movement();
 translate(godX,godY,godZ);
 rotate(-angleZ/57.5);
 rotateX(-angleX/57.5);
 fill(255,24,78);
 box(60);
}

void movement()
{
   godX = godX + xpos;
   godY = godY + ypos;
   godZ = godZ + zpos;
}
void moveZ()
{
   xpos = (sin(radians(angleZ))*speed)*-1;
   ypos = (cos(radians(angleZ))*speed)*-1;
}
void moveX()
{  
   ypos = cos(radians(angleX))*speed;
   zpos = sin(radians(angleX))*speed;
}

void POR()
{
   stroke(255,0,0);
   line(-200, 0, 0, 200, 0, 0); //X is red
   stroke(0,255,0);
   line(0, -200, 0, 0, 200, 0); //Y is green
   stroke(0,0,255);
   line(0, 0, -200, 0, 0, 200); //Z is Blue
}

void keyPressed()
{
   if(keyCode == DELETE)
   {
     //angleY = angleY - .2;
   }
   else if(keyCode == LEFT)
   {
     planeMovement = 1;
     angleZ = angleZ + 1;    
   }
   else if(keyCode == RIGHT)
   {
     planeMovement = 1;
     angleZ = angleZ - 1;      
   }
   else if(keyCode == UP)
   {
     planeMovement = 0;
     angleX = angleX + 1;  
   }
   else if(keyCode == DOWN)
   {
     planeMovement = 0;
     angleX = angleX - 1;    
   }
   else if(key == '-')
   {
         speed = speed + 0.1;
   }
   else if(key == '=')
   {
         speed = speed - 0.1;
   }  
}
Page Index Toggle Pages: 1