Author Topic: translate a 3ds object  (Read 2165 times)

Offline fardke

  • byte
  • *
  • Posts: 10
    • View Profile
translate a 3ds object
« on: June 02, 2008, 10:32:03 pm »
Hello,

I want to do an animation with my object 3ds (i want to see it move vertically until y=-50 while i do the translate()).
Here is the code i use for that :

Code: [Select]
while(selected.getTransformedCenter().y>-50){
System.out.println("x : "+x);
x=Math.sqrt(x);
selected.translate(new SimpleVector(0,-x,0));
try {
        Thread.sleep(500);
} catch (InterruptedException e) {
        // TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("selected y : "+selected.getTransformedCenter().y);
x++;
repaint();
}

The problem is that i only see the object at its last position, i don't see the animation.
When i drag the object with the mouse, it works perfectly (i do the same thing, a translate() then a repaint()) but it doesn't work "manually".


Help me please  ???

Offline paulscode

  • double
  • *****
  • Posts: 863
    • View Profile
    • PaulsCode.Com
Re: translate a 3ds object
« Reply #1 on: June 03, 2008, 03:27:47 am »
My question is probably dumb, but are you running that loop from a different thread than the one the paint() method is running from?  I experienced a problem like what you described when I was running a loop to translate an object from inside paint().  My problem was that the FrameBuffer needs to be updated for translations to take effect.  The solution was to run the translation loop in a different thread than the paint() method (i.e. the main "game loop").

Another way (easier, but uglier) would be to structure your paint() method something like this (untested):

Code: [Select]
@Override
public void paint( Graphics g )
{
    if(selected.getTransformedCenter().y <= -50){
        System.out.println("x : "+x);
        x=Math.sqrt(x);
        selected.translate(new SimpleVector(0,-x,0));
        try {
            Thread.sleep(500); // (paint() is a bad place for this to be)
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        System.out.println("selected y : "+selected.getTransformedCenter().y);
        x++;
        repaint(); // doesn't actually call paint(), but lets program know it should
    }
    buffer.clear();   // erase the previous frame

    // render the world onto the buffer:
    world.renderScene( buffer );
    world.draw( buffer );
    buffer.update();
    buffer.display( g, 0, 0);  // Paint this frame (software mode)
}

In summary, the important point here is: the FrameBuffer must be updated for translations take effect.  That is probably not what your problem is, but I thought I would bring it up just in case.  Sorry if this doesn't help you out much.
« Last Edit: June 03, 2008, 04:03:30 am by paulscode »

Offline Melssj5

  • double
  • *****
  • Posts: 724
    • View Profile
Re: translate a 3ds object
« Reply #2 on: June 03, 2008, 03:49:31 pm »
Exactly, you are moving your object inside the while loop and then you render it.

the while loop is taken like a whole block, if you want to watch the movement you must render on each movement iteration, this can be done by doing the rendering on another thread or by calling the rendering stuff from within your loop.
Nada por ahora