Trouble with P3D box edges clipping

Hello everyone! New to the forum, all help is appreciated!

I’m working on a P3D project and am drawing boxes with a stroke on the edges. The issue that I noticed is that when I move around them (I’m using queasycam) the stroke and the fill of the shapes clip around one another. Is there anything that I can do to prevent this?

Thanks again!

Is this happening always, or just when you bump into them?

Without seeing the collision code it’s hard to say, but there’s two solutions that come to mind, add to the collision distance check, or reduce the stroke weight.

Thank you for your response!

The clipping of the edges (what makes them look all jagged) is something that constantly happens, it’s just that when I move around it, the clipping is much more noticeable—honestly it’s easier to see on video.

And sorry I don’t actually know what the collision distance check is, I couldn’t really find anything about it on the forums

I may end up reducing the strokeWeight, but if possible I’d prefer to keep it as is

You might want to use lights(); at start of draw()

Also, try noSmooth();

There is another kind of clipping at the camera. But this is not the topic here

Thanks @Chrisir!
Unfortunately, not much changed besides the darkened sides of the cubes; I’m not really sure what else to do

or say noStroke(); so get rid of the lines completely.

Do you mind to share your code? Or the part where you display a box?

Also, try noSmooth();

Yeah I might just end up reducing the stroke weight, that seems like the solution everyone is pointing towards

This code is part of a bigger project that I’m working on (I’m kind of new and wanted to challenge myself and make some sort of voxel game similar to Minecraft), so I can’t share the whole thing—but I’ve rewritten the code of this part into its own sketch (make sure you have QueasyCam installed):

//Queasycam
import queasycam.*;
QueasyCam cam;

//rendering settings
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
float nearClippingDistance = 0.01; // default is cameraZ/10.0

ArrayList<PVector> blocks = new ArrayList<PVector>();

void setup() {
  size(1920, 1080, P3D);
  fullScreen();
  
  colorMode(HSB, 360, 100, 100, 255);

  //setup boxes
  for (int x = 0; x < 6; x++) {
    for (int y = 0; y < 6; y++) {
      for (int z = 0; z < 6; z++) {
        if (random(1) > 0.5) blocks.add(new PVector(x, y, z));
      }
    }
  }

  //cam
  cam = new QueasyCam(this);
  cam.speed = 5;
  cam.sensitivity = 1;

  noCursor();
}

void draw() {
  background(0, 0, 100);

  //rendering settings
  lights();
  noSmooth();
  perspective(fov, float(width)/float(height), nearClippingDistance, cameraZ*1000.0); //camera perspective
  
  pushMatrix();
  
  float blockSize = 100;
  scale(blockSize);
  for (PVector p : blocks) {
    pushMatrix();
    translate(p.x, p.y, p.z);

    //fill & stroke
    fill(100, 80, 100);
    stroke(100, 80, 0.4*100);
    strokeWeight(0.1);

    box(0.9);

    popMatrix();
  }
  
  popMatrix();
}

Hello,

You are on your way!

Hints:

  • for (int x = -100; x < 100; x+=20) //spaces boxes every 20
  • translate(width/2, height/2); //Puts origin at center of screen
  • Use a box size to fit in spacing
  • Don’t use scale() for now; this always creates problems for the uninitiated.

I removed the camera and did my own rotating so you may have to modify above.

:)

@glv Thank you! :smiley:
This is actually my second attempt at making Minecraft! The main aim of this one is to create cleaner/readable and more expandable code (as well as infinite worlds, better world gen, menus, saving worlds, etc).
My first had code that was practically unreadable (except for me…). However, it did produce some cool terrain!

I won’t post the source for the old one because it’s so bad lol

3 Likes

Your screenshots look beautiful!

Anyway, your initial problem was probably caused by scale.

Here is my version without scale. I just mult the PVectors in setup()


//Queasycam
import queasycam.*;

QueasyCam cam;

//rendering settings
float fov = PI/3.0;
float cameraZ = (height/2.0) / tan(fov/2.0);
float nearClippingDistance = 0.01; // default is cameraZ/10.0

ArrayList<PVector> blocks = new ArrayList<PVector>();

void setup() {

  size(1920, 1080, P3D);
  //  fullScreen();

  colorMode(HSB, 360, 100, 100, 255);
  //setup boxes
  for (int x = 0; x < 6; x++) {
    for (int y = 0; y < 6; y++) {
      for (int z = 0; z < 6; z++) {
        if (random(1) > 0.5) 
          blocks.add(new PVector(x*21, y*21, z*21));
      }
    }
  }

  //cam
  cam = new QueasyCam(this);
  cam.speed = 5;
  cam.sensitivity = 1;
  //  noCursor();
}

void draw() {
  background(0, 0, 100);

  //rendering settings
  lights();
  noSmooth();
  perspective(fov, float(width)/float(height), nearClippingDistance, cameraZ*1000.0); //camera perspective

  pushMatrix();
  translate(521, 21, -300);

  for (PVector p : blocks) {
    myBox(p);
  }
  popMatrix();
}

//-----------------------------------------------------------

void myBox(PVector p) {
  pushMatrix();

  translate( p.x, p.y, p.z );

  //fill & stroke
  fill(100, 80, 100);
  stroke(100, 80, 0.4*100);
  strokeWeight(3);

  box(20);

  popMatrix();
}
//
1 Like

Oh that’s strange, I don’t really know what scale would change to make the edges not clip. I think this is the approach that I will follow, thanks everyone!

Also it looks like pasteboard is down right now, so that’s why the link in the first post is broken

scale() is not necessary in your sketch you can just place everything in the right size

When you insist on scale()

scale() with lines seems problematic; maybe use noStroke() before box() so the lines are not drawn in the first place

If I continue to use the strokes on the edges of blocks, I probably won’t use scale.

However, scaling was pretty useful because I could have a coordinate system very similar to the real Minecraft (I did scale(blockSize, -blockSize, blockSize); so that I could reverse y, and have every block be one unit ).

So if I end up going back to using scale, I may just use textures instead of the fill/stroke design that I’m using right now so I don’t have to deal with the clipping.
Although, I’m not so sure how the performance will be to loading a tileset, then drawing a texture for each block every frame—I’ll just have to see how it turns out, I suppose.

1 Like

This can be achieved without scale() - see my code