1

I'm a beginner in JavaFx 3d modelling. I'm trying to create a 3d-model of boxes in a room. I have the dimensions of the boxes and the coords from the front-left-bottom corner of every box. I've tried to set the coords with setTranslateX(), but the result isn't correct. Here is the pice of my code where I try to set the coords:

for (int i = 0; i < Main.load.size(); i++) {
        Load l=Main.load.get(i);
        Box sphere = new Box(l.getLength()*10, l.getWidth()*10, l.getHeight()*10);
        sphere.setTranslateX(l.getX()*10);
        sphere.setTranslateY(l.getY()*10);
        sphere.setTranslateZ(l.getZ()*10);
        PhongMaterial m = new PhongMaterial();
        m.setDiffuseColor(new Color(Math.random(),Math.random(),Math.random(),1));
        m.setSpecularColor(Color.BLACK);
        sphere.setMaterial(m);
        root.getChildren().add(sphere);
    }

I hope someone can help me.

Here is an example:

Sizes: blue (30,50,50) pink (10,10,20)

Position: blue (0,0,0) pink (30,0,0)

And this is what I get

holzfeli
  • 35
  • 7
  • Could you please describe the effect you are seeing? What version of Java are you using? Have you set your 3D subscene's depthBuffer to true via the constructor? – Birdasaur Feb 08 '16 at 16:39
  • 1
    @Birdasaur I updated the question with an example. I'm using Java 8. The depthBuffer is true. Thanks for your help! – holzfeli Feb 08 '16 at 16:56
  • It appears as though your objects are translated exactly where they should be assuming the camera you are using is a PerspectiveCamera. PerspectiveCamera uses a 3D origin (0,0,0) in the center of the 3D scene. Is that what you want or are your position values expecting one of the "corners" to be the origin? – Birdasaur Feb 08 '16 at 17:09
  • 1
    @Birdasaur It's true, that I'm using a PerspecitveCamera. I want to insert the boxes in one and the same coordinate system. The pink box should be attached to the blue one. Sorry for my bad explanation. – holzfeli Feb 08 '16 at 17:22
  • When you do a translation of a JavaFX 3D object like a Box you need to account half the width of the object along any axis. The default placement for a Box is to be centered at the origin meaning the center of the Box object is at 0,0,0. Your width is 30 * 10 but your translateX converts to 0*10=0. So the right most edge of your blue box will be X=150 (300 / 2.0 = 150). Your Pink Box has a translateX of 10*30=300. The center of the pink box will be translated to 300 which means the left most edge will be at 300 - (width/2.0) = 300 - (50) = 250. – Birdasaur Feb 08 '16 at 18:49
  • @Birdasaur Thank you! You have solved my problem! – holzfeli Feb 08 '16 at 19:16
  • Awesome! I reposted the last comment as an "answer" so you can mark it as solved. – Birdasaur Feb 09 '16 at 03:27

1 Answers1

0

When you do a translation of a JavaFX 3D object like a Box you need to account half the width of the object along any axis. The default placement for a Box is to be centered at the origin meaning the center of the Box object is at 0,0,0. Your width is 30 * 10 but your translateX converts to 0*10=0. So the right most edge of your blue box will be X=150 (300 / 2.0 = 150). Your Pink Box has a translateX of 10*30=300. The center of the pink box will be translated to 300 which means the left most edge will be at 300 - (width/2.0) = 300 - (50) = 250.

Birdasaur
  • 736
  • 7
  • 10