Accelerate and decelerate when using moveTo( )
by Amjad Yahya · in Torque 2D Beginner · 06/22/2013 (8:18 am) · 9 replies
How can I make an object accelerate or decelerate when I'm using moveTo() method, I want to be able to move an object to a specific world position with a linear velocity that gets accelerated or decelerated over time until the object reaches its target.
I tried using setLinearDamping() to decelerate but the object never reaches the destination.
I have no idea how to accelerate during moveTo().
I tried using setLinearDamping() to decelerate but the object never reaches the destination.
I have no idea how to accelerate during moveTo().
About the author
#2
Applying linearDamping might work, but you'd have to use very small values as it ranges from 0.0 to 1.0, meaning that each step of 0.1 is 10%!
The function does estimate the time which the Moveto will take; I guess it would be a matter of creating a curve and scaling it to fit into this time.
06/22/2013 (8:52 am)
Unfortunately, there are no Easing functions in Moveto().Applying linearDamping might work, but you'd have to use very small values as it ranges from 0.0 to 1.0, meaning that each step of 0.1 is 10%!
The function does estimate the time which the Moveto will take; I guess it would be a matter of creating a curve and scaling it to fit into this time.
#3
@Simon: Thanks.
06/22/2013 (9:48 am)
@Alien Cabbage: Thanks, but using forces will not get you to the position you specify.@Simon: Thanks.
Quote:So, can moveTo() be modified to have an easing function?
The function does estimate the time which the Moveto will take; I guess it would be a matter of creating a curve and scaling it to fit into this time.
#4
AS with all code changes, it takes time to study the issue in-depth and come up with hypotheses to solve the problem. At the moment, my limited time is 100% committed to T2D 3.0.
If anyone wants to take a shot at it, please do!
06/22/2013 (10:25 am)
It would have to be a brand new function. You couldn't simply modify MoveTo(), it would require a new way of achieving similar results.AS with all code changes, it takes time to study the issue in-depth and come up with hypotheses to solve the problem. At the moment, my limited time is 100% committed to T2D 3.0.
If anyone wants to take a shot at it, please do!
#5
Anyway, I managed to get the object to decelerate using damping, but how can we make an object accelerate? I tried using applyForce() but the object seems to be traveling at constant speed.
06/23/2013 (3:14 am)
Maybe in the future moveTo() will have this feature.Anyway, I managed to get the object to decelerate using damping, but how can we make an object accelerate? I tried using applyForce() but the object seems to be traveling at constant speed.
#6
applyForce instantly applies a constant force to the object, like gravity.
applyLinearImpulse applies an...impulse, a brief push on the object. Call it a few times using schedules and you will have an accelerating object.
applyAngularImpulse will apply an impulse on the Angular velocity of the object. i.e. what speed are we rotating around our origin or another object.
applyTorque will apply Torque to the object. This is the force that is applied to the object, not its speed!
From the Getting Started tutorial :
where ThrustVector is ("X Y");
06/23/2013 (8:32 am)
To make an object accelerate gradually, you must use applyImpulse.applyForce instantly applies a constant force to the object, like gravity.
applyLinearImpulse applies an...impulse, a brief push on the object. Call it a few times using schedules and you will have an accelerating object.
applyAngularImpulse will apply an impulse on the Angular velocity of the object. i.e. what speed are we rotating around our origin or another object.
applyTorque will apply Torque to the object. This is the force that is applied to the object, not its speed!
From the Getting Started tutorial :
%this.applyLinearImpulse(%ThrustVector, "0 0");
where ThrustVector is ("X Y");
#7
Huh?
Did I not say you'll have to apply a constant force which is what acceleration or deceleration is? You have to constantly or periodically apply the force, not just once as that just causes an instantaneous change in speed i.e. acceleration/deceleration.
06/24/2013 (12:48 am)
Quote:@Alien Cabbage: Thanks, but using forces will not get you to the position you specify.
Huh?
Did I not say you'll have to apply a constant force which is what acceleration or deceleration is? You have to constantly or periodically apply the force, not just once as that just causes an instantaneous change in speed i.e. acceleration/deceleration.
#8
06/24/2013 (9:39 am)
Thanks for both of you.
#9
This is definitely possible to do in TorqueScript but it wouldn't be trivial to implement. I'd probably start by writing moveTo using one of the functions mentioned. For an object at rest,
distance = 0.5(acceleration)(time)^2
if you schedule the acceleration every second you can use this equation to determine how long it'll take for your object to get there, similarly, if you want an object to move to a spot in a certain time you can determine the acceleration using this equation as well. It's a bit more simplistic than what's done in cocos2d but it's probably a good place to start.
07/01/2013 (9:34 am)
In cocos2d-x/iphone these features are available. It's done similar to what Simon is saying by changing parameters based off of curves during updates in order to keep the timing the same even though the speed of the object is changing. The source code for both of these engines are available. This is the basic idea http://hosted.zeh.com.br/tweener/docs/en-us/misc/transitions.htmlThis is definitely possible to do in TorqueScript but it wouldn't be trivial to implement. I'd probably start by writing moveTo using one of the functions mentioned. For an object at rest,
distance = 0.5(acceleration)(time)^2
if you schedule the acceleration every second you can use this equation to determine how long it'll take for your object to get there, similarly, if you want an object to move to a spot in a certain time you can determine the acceleration using this equation as well. It's a bit more simplistic than what's done in cocos2d but it's probably a good place to start.
Alien Cabbage
If you apply damping then it won't get there in the time you specify as you're working against it.
If you want to have an object accelerate or decelerate then you'll obviously have to apply a constant force which is what acceleration and deceleration is. MoveTo won't do this for you as it's not what it's for.