Creating a small 3D model (House)

Hello.
Can someone help me with a small 3d model?
I want to make a small “area” with only a street, a basic house and a street lamp.
I tried to create the basic template, but for a reason all the objects gets pushed in a weird way and I cant manage to place one object on top of other, for example the base of the streetlight and the street.

1 Like

Can you please post your entire code so we can work from there

2 Likes

That model is perfect, thank you so much

1 Like

Model a bit simplified.

  • No class, no plates, no ArrayList

  • Mainly working with boxes, called by the function boxParams

  • cam flies around the scene on a circle; use mouseY for cam height (mouse up and down)


// Simple scene 

// floor Y 
final int GROUND_LEVEL_Y = 480;

// cam stuff: cam flies around the scene on a circle; mouseY for cam height 
float camAngle  = 0;  // rotation angle in degree 

// ---------------------------------------------------------------------
// 2 core functions 

void setup() {
  size(1600, 900, P3D);
  background(20, 22, 170);
  avoidClipping();
} // func 

void draw() {
  background(20, 22, 170);
  lights();

  // camera();
  cameraManagement();

  // house and ground and street
  drawHouse();

  // draw Lamp Posts
  for (int i=0; i < 9; i++) {
    drawLampPost(600-i*150, 386);
  }//for
} // func

// ---------------------------------------------------------------------
// other main functions

void drawHouse() {
  // House 

  // Sphere at the house
  fill(250, 0, 0);//red 
  sphereParams (0, 600/2, 120, 
    140);

  // main house: a box 
  fill(210);//gray 
  stroke(40);
  boxParams (0, 600/2, 0, 
    360, 360, 360);

  // ground 
  fill(0, 255, 0); // green 
  boxParams (0, GROUND_LEVEL_Y, 0, 
    1360, 2, 1360);

  // A way 
  fill(144); // dark 
  boxParams (0, GROUND_LEVEL_Y-1, 320, 
    1360, 2, 60);
  //
} // 

void drawLampPost(float postX_, float postY_) {
  // Lamp Post

  // data
  final float postHeight=140;
  final float postDiam=12; 

  final float postX=postX_; 
  final float postY=GROUND_LEVEL_Y-postHeight/2; 
  final float postZ=postY_; 

  // The post is a box (with parameters)
  fill(217);
  stroke(33);
  boxParams(postX, postY, postZ, // the position of the box  
    postDiam, postHeight, postDiam); // the dimensions of the box 

  // The lamp on top 
  fill( #FCFF4D ); // yellow  
  sphereParams (postX, postY-postHeight/2, postZ, 
    postDiam*2.4);
}

void cameraManagement() {
  // make a camera that rotates 

  final float camRadius = 800;         // e.g. 700 radius (dist to scene)
  float camX=0, camY=0, camZ=0;  // its position 

  // cam pos (eye) 
  camX= camRadius * cos ( radians(camAngle) ) +0;
  camY= map(mouseY, 0, height, -440, GROUND_LEVEL_Y-12); // OR:   GROUND_LEVEL_Y-240;
  camZ= camRadius * sin ( radians(camAngle) ) +0;

  // set cam
  camera(camX, camY, camZ, // cam pos (eye) 
    0.0, GROUND_LEVEL_Y-200, 0.0, // look at 
    0.0, 1.0, 0.0);        // angles are standard 

  // camera can be stopped. 
  if (!keyPressed) {
    // rotate
    camAngle+=0.6;
    if (camAngle>360) 
      camAngle=0;
  } // if
}

// ---------------------------------------------
// 3 helper functions

void boxParams(float x, float y, float z, 
  float sizeX, float sizeY, float sizeZ) {
  // a box with parameters (both colors (stroke and fill) must be set before calling the function)
  pushMatrix();
  translate(x, y, z);
  box(sizeX, sizeY, sizeZ);
  popMatrix();
}

void sphereParams(float x, float y, float z, 
  float sizeSphere) {
  // a sphere with parameters (the fill color must be set before calling the function; noStroke() is used in the function)
  pushMatrix();
  noStroke(); 
  translate(x, y, z);
  sphere(sizeSphere);
  popMatrix();
}

void avoidClipping() {
  // avoid clipping (at camera): 
  // https : // 
  // forum.processing.org/two/discussion/4128/quick-q-how-close-is-too-close-why-when-do-3d-objects-disappear
  perspective(PI/3.0, (float) width/height, 1, 1000000);
}//func 
//

1 Like

by the way:

in 3D mode (using P3D), the point 0,0,0 is in the upper left corner
(like in 2D) but the standard camera looks to a point right from there:

  • to width/2.0, height/2.0 and a Z-Position

You can see this here:

void setup() {
  size(1600, 900, P3D);
  background(20, 22, 254);
} // func 

void draw() {
  background(20, 22, 254);
  lights(); 

  sphereParams(0, 0, 0, 
    140);
}

void sphereParams(float x, float y, float z, 
  float sizeSphere) {
  // a sphere with parameters (the fill color must be set before calling the function; noStroke() is used in the function)
  pushMatrix();
  noStroke(); 
  translate(x, y, z);
  sphere(sizeSphere);
  popMatrix();
}
//

The sphere is in the upper left corner.

The standard camera has these default values :

camera(width/2.0, height/2.0, (height/2.0) / tan(PI*30.0 / 180.0),  // pos
width/2.0, height/2.0, 0,     // look at
0, 1, 0);                              // angle 

Therefore in the “House Sketch” the cam is looking at
0, GROUND_LEVEL_Y-200, 0 which is the center of the scene.

Remark

Remember that in a 3D Sketch,

  • x is normal (left/right position)
  • but y is the virtual height above the scene and
  • Z is into the screen (away from you) (forward / backward position)

So to place something on the scene / stage we mainly change x and z (!), not so much y.

The idea of 3D space

The idea of 3D space (with size(1600, 900, P3D);) is that you draw not on a canvas but inside a box / room / stage where you can place stuff further away (away from you, into the screen depth, Z-value) or nearer to you.

Imagine the room as the space on a table where you place your scene:

The idea is that

  • the first parameter is x (you move something left and right on the table surface e.g.),
  • the 2nd parameter (y) is the height (you move something up and down above the table) and
  • the 3rd parameter (z) is that you move something back and forth on the table surface (depth).

The camera

The camera has 9 parameters: 3 for position, 3 for lookAt, 3 for UP vector.

Houses

See also houses Some Movies I made - #23 by Chrisir

Warm regards,

Chrisir

1 Like

Hello @a_random_cat,

There is a tutorial here:
https://processing.org/tutorials/p3d

Scrutinize the first example in the 3D shapes section and look up all the references.

The above is a static sketch (static mode)… you can create an interactive sketch (active mode) with setup() and draw().

References:

Have fun!

:)