Skip to main content
GameDev.net gamedev.net
🔒 Locked

[java] Java3D timebased movement

Started by Lord_Evil Dec 9, 2008 at 9:16 AM 4 replies 2.2k views
Original Post
Lord_Evil
Lord_Evil
Hi, I'm having difficulties with timebased movement in a Java3D visualization app. Since the application is for visualization purposes it doesn't have a constant frame rate or notion of a "tick". So when I want to move the camera using the keyboard I don't know which time to use for my timebased movement. I tried getting the time between two consecutive KeyPress events but I can't figure out for sure if the user has released the key in between (since I always get a KeyRelease event, even if I keep pressing the key). Any ideas of how I could achieve smooth timebased camera movement without having a main loop (i.e. rendering on demand)? Thanks in advance, Thomas
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Lord_Evil
Lord_Evil
Problem is I don't have FPS.

The application visualizes a warehouse and does rendering on demand (i.e. when the scene has changed or the window needs to be repainted). As you can see, I can't use FPS in this case (if the screen was updated one minute ago, should I use fps = 1/60 ? ;) )
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Antheus
Antheus
Quote:
Original post by Lord_Evil
Problem is I don't have FPS.

The application visualizes a warehouse and does rendering on demand (i.e. when the scene has changed or the window needs to be repainted). As you can see, I can't use FPS in this case (if the screen was updated one minute ago, should I use fps = 1/60 ? ;) )


You have two time steps. One for rendering, one for simulation.

Rendering runs in a typical dynamic time step manner, rendering the state on screen.

Simulation itself however runs on demand.

Otherwise, you simply used fixed time step, whereas on key press you simply state that you moved by 0.1 seconds. This will be "on demand", but will not lead to smooth movement. The difference between simulation and renderer still exists here.
Lord_Evil
Lord_Evil
Well, it's not that much a simulation but merely an editor and viewer for warehouse topology.

I currently have a fixed step movement, but as you said it's not that smooth. And what is more important, the movement speed then depends on the speed of the machine it runs on.

So maybe I'll use the time between two KeyPress Events unless it exceeds a given limit (similar to determining whether a drag occured).
If I was helpful, feel free to rate me up ;)If I wasn't and you feel to rate me down, please let me know why!
Antheus
Antheus
Simulation refers to state in general, while renderer simply reads the state and issues graphics calls. It doesn't necessarily mean physics simulation.

Event based model isn't really ideally suited for current rendering API.

If you want time-based movement, then try using a timer to call repaint or whatever triggers your rendering 10 times a second or so.

Then let your camera have old and new position:
public class Camera {  float currentX, currentY;  float targetX, targetY;  bool needsUpdating;  bool hasChanged() {    // is current position different from target position?    // is needsUpdating true?  }  void step(float delta) {    // move current position towards target position  }};


When you receive a mouse or keyboard event, translate that to new camera position, and then set camera's target position.

Then, on each timer event, advance current to target based on this timestep.
double t = System.currentTime... // whichever version of java you're usingdouble delta = t - oldTime;if (camera.hasChanged()) {  camera.step(delta);  camera.needsUpdating = false;  repaint(); // redraw the screen}


The above will update camera only when it needs to be updated. Whenever your state changes, set needsUpdating to true regardless of coordinates.

This will give you nice the desired behavior while not constantly rendering if nothing has changed.

step() method needs to be implemented in some way, perhaps use fixed velocity when moving or something...

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.