grahambo2005
YaBB Newbies
Offline
Posts: 9
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; } }