Using PGraphics to mix p2d and p3d not going as expected

This worked fine in java mode but not pjs. Unfortunately the target platform I am aiming for is PJS It uses PGraphics to display the 3d image and I plan to create a hud that is 2d so I want it to run in the PGraphics buffer buffer.

> World world;
> Player player;
> int enemy_i = 6;
> Enemy enemy[] = new Enemy[enemy_i + 1];
> PGraphics pg;
> 
> void setup()  {
> 
>   world = new World();
>   player = new Player(-50,0,0);
> 
>   for(int i = 0; i <= enemy_i; i++) {
> 
>     float d = random(300,1000);
>     int rx = floor(random(4))*1500;
>     int ry = floor(random(4))*1500;
>     enemy[i] = new Enemy(rx,ry,d);
>   }
>   size(500,500);
>   pg = createGraphics(500, 400,P3D);
> }
> 
> void draw()  {
>   pg.beginDraw();
>   pg.background(0,255,0);
>   world.handleSky();
> 
>   player.posCam();
>   world.drawWorld();
> 
>   for(int i = 0; i <= enemy_i; i++) {
>     
>     enemy[i].drawCharacter();
>   }
>   pg.endDraw();
>   
>   image(pg, 0, 0);
> }
> 
> class World  {
>   
>   float R;
>   float G;
>   float B;
> 
>   public float px;
>   public float py;
> 
>   int wDIM = 9;
> 
>   public int city[][] = new int[wDIM+1][wDIM+1];
> 
>   World()  {
>     
>     px = 0;
>     py = 0;
> 
>     R = 0;
>     G = 96;
>     B = 192;
> 
>     for(int _w_ = 0; _w_ <= wDIM; _w_++) {
>       
>       for(int _h_ = 0; _h_ <= wDIM; _h_++) {
>         
>         float R = floor(random(13));
>         
>         if(R == 0)
>           city[_w_][_h_] = 2;
>         else if(R == 1)
>           city[_w_][_h_] = 3;
>         else
>           city[_w_][_h_] = 0;
>       }
>     }
>     
>     for(int _h_ = 0; _h_ <= wDIM; _h_+=3)  {
>     
>       for(int i = 0; i <= wDIM; i++) {
>         
>         city[i][_h_]  = 1;
>       }
>     }
>     
>     for(int _w_ = 0; _w_ <= wDIM; _w_+=3)  {
>       
>       for(int i = 0; i <= wDIM; i++)  {
>         
>         city[_w_][i] = 1;
>       }
>     } 
>   }
>   
>   public void handleSky() {
> 
>     R++;
>     G++;
>     B++;
> 
>     pg.colorMode(HSB);
>     pg.background(R%255,255,255);
>     
> 
>     pg.pushMatrix();
>       pg.ambientLight((((R+B+G)/3) % 255),64,128+(2*(16383/R)));
>     pg.popMatrix();
> 
>     pg.colorMode(RGB);
>   }
> 
>   private void drawConcrete(float X,float Y)  {
>     
>     pg.pushMatrix();
>       pg.translate(0,X,Y);
>       pg.fill(128);
>       pg.box(1,500,500);
>     pg.popMatrix();
>   }
> 
>   private void drawBuilding(float X, float Y)  {
>     
>     pg.pushMatrix();
>       pg.translate(-5,X,Y);
>       pg.fill(200);
>       pg.box(10,500,500);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-250,X,Y);
>       pg.fill(128);
>       pg.box(500,350,350);
>     pg.popMatrix();
>   }
>   
>   private void drawGrass(float X, float Y)  {
>     
>     pg.pushMatrix();
>       pg.translate(-5,X,Y);
>       pg.fill(200);
>       pg.box(10,500,500);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-5,X,Y);
>       pg.fill(0,255,0);
>       pg.box(15,350,350);
>     pg.popMatrix();
>   }
> 
>   private void drawGarden(float X, float Y)  {
>     
>     pg.pushMatrix();
>       pg.translate(-5,X,Y);
>       pg.fill(200);
>       pg.box(10,500,500);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-50,X-150,Y);
>       pg.fill(0,255,0);
>       pg.box(100,100,250);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-30,X+100,Y);
>       pg.fill(200);
>       pg.box(60,100,250);
>       pg.fill(200,100,100);
>       pg.box(70,80,230);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-60,X+100,Y-75);
>       pg.fill(0,200,0);
>       pg.box(60,50,50);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-60,X+100,Y+75);
>       pg.fill(100,50,50);
>       pg.box(60,10,10);
>     pg.popMatrix();
> 
>     pg.pushMatrix();
>       pg.translate(-100,X+100,Y+75);
>       pg.fill(255,0,0);
>       pg.box(30);
>     pg.popMatrix();
>   }
>   
>   public void drawWorld() {
> 
>     for(int _w_ = 0; _w_ <= wDIM; _w_++) {
>       
>       for(int _h_ = 0; _h_ <= wDIM; _h_++) {
> 
>         switch(city[_w_][_h_])  {
> 
>           case 0:
>             drawBuilding(_w_*500,_h_*500);
> 
>             if((player.x<(_w_*500)+200) &&
>                (player.x>(_w_*500)-200) &&
>                (player.y<(_h_*500)+200) &&
>                (player.y>(_h_*500)-200)
>                )  {
> 
>               player.x = px;
>               player.y = py;
>             }
> 
>             for(int i = 0; i <= enemy_i; i++) {
> 
>               if((enemy[i].x<(_w_*500)+200) &&
>                (enemy[i].x>(_w_*500)-200) &&
>                (enemy[i].y<(_h_*500)+200) &&
>                (enemy[i].y>(_h_*500)-200)
>                )  {
> 
>                 enemy[i].x = enemy[i].px;
>                 enemy[i].y = enemy[i].py;
>               }
>             }
>           break;
> 
>           case 1:
>             drawConcrete(_w_*500,_h_*500);
>           break;
>           
>           case 2:
>             drawGrass(_w_*500,_h_*500);
>           break;
> 
>           case 3:
>             drawGarden(_w_*500,_h_*500);
> 
>             if((player.x<(_w_*500)+150) &&
>                (player.x>(_w_*500)-150) &&
>                (player.y<(_h_*500)+150) &&
>                (player.y>(_h_*500)-200)
>                )  {
> 
>               player.x = px;
>               player.y = py;
>             }
> 
> 
>             for(int i = 0; i <= enemy_i; i++) {
> 
>               if((enemy[i].x<(_w_*500)+200) &&
>                (enemy[i].x>(_w_*500)-200) &&
>                (enemy[i].y<(_h_*500)+200) &&
>                (enemy[i].y>(_h_*500)-200)
>                )  {
> 
>                 enemy[i].x = enemy[i].px;
>                 enemy[i].y = enemy[i].py;
>               }
>             }
>           break;
>         }
>       }
>     }
>   }
> }
> 
> class Player  {
>   
>   public float x;
>   public float y;
>   public float z;
>   
>   private float dir;
>   
>   private boolean isLeft;
>   private boolean isRight;
>   private boolean isUp;
>   private boolean isDown;
>   private int b_count;
>   private final int tb_count = 20;
>   
>   private Bullet bullet[] = new Bullet[tb_count + 1];
> 
>   Player(float Z, float X, float Y)  {
>     
>     x = X;
>     y = Y;
>     z = Z;
>     
>     dir = 0;
>     b_count = 0;
> 
>     for(int i = 0; i <= tb_count; i++)
>       bullet[i] = new Bullet();
>   }
> 
>   public void checkShoot()  {
> 
>     if(key == RETURN || key == ENTER) {
> 
>       b_count++;
>       if(b_count > tb_count)
>         b_count = 0;
>       bullet[b_count].shoot(x,y,dir);
>     }
>   }
> 
>   private void drawPlayer()  {
> 
>     for(int i = 0; i <= tb_count; i++)
>       bullet[i].draw_self();
> 
>     pg.pushMatrix();
>       pg.translate(-25,x,y);
>       pg.rotateX(radians(dir));
>       pg.fill(0,0,100);
>       pg.box(50,10,30);
>     pg.popMatrix();
>     
>     pg.pushMatrix();
>       pg.translate(-65,x,y);
>       pg.rotateX(radians(dir));
>       pg.fill(100);
>       pg.box(30,10,30);
>     pg.popMatrix();
>     
>     pg.pushMatrix();
>       pg.translate(-85,x,y);
>       pg.rotateX(radians(dir));
>       pg.fill(200,150,0);
>       pg.box(20);
>     pg.popMatrix();
>   }
>   
>   public boolean setMove(int k, boolean b) {
>     
>     switch (k) {
>       
>       case 'w':
>       case UP:
>         return isUp = b;
>    
>       case 's':
>       case DOWN:
>         return isDown = b;
>    
>       case 'a':
>       case LEFT:
>         return isLeft = b;
>    
>       case 'd':
>       case RIGHT:
>         return isRight = b;
>       default:
>         return b;
>     }
>   }
>   
>   public void posCam()  {
>     
>     if(isRight)  {
>       
>       dir--;
>     }
>     
>     if(isLeft)  {
>       
>       dir++;
>     }
>     
>     world.px = x;
>     world.py = y;
> 
>     if(isUp)  {
>       
>       world.px = x;
>       world.py = y;
>       x += cos(radians(dir))*5;
>       y += sin(radians(dir))*5;
>     }
>     
>     if(isDown)  {
>       
>       world.px = x;
>       world.py = y;
>       x -= cos(radians(dir))*5;
>       y -= sin(radians(dir))*5;
>     }
>     
>     pg.camera(-150, x - cos(radians(dir))*200, y - sin(radians(dir))*200,z,x,y,1,0,0);
>     drawPlayer();
>   }
> }
> 
> class Enemy {
> 
>   private float dist_f;
>   public float x;
>   public float y;
> 
>   public float px;
>   public float py;
> 
>   float cd;
>   float dir;
> 
>   private int f_color;
>   private int p_color;
>   private int s_color;
> 
>   Enemy(float X,float Y, float D) {
> 
>     dist_f = D;
>     x = X;
>     y = Y;  
>     cd = dist(x,y,player.x,player.y);
>     dir = 0;
> 
>     f_color = (int)floor(random(2));
>     s_color = (int)floor(random(4));
>     p_color = (int)floor(random(2));
>   }
> 
>   public void drawCharacter() {
> 
>     pg.pushMatrix();
>       pg.translate(-25,x,y);
>       pg.rotateX(radians(dir));
> 
>       switch(p_color)  {
> 
>         case 0:
>           pg.fill(0,0,100);
>         break;
> 
>         case 1:
>           pg.fill(100);
>         break;
>       }
>       pg.box(50,10,30);
>     pg.popMatrix();
>     
>     pg.pushMatrix();
>       pg.translate(-65,x,y);
>       pg.rotateX(radians(dir));
> 
>       switch(s_color) {
> 
>         case 0:
>           pg.fill(100);
>         break;
> 
>         case 1:
>           pg.fill(0,128,0);
>         break;
> 
>         case 2:
>           pg.fill(128,128,0);
>         break;
> 
>         case 3:
>           pg.fill(128,0,0);
>         break;
> 
>         case 4:
>           pg.fill(255);
>         break;
>       }
>       pg.box(30,10,30);
>     pg.popMatrix();
>     
>     pg.pushMatrix();
>       pg.translate(-85,x,y);
>       pg.rotateX(radians(dir));
> 
>       switch(f_color)  {
> 
>         case 0:
>           pg.fill(200,150,0);
>         break;
> 
>         case 1:
>           pg.fill(128,64,0);
>         break;
>       }
>       pg.box(20);
>     pg.popMatrix();
> 
>     dir = 180+degrees(atan2(y-player.y, x - player.x));
>     moveTowards();
>   }
> 
>   private void moveTowards() {
> 
>     cd = dist(x,y,player.x,player.y);
> 
>     if(cd > dist_f && cd < dist_f*2) {
> 
>       x += cos(radians(dir))*3;
>       y += sin(radians(dir))*3;
>     }
>   }
> }
> 
> class Bullet  {
> 
>   public float x;
>   public float y;
>   private float dir;
>   private boolean enable;
> 
>   Bullet()  {
> 
>     x = 0;
>     y = 0;
>     dir = 0;
>     enable = true;
>   }
> 
>   public void shoot(float X, float Y, float DIR) {
> 
>     x = X;
>     y = Y;
>     dir = DIR;
>     enable = true;
>   }
> 
>   public void draw_self()  {
> 
>     if(enable)  {
> 
>       x += cos(radians(dir))*15;
>       y += sin(radians(dir))*15;
> 
>       pg.pushMatrix();
>         pg.translate(-50,x,y);
>         pg.fill(255,255,0);
>         pg.box(5);
>       pg.popMatrix();
>     }
>   }
> }
> 
> void keyPressed()  {
>   
>   player.setMove(keyCode, true);
>   player.checkShoot();
> }
> 
> void keyReleased()  {
>   
>   player.setMove(keyCode, false);
> }

Answers

  • Can you narrow that down to an MCVE instead of your entire sketch? What exactly do you mean when you say it doesn't work?

  • edited March 2015 Answer ✓
    • Your posted code is prefixed w/ > in all of its lines! Please correct that!
    • You state that it works fine in "Java Mode". But only Processing 1 can run your code the way it is!
    • B/c in Processing 2+, we can't have OpenGL PGraphics while using a non-OpenGL main canvas!
    • Also why you place size() at the end of setup()? That is a source of bugs when using OpenGL renderers!
  • edited March 2015

    Some extra tips:

    • Since that P3D PGraphics got almost same dimensions as the main canvas, only the bottom isn't part of it, wouldn't make more sense to directly draw to the 3D main canvas instead?
    • If you intend of using the bottom part for something else, make a separate 2D PGraphics for it.
    • And before I forget: Cool sketch there! ;)
Sign In or Register to comment.