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 Navigation in Game!
Page Index Toggle Pages: 1
3D Navigation in Game! (Read 634 times)
3D Navigation in Game!
Nov 2nd, 2006, 12:19am
 
Hi All,

Im starting a wolf3D type game ;)

BUT im trying to get my guy to move around.

I want to move the guy forward then rotate around his center to face a new direction and then to move forward that way.

please see my code...

//=====================================================-

int boxSize;
float _LR,_FB,_round;

void setup()
{
 size(500, 500, P3D);
 frameRate(30);

 _LR = 0.0;
 _FB = 0.0;
 _round = 0.0;  
 boxSize = 20;
}

void draw()
{background(200);

 camera(280.29, 197.79, 48.80, 256.60, 247.29, 0.0, 0.0, 1.0, 0.0);
 translate(width/2,height/2,0);

 fill(0,125,255);
 box(2,2,2);//Blue
     
 pushMatrix();
                               //rotateY(_round); //Rotate aroune the center of the world But move from the guy
     translate(_LR, 0, _FB);
                                 rotateY(_round); //Rotate aroune the guy But move from the center of the world
     fill(50);
     box(2,2,3);
     pushMatrix();
         translate(0,0,3);
         fill(255);
         box(2,2,3);
    popMatrix();
     
 popMatrix();

 walls();
}

void keyPressed()
{
 if (keyCode == LEFT)
 {  _LR +=0.3;  }
 
 else if (keyCode == RIGHT)
 {  _LR -=0.3;  }
 
 if (keyCode == DOWN)
 {  _FB -=0.3;  }
 
 else if (keyCode == UP)
 {  _FB +=0.3; }
 
 if(key == '1')
 {  _round +=0.1;  }
 
 else if(key == '2')
 {  _round -=0.1;  }
}
void walls()
{
 dispalyWalls();
 pushMatrix();
   translate(boxSize,0,0+boxSize);
   rotateY(-1.6);
   dispalyWalls();
 popMatrix();
}
void dispalyWalls()
{
 pushMatrix();
     fill(255,0,0);
    translate(0,0,-(boxSize/2));
     box(boxSize,boxSize,2);
   popMatrix();
   
   pushMatrix();
     fill(0,255,0);
     translate(-(boxSize/2),0,0);
     rotateY(1.6);
     box(boxSize,boxSize,2);
   popMatrix();
   
    pushMatrix();
     fill(0,0,255);
     translate((boxSize/2),0,0);
     rotateY(1.6);
     box(boxSize,boxSize,2);
   popMatrix();
}

//=================================================-

Thanks for any help.
Re: 3D Navigation in Game!
Reply #1 - Nov 2nd, 2006, 8:46pm
 
Ahh wolfenstein...  The way they made that game was really darn clever.  A technique called Raycasting.  Do a search to see how John Carmack designed it 'back in the day'.  But nowadays we can get the same results easier in 3D.

Since a wolfenstein style game is essentially a 2D
game with a 3D display, you only need to keep track of 3 things:

playerX, playerY, heading

Heading will be an angle measured in radians, where 2*PI radians=360 degrees.  X,Y are your position on the map.

Then when you press the "forward" button:

playerX+=cos(heading)*stepsize;//try various stepsize values
playerY+=sin(heading)*stepsize;

When you press the "back" button:

playerX-=cos(heading)*stepsize;
playerY-=sin(heading)*stepsize;

Rotate left: heading+=.1;
Rotate right: heading-=.1;

Try drawing everything as a 2D top-down (third person) view first.  Draw a little arrow to represent your player.  That will help you get it rolling because you'll be able to see exactly whats happening.

good luck
Re: 3D Navigation in Game!
Reply #2 - Nov 3rd, 2006, 12:08am
 
You might find it helpful to use the OCD camera library and simply treat the camera you create as the player's eyes. Seems like it'd be a bit less of a headache.
Re: 3D Navigation in Game!
Reply #3 - Nov 3rd, 2006, 12:27am
 
Huge thanks movax & ddf
I had looked into that camera But movax has the right thing i was looking for.

O.. movax, I had to switch around the +/- & cos/sin !

I’m hoping to now mount the cam on the front of the guy/box and do my collision detection on the box, that should keep the cam in the level & all that jazz ^_^

//====================================== New Code + Strafing

int boxSize;
float playerX,playerZ,heading,stepsize;

void setup()
{
 size(500, 500, P3D);
 frameRate(30);

 playerX = 0.0;
 playerZ = 0.0;
 heading = 1.6;  
 boxSize = 20;
 stepsize = 0.2;
}

void draw()
{background(200);

 camera(250.0, 197.79, -23.80, 250.0, 250.0, 0.0, 0.0, 1.0, 0.0);
 translate(width/2,height/2,0);
 fill(0,125,255);
 box(2,2,2);//Blue

 pushMatrix();
    translate(playerX, 0, playerZ);
    rotateY(heading-1.6);
    fill(50);
    box(2,2,3);
  pushMatrix();
    translate(0,0,2);
    fill(255);
    box(1,1,2);
  popMatrix();
popMatrix();

 walls();
}

void keyPressed()
{
 if (LEFT == keyCode || 81 == keyCode) // 81 = 'q';
 {  heading += .1;  }
 
 else if (RIGHT == keyCode || 69 == keyCode) // 69 = 'e';
 {  heading -= .1;   }
 
 if (DOWN == keyCode || 83 == keyCode) // 83 = 'S';
 {
    playerX += cos(heading)*stepsize;
    playerZ -= sin(heading)*stepsize;
 }
 
 else if (UP == keyCode || 87 == keyCode) // 87 = 'w';
 {
    playerX -= cos(heading)*stepsize;
    playerZ += sin(heading)*stepsize;
 }
 
 //sidestepping ~ Strafing
 if(65 == keyCode) // 65 = 'a';
 {
    playerZ += cos(heading)*stepsize;
    playerX += sin(heading)*stepsize;
 }
 else if(68 == keyCode) // 68 = 'd';
 {
    playerZ -= cos(heading)*stepsize;
    playerX -= sin(heading)*stepsize;
 }
 keyCode = '?'; //Reset the keyCode

}
void walls()
{
  //...ADD CODE FROM ABOVE
}
void dispalyWalls()
{
  //...ADD CODE FROM ABOVE
}
Page Index Toggle Pages: 1