Author Topic: object not translating as expected?  (Read 2467 times)

Offline tjm

  • byte
  • *
  • Posts: 17
    • View Profile
object not translating as expected?
« on: March 25, 2010, 02:46:39 pm »
Hi all.

Completely new to 3D here .... I used to be a middleware product engineer (BEA was probably my best known employer), but am really rusty after a few years as a stay-at-home-dad! And this 3D geometry has really got me messed up!! Most likely it's because of the perspective projection .... I just can't get out of the 2D x,y type of thinking.

I'm trying to move a group of objects along the Z axis. The camera view should be fixed so that the objects appear to move under the camera and out of the view. The goal is to have a group of objects with co-ordinated movement, all time synched to midi events .... think of a row of pineapples zig-zagging over a terrain while they 'bob' to a beat.

This is my 1st hacked-up experiment at translating objects, just to get the basics of movement figured out. With 1 box, the box appeared to move along the Z axis as expected. Then I changed the code to an array of boxes, and now the boxes appear to zoom off into space, not moving along the Z axis at all.  Can anyone tell me why?

Thanks.
--Tim.

Code: [Select]
import com.threed.jpct.*;

public class MyTest {

private static float PI = (float) Math.PI;

private World world;
private FrameBuffer buffer;
private Object3D [] box = new Object3D[3];
private Object3D plane = null;

int counter = 0;
boolean right = true;

public static void main(String[] args) throws Exception {
new MyTest().loop();
}

public MyTest() throws Exception {
world = new World();
world.setAmbientLight(255, 255, 255);

TextureManager.getInstance().addTexture("box", new Texture("box.jpg"));
TextureManager.getInstance().addTexture("ground", new Texture("cobblestones.jpg"));

for (int i=0;i<3;i++) {
box[i] = Primitives.getBox(13f, 2f);
box[i].setTexture("box");
box[i].setEnvmapped(Object3D.ENVMAP_ENABLED);
box[i].build();
world.addObject(box[i]);
box[i].setOrigin(SimpleVector.ORIGIN);
}
box[0].translate(-40,-1,0);
box[1].translate(0,-1,0);
box[2].translate(40,-1,0);

box[0].setAdditionalColor(java.awt.Color.RED);
box[1].setAdditionalColor(java.awt.Color.GREEN);
box[2].setAdditionalColor(java.awt.Color.BLUE);

plane = Primitives.getPlane(20, 30);
plane.rotateX(PI / 2f);
plane.setSpecularLighting(true);
plane.setTexture("ground");


world.addObject(plane);

plane.build();


plane.compileAndStrip();

world.getCamera().setPosition(0, -150, -500);
world.getCamera().lookAt(box[1].getTransformedCenter());
}

private void loop() throws Exception {
buffer = new FrameBuffer(800, 600, FrameBuffer.SAMPLINGMODE_NORMAL);
buffer.disableRenderer(IRenderer.RENDERER_SOFTWARE);
buffer.enableRenderer(IRenderer.RENDERER_OPENGL);

SimpleVector tm = null;

while (!org.lwjgl.opengl.Display.isCloseRequested()) {
for (int i = 0; i < 3; i++) {
tm = box[i].getTranslation();
if (tm.z < -200) {
tm.z = 200;
box[i].translate(tm);
box[i].setOrigin(SimpleVector.ORIGIN);
System.out.println("RESET ORIGIN");
} else {
tm.z -= 0.1f;
box[i].translate(tm);
}
}
buffer.clear(java.awt.Color.BLUE);
world.renderScene(buffer);
world.draw(buffer);
buffer.update();
buffer.displayGLOnly();
Thread.sleep(300);
}
buffer.disableRenderer(IRenderer.RENDERER_OPENGL);
buffer.dispose();
System.exit(0);
}
}


Offline tjm

  • byte
  • *
  • Posts: 17
    • View Profile
Re: object not translating as expected?
« Reply #1 on: March 25, 2010, 04:14:08 pm »
Figured it out .... I wasn't setting the translation matrix correctly.

--Tim.

Offline EgonOlsen

  • Administrator
  • quad
  • *****
  • Posts: 12295
    • View Profile
    • http://www.jpct.net
Re: object not translating as expected?
« Reply #2 on: March 25, 2010, 05:33:42 pm »
Actually, you don't have to set the translation matrix for this simple case. Translations are cummulative, i.e. if you do a translate(0,0,0.1f); or similar each frame, you should get the desired movement without modifying the translation matrix yourself.

Offline tjm

  • byte
  • *
  • Posts: 17
    • View Profile
Re: object not translating as expected?
« Reply #3 on: March 25, 2010, 05:40:26 pm »
Thanks Egon.

Quote
if you do a translate(0,0,0.1f); or similar each frame, you should get the desired movement without modifying the translation matrix yourself

That's exactly what I did! After slowing down the loop it became apparent that modifiying the translation matrix was the wrong approach .... studying your demos got me sorted out.

--Tim.