Wierd issue with P3D and camera clipping?
in
Programming Questions
•
5 months ago
I'm developing a small sort of FPS thingy that I hope to evolve into an actual sort of game thing, and so far i have a cube rendered on screen which the player can collide with when moving around with WASD and looking with mouse, but when the player looks at the box while colliding with it; the camera clips strangely with the geometry. What could i do to fix it?
My code:
- import java.awt.AWTException;
- import java.awt.Robot;
- import java.awt.Point;
- import java.awt.PointerInfo;
- import java.awt.MouseInfo;
- Robot bot;
- PointerInfo pointer;
- Block blocks;
- Player player;
- Boolean keys[] = new Boolean[526];
- void setup(){
- size(640,480,P3D);
- noCursor();
- try {
- bot = new Robot();
- }
- catch (AWTException e) {
- e.printStackTrace();
- }
- blocks = new Block(32,32,-16,64,64,16);
- player = new Player(0,0,0);
- for(int i = 0; i< keys.length; i++){
- keys[i] = false;
- }
- noStroke();
- }
- void draw(){
- ////GAME LOGIC////
- float newx, newy;
- if(checkKey("w")){
- newx=player.walkSpeed*cos(radians(player.hlook));
- newy=player.walkSpeed*sin(radians(player.hlook));
- if(!collision(blocks.x1,blocks.x2,(player.x+newx)-8,(player.x+newx)+8,
- blocks.y1,blocks.y2,(player.y+newy)-8,(player.y+newy)+8,
- blocks.z1,blocks.z2,player.z-8,player.z+8)){
- player.x+=newx;
- player.y+=newy;
- }
- }
- if(checkKey("s")){
- newx=player.walkSpeed*cos(radians(player.hlook+180));
- newy=player.walkSpeed*sin(radians(player.hlook+180));
- if(!collision(blocks.x1,blocks.x2,(player.x+newx)-8,(player.x+newx)+8,
- blocks.y1,blocks.y2,(player.y+newy)-8,(player.y+newy)+8,
- blocks.z1,blocks.z2,player.z-8,player.z+8)){
- player.x+=newx;
- player.y+=newy;
- }
- }
- if(checkKey("a")){
- newx=player.walkSpeed*cos(radians(player.hlook+90));
- newy=player.walkSpeed*sin(radians(player.hlook+90));
- if(!collision(blocks.x1,blocks.x2,(player.x+newx)-8,(player.x+newx)+8,
- blocks.y1,blocks.y2,(player.y+newy)-8,(player.y+newy)+8,
- blocks.z1,blocks.z2,player.z-8,player.z+8)){
- player.x+=newx;
- player.y+=newy;
- }
- }
- if(checkKey("d")){
- newx=player.walkSpeed*cos(radians(player.hlook-90));
- newy=player.walkSpeed*sin(radians(player.hlook-90));
- if(!collision(blocks.x1,blocks.x2,(player.x+newx)-8,(player.x+newx)+8,
- blocks.y1,blocks.y2,(player.y+newy)-8,(player.y+newy)+8,
- blocks.z1,blocks.z2,player.z-8,player.z+8)){
- player.x+=newx;
- player.y+=newy;
- }
- }
- ////RENDERING////
- background(128);
- player.hlook -= (MouseInfo.getPointerInfo().getLocation().x)-(frame.getX()+(width/2));
- player.vlook += (MouseInfo.getPointerInfo().getLocation().y)-(frame.getY()+(height/2));
- player.vlook = constrain(player.vlook, -89, 89);
- bot.mouseMove(frame.getX()+(width/2),frame.getY()+(height/2));
- camera(player.x,player.y,player.z,player.x+(cos(radians(player.vlook))*cos(radians(player.hlook))),
- player.y+(cos(radians(player.vlook))*sin(radians(player.hlook))),
- player.z+(sin(radians(player.vlook))),0,0,1);
- ambientLight(102, 102, 102);
- pointLight(128,128,128,0,0,0);
- blocks.render();
- }
- boolean checkKey(String k){
- for(int i = 0; i < keys.length; i++){
- if(KeyEvent.getKeyText(i).toLowerCase().equals(k.toLowerCase())){
- return(keys[i]);
- }
- }
- return (false);
- }
- void keyPressed(){
- keys[keyCode] = true;
- }
- void keyReleased(){
- keys[keyCode] = false;
- }
- ///////////////
- //BLOCK CLASS//
- ///////////////
- class Block{
- float x1, y1, z1, x2, y2, z2;
- Block(float xx1, float yy1, float zz1, float xx2, float yy2, float zz2){
- x1 = xx1;
- y1 = yy1;
- z1 = zz1;
- x2 = xx2;
- y2 = yy2;
- z2 = zz2;
- }
- void render(){
- pushMatrix();
- translate(x1+((x2-x1)/2),y1+((y2-y1)/2),z1+((z2-z1)/2));
- box(x2-x1,y2-y1,z2-z1);
- popMatrix();
- }
- }
- ////////////////
- //PLAYER CLASS//
- ////////////////
- class Player{
- float x, y, z, walkSpeed, hlook, vlook;
- Player(float xx, float yy, float zz){
- x = xx;
- y = yy;
- z = zz;
- walkSpeed = 2;
- hlook = 0;
- vlook = 0;
- }
- }
- boolean collision(float xa1, float xa2, float xb1, float xb2){
- return((xa1>=xb1)&&(xa1<=xb2))||((xa2>=xb1)&&(xa2<=xb2))||
- ((xb1>=xa1)&&(xb1<=xa2))||((xb2>=xa1)&&(xb2<=xa2));
- }
- boolean collision(float xa1, float xa2, float xb1, float xb2,
- float ya1, float ya2, float yb1, float yb2){
- return(collision(xa1, xa2, xb1, xb2)&&collision(ya1, ya2, yb1, yb2));
- }
- boolean collision(float xa1, float xa2, float xb1, float xb2,
- float ya1, float ya2, float yb1, float yb2,
- float za1, float za2, float zb1, float zb2){
- return(collision(xa1, xa2, xb1, xb2)&&collision(ya1, ya2, yb1, yb2)&&collision(za1, za2, zb1, zb2));
- }
EDIT: I am using Processing 1.5; the latest version seems to be missing a function concerning keyboard keys so I'm using this for now
1