P3D collision

edited August 2017 in Library Questions

How would I make collision with boxes in P3D, using the Queasycam library?

Tagged:

Answers

  • elaborate.

  • I mean collision between you and the boxes, so that you hit the boxes instead of going through them. Here's my code:

    import queasycam.*;
    QueasyCam cam;
    float x, y, z;
    float s;
    void setup() {
      size(1280, 904, P3D);
      x = 100;
      y = 100;
      z = 0;
      s = 0.1;
      cam = new QueasyCam(this);
      cam.speed = 10;
      cam.sensitivity = 0;
    }
    void draw() {
      background(151, 212, 247);
      noStroke();
      lights();
      directionalLight(200, 200, 200, 0, 100, 0);
      translate(width/2, y-470+(width/2), z);
      rotateX(361.26);
      // World
    
      // Stone
      for (float i = -1000; i<1000; i+=100) {
        for (float j = -1000; j<1000; j+=100) {
          stoneBlock(i, -100, j);
        }
      }
      // Grass
      for (float i = -1000; i<1000; i+=100) {
        for (float j = -1000; j<200; j+=100) {
          grassBlock(i, 0, j);
        }
      }
      for (float i = -1000; i<1000; i+=100) {
        for (float j = 400; j<1000; j+=100) {
          snowBlock(i, 0, j);
        }
      }
      // Snow
      snowBlock(-1000, 100, 900);
      snowBlock(-1000, 200, 900);
      snowBlock(-1000, 100, 800);
      snowBlock(-900, 100, 900);
      // River
      for (float i = -1000; i<1000; i+=100) {
        waterBlock(i, 0, 300);
        waterBlock(i, 0, 200);
      }
      // Trees
      tree(-200, 0, 0);
      tree(500, 0, -700);
      zombie(0, 275, 0, 10);
      // Rotating
      if (mousePressed) {
        cam.sensitivity = 1.2;
      }
      else {
        cam.sensitivity = 0;
      }
    }
    void zombie(float mx, float my, float mz, float rote) {
      pushMatrix();
      translate(mx, my, mz);
      rotateY(rote);
      scale(0.73);
      // Head
      fill(101, 128, 78);
      box(50, 80, 100);
      pushMatrix();
      translate(0, 50);
      fill(80, 112, 51);
      box(50, 20, 100);
      pushMatrix();
      translate(-27, -45, 13);
      rotateY(55);
      fill(0, 0, 0);
      rect(0, 0, 23, 12);
    
      translate(-46, 0, 0);
      rect(0, 0, 23, 12);
    
      translate(23, -12, 0);
      fill(80, 112, 51);
      rect(0, 0, 23, 12);
      popMatrix();
      translate(50, -40);
      fill(80, 112, 51);
      box(50, 100, 100);
      popMatrix();
      // Body
      pushMatrix();
      translate(25, -90);
      fill(77, 161, 153);
      box(50, 100, 100);
      translate(0, -60, -37);
      box(50, 25, 25);
      popMatrix();
      // Legs
      pushMatrix();
      translate(25, -215, 13);
      fill(63, 79, 161);
      box(50, 150, 75);
      translate(0, -11, -50);
      box(50, 127, 25);
      popMatrix();
      // Feet
      pushMatrix();
      translate(25, -300, 0);
      fill(100, 100, 100);
      box(50, 20, 100);
      popMatrix();
      // Arms
      pushMatrix();
      translate(25, -65, 75);
      fill(77, 161, 153);
      box(50);
      translate(-75, 0, 0);
      fill(101, 128, 78);
      box(100, 50, 50);
      popMatrix();
      pushMatrix();
      translate(25, -65, -75);
      fill(77, 161, 153);
      box(50);
      translate(-75, 0, 0);
      fill(101, 128, 78);
      box(100, 50, 50);
      popMatrix();
      popMatrix();
    }
    // Tree
    void tree(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      woodBlock(0, 400, 0);
      woodBlock(0, 300, 0);
      woodBlock(0, 200, 0);
      woodBlock(0, 100, 0);
      woodBlock(0, 0, 0);
      for (float i = -200; i <= 200; i+=100) {
        for (float j = -200; j <= 200; j+=100) {
          leavesBlock(i, 400, j);
        }
      }
      for (float i = -100; i <= 100; i+=100) {
        for (float j = -100; j <= 100; j+=100) {
          leavesBlock(i, 500, j);
        }
      }
      leavesBlock(0, 600, 0);
      leavesBlock(-100, 600, 0);
      leavesBlock(100, 600, 0);
      leavesBlock(0, 600, 100);
      leavesBlock(0, 600, -100);
      popMatrix();
    }
    // Grass block
    void grassBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by-7, bz);
      fill(100, 63, 25);
      box(100, 85, 100);
      translate(0, 50);
      fill(70, 128, 23);
      box(100, 15, 100);
      popMatrix();
    }
    // Sand block
    void sandBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(194, 178, 128);
      box(100);
      popMatrix();
    }
    // Wood block
    void woodBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(199, 147, 69);
      box(80, 100, 80);
      pushMatrix();
      translate(0, 0, -45);
      fill(92, 52, 8);
      box(100, 100, 10);
      popMatrix();
      pushMatrix();
      translate(0, 0, 45);
      fill(92, 52, 8);
      box(100, 100, 10);
      popMatrix();
      pushMatrix();
      translate(-45, 0, 0);
      fill(92, 52, 8);
      box(10, 100, 100);
      popMatrix();
      pushMatrix();
      translate(45, 0, 0);
      fill(92, 52, 8);
      box(10, 100, 100);
      popMatrix();
      popMatrix();
    }
    // Stone block
    void stoneBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(130, 130, 130);
      box(100);
      popMatrix();
    }
    // Water block
    void waterBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(0, 100, 255, 50);
      box(100);
      popMatrix();
    }
    // Lava block
    void lavaBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(255, 75, 0, 200);
      box(100);
      popMatrix();
    }
    // Leaves block
    void leavesBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by, bz);
      fill(57, 107, 23);
      pushMatrix();
      translate(0, 0, -50);
      drawLeaf();
      popMatrix();
      pushMatrix();
      translate(0, -50, 7.5);
      rotateX(89.5);
      drawLeaf();
      popMatrix();
      pushMatrix();
      translate(0, 0, 50);
      drawLeaf();
      popMatrix();
      pushMatrix();
      translate(0, 50, 7.5);
      rotateX(89.5);
      drawLeaf();
      popMatrix();
      pushMatrix();
      translate(-50, 0, 7.5);
      rotateY(89.5);
      rotateZ(89.5);
      drawLeaf();
      popMatrix();
      pushMatrix();
      translate(50, 0, 7.5);
      rotateX(89.5);
      rotateY(89.5);
      drawLeaf();
      popMatrix();
      popMatrix();
    }
    // Snow block
    void snowBlock(float bx, float by, float bz) {
      pushMatrix();
      translate(bx, by-7, bz);
      fill(100, 63, 25);
      box(100, 85, 100);
      translate(0, 50);
      fill(245, 253, 255);
      box(100, 15, 100);
      popMatrix();
    }
    
    // Leaves
    void drawLeaf() {
      for (float i = x-100; i<100; i+=13) {
        for (float j = y-100; j<100; j+=13) {
          pushMatrix();
          translate(i-50, j-50);
          box(10, 7, 1);
          popMatrix();
        }
      }
    }
    
  • I'm trying to remember -- does the QueasyCam FPS example sketch already have collision detection in it?

Sign In or Register to comment.