Where to update movement vars?
by Andre Lind · in Torque Game Builder · 08/01/2006 (12:00 pm) · 8 replies
A small question really:
How should I update my movement variable (for example)?
Right now I'm using a timer set to 25ms (too fast? slow?) that calls my method updateMovement but I've seen examples where people use onUpdateScene(%this)...
What is the best solution? Movement I think should be unbound by framerate, but is a timer the best solution?
How should I update my movement variable (for example)?
Right now I'm using a timer set to 25ms (too fast? slow?) that calls my method updateMovement but I've seen examples where people use onUpdateScene(%this)...
What is the best solution? Movement I think should be unbound by framerate, but is a timer the best solution?
#2
Also, never put any movement functions within the 'onUpdateScene' function since that function depends on the frame rate.
08/01/2006 (1:00 pm)
I believe there are examples of two different methods. One is using linearvelocity everytime the key is pressed, which is found in the shooter tutorial. The other has movement using timers, which is the car physics tutorial.Also, never put any movement functions within the 'onUpdateScene' function since that function depends on the frame rate.
#3
08/01/2006 (9:43 pm)
You can use t2dSceneGraph::onUpdateScene to call your physics methods, but you should make sure that you are calling it on the right scenegraph (because the level builder has its own scenegraph and you can crash TGB if youre not careful) and you may have to scale certain physics calculations based on the time since the last frame (you can use getSceneTime on your scenegraph for this).
#4
08/01/2006 (9:59 pm)
Thomas, isn't calling any physics/movement in the onUpdateScene function a risk since the FPS will vary on different computers? Even if you try and overcome this by finding delta time, isn't that overcomplicating matters? Or is there an advantage to using this method instead of simply checking when a key is pressed and calling a movement function?
#5
08/02/2006 (3:20 am)
@Apurva - It depends on whether or not you need precision (in other words - it depends on the ammount of precision you need). If you need it to act exactly the same on every computer regardless of FPS, then you will need to scale it based on time - regardless of whether you are using a timer, a schedule, or the onUpdateScene callback. Even the schedules and timers arent precise. When a timer or schedule is due it still won't run until the next scene update after it's supposed to be called. So if you need to be exact, then you will need to compensate for delta time regardless of what method you use. In fact, there are some cases where you will need to update your physics every frame. For example, in the platformer template we are working on we had to update physics every frame because we need to know as soon as possible if the player is on the ground.
#6
08/02/2006 (4:43 am)
Just to clarify, as long as you are using setLinearVelocity, you do not need to worry about frame time. Using delta time only matters if you are setting the position of an object yourself, via setPosition or setArea.
#7
08/02/2006 (5:31 am)
Exactly, unless you're specifically incrementing linear/angular velocity over a period of time.
#8
08/02/2006 (12:49 pm)
Oh I see. Thanks for the clarification.
Torque Owner Philip Mansfield
Without knowing the effect you're after, it's a bit difficult to give advice.