Time Based Movement?
by David House · in Torque Game Builder · 03/06/2005 (5:45 am) · 4 replies
Can I use LinearVelocity to do time based movement. Say I want to move a sprite from one position to another, and I want it to take exactly ( or real close ) to 5 seconds. I can get the distance between the two positions by some simple math. But if I set the LinearVelocity, does that translate at all to a specific time? I know I could also just set the position manually in the Update of each frame, but I'd rather use that as a last resort. Any other ideas?
#2
Something along these lines...
- Melv.
03/06/2005 (11:39 am)
... just make sure you've got damping set to zero so you don't get any deceleration.Something along these lines...
%startPos = "0 0"; // Start Position, %endPos = "30 20"; // End Position. %speed = 5; // World-units / sec. // Calculate linear velocity over time. %velocity = vectorScale2D( vectorSub2D(%endPos , %startPos ), 1 / %speed ); // Set Linear Velocity. %obj.setLinearVelocity( %velocity );
- Melv.
#3
03/06/2005 (11:49 am)
Great, that was what I was looking for. I'll give that a try and see how it works out. Thanks!
#4
You invoke it as such:
Note that you could make this a bit smarter by taking the linear velocity and then scaling it against the distance check, so there's no possible chance of bounce-back. I just used "30" because it fits my needs. Also, you need to make sure damping is set to something low that will let the object move. I use a damping of 20 at the end to make the object skid to a halt at just the right distance. If you don't, the object will jerk towards its goal, since a high damping doesn't let it get very far per moveto iteration.
Man, I sure hope schedules really don't take that much CPU, my script is absolutely brimming with 'em. :)
PS: I re-typed that function from my code, as what I had was a lot more specific to my game, so it might contain errors.
03/06/2005 (7:08 pm)
I use a slightly more adaptive method that's a bit more ugly, but it WILL get the object to point X/Y, and better, it'll handle any random impulses you might throw at the object along the way... (That's how the boss in Cloudburst returns to his waypoints even when you knock him back.)function fxAnimatedSprite2D::moveto (%obj, %goal, %accel) {
%dist = vectorlength2D (vectorsub2d (%obj.getposition(), %goal)) ;
if (%dist > 30) {
%turnangle = getangle2d (%obj.getposition(), %goal) ;
%obj.setlinearvelocitypolar (%turnangle, %accel) ;
if (iseventpending (%obj.movetosched))
cancel (%obj.movetosched) ;
%obj.movetosched = %obj.schedule (150, moveto, %goal, %accel) ;
} else {
%obj.setdamping (20) ;
}
}You invoke it as such:
object.moveto ("300 300", 100) ;Where first argument is position in world units, and the second is driving force.Note that you could make this a bit smarter by taking the linear velocity and then scaling it against the distance check, so there's no possible chance of bounce-back. I just used "30" because it fits my needs. Also, you need to make sure damping is set to something low that will let the object move. I use a damping of 20 at the end to make the object skid to a halt at just the right distance. If you don't, the object will jerk towards its goal, since a high damping doesn't let it get very far per moveto iteration.
Man, I sure hope schedules really don't take that much CPU, my script is absolutely brimming with 'em. :)
PS: I re-typed that function from my code, as what I had was a lot more specific to my game, so it might contain errors.
Torque Owner Josh Williams
Default Studio Name