Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: Cpt Ezz on Mon 15/06/2009 07:38:30

Title: Accelaration and deccelaration
Post by: Cpt Ezz on Mon 15/06/2009 07:38:30
i have my ideas how to do it, and i'm working on it now
but if any one knows for serten who to make a character (car) slow down and speed up it would be a great help
Title: Re: Accelaration and deccelaration
Post by: Akatosh on Mon 15/06/2009 08:42:16
One way would be to manipulate its WalkSpeed property.

Or you could bypass the normal movement system and build something from scratch, involving a float variable or two, the repeatedly_execute part of the global script, a timer and the cCar.X and cCar.Y properties. You make the call.  :=
Title: Re: Accelaration and deccelaration
Post by: GuyAwesome on Mon 15/06/2009 11:20:36
But the walk speed can't be changed while a character is moving, can it? So it might be difficult to get smooth movement if you go that route.

Edmundito's Tween Module (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=38015.0) might be of use here:
Quote
For example, if I'm moving a car from point a to b, I want it to look like it's decelerating when as of to simulate breaking, so I can do that.
And there are AGS driving games (e.g. zabnat's Taxi demo (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=36546.0)) that prove it can be done. zabnat's demo thread also features the source code, for anyone to use or learn from. (I think it uses the 'bypass normal movement' method.) I'm fairly certain there's other examples out there, too.

EDIT:
Ah, nevermind - I see you're familiar with some AGS driving games (http://www.adventuregamestudio.co.uk/yabb/index.php?topic=36381.msg499939#msg499939). Yes, contacting the authors is probably a good idea.
Title: Re: Accelaration and deccelaration
Post by: Cpt Ezz on Wed 17/06/2009 04:38:12
i know completely off topic but how do i install the tweens module?
Title: Re: Accelaration and deccelaration
Post by: Khris on Wed 17/06/2009 08:32:01
Right-click the Scripts node in the project tree.
Title: Re: Accelaration and deccelaration
Post by: Cpt Ezz on Thu 18/06/2009 00:56:55
i know that much
and you open the global script.asc
but how do import the scripts into the game
do i have to type it all out or what?

Edit: Thanks for advice about making my own akatosh i was thinking about that
Title: Re: Accelaration and deccelaration
Post by: GuyAwesome on Thu 18/06/2009 01:08:31
Quote
i know that much
and you open the global script.asc
No, you select 'Import script...' then you navigate to wherever you saved the module and, well, import it. Left-clicking will expand out the node, giving you the option to open the global script. Right-clicking gives the option to import a module or create your own.
Title: Re: Accelaration and deccelaration
Post by: Trent R on Thu 18/06/2009 01:25:24
Then read the instructions of use in the module's manual (usual contained in the module's script header, so that you can read it in AGS)

~Trent
Title: Re: Accelaration and deccelaration
Post by: zabnat on Thu 18/06/2009 17:37:58
First of all, what ever it is you will be manipulating its x and y coordinates directly.

Here's a snippet that involves straight line physics in Taxi source (with added comments) (most of the variables are structs, so that's why they have .x):

Fdrag.x = -Cdrag * velocity.x * velocity.x * velocity.x; // Cdrag = 0.1 - real formula is -Cdrag * velocity.x * abs(velocity.x)
Frr.x = -Crr * velocity.x; // Crr = 20.0
F.x = Ftraction + Fdrag.x + Frr.x; // Ftraction = 8000.0
acceleration.x = F.x / mass; // F = ma, second law of motion
velocity.x += acceleration.x * delta_time; // delta_time is time the frame takes so in 40fps it would be 1/40

And now that you have velocity, you can move the car sprite like: player.x += velocity.x * delta_time
You multiply with delta time to get the units right. Acceleration is m/s2 and velocity is m/s and the distance to move the player is m.

The values are tweaked to get a toy car like acceleration and top speed. That's why drag formula is tweaked to make it grow faster with velocity. And ofcourse for braking (decceleration) you just flip the sign of Ftraction and the velocity will come down. When at full stop and you "brake" this way the car will go on reverse.

Hope this helps.
Title: Re: Accelaration and deccelaration
Post by: Cpt Ezz on Fri 19/06/2009 12:55:34
WOW thats a load full :o :o
i don't really know if it will help but i'm really thankfull of the script
i will try to use it if i can
so thanks a lot
Title: Re: Accelaration and deccelaration
Post by: Cpt Ezz on Mon 27/07/2009 07:29:47
Thanks again
I have just started looking at my game again and i just want to clear up some things with the script....
First where is the characters name in all this?
Which ones are bools and variables and all that(so i can add them at the top)?
So is that all that i need nothing more?
Sorry for all the questions but i don't what my computer to explode when i put the script in(i have a feeling it will do that)
Title: Re: Accelaration and deccelaration
Post by: zabnat on Mon 27/07/2009 13:47:28
About the code above:
There are no characters or objects involved in that code, the code is actually not AGS-specific. All the things in the code are float variables (well Cdrag and Crr can be constants) and even the result (player.x) is a float. After this physics calculation you need to draw the stuff on the screen. So you set the coordinates of the character or object (whatever it is you are using for your car) and draw them (first you convert coordinates from float to int). This should be all that you need on the physics department for straight driving car. In addition you'll need to assign coordinates to the car object (as I said above) and maybe assign a button or buttons to control acceleration decceleration (controlling the value of Ftraction).
Title: Re: Accelaration and deccelaration
Post by: tzachs on Mon 27/07/2009 18:50:12
You can do this easily with the Tween module like this:


int toX = 50; //Your x here
int toY = 50; //Your y here
float timeInSeconds = 4.5; //Your time here
cCar.TweenPosition(timeInSeconds, toX, toY, eEaseInTween);


This will accelerate your car's position from it's current position to 50,50 in 4.5 seconds.
In order to decelerate just replace 'eEaseInTween' with 'eEaseOutTween'.

Also, if you have a driving animation to your car, you can decrease and increase your animation speed together with the position to get a better effect, with this code:

int toX = 50; //Your x here
int toY = 50; //Your y here
float timeInSeconds = 4.5; //Your time here
cCar.TweenPosition(timeInSeconds, toX, toY, eEaseInTween, eNoBlockTween);
cCar.TweenAnimationSpeed(timeInSeconds,  cCar.AnimationSpeed -20);


This will speed up the driving animation for the car to be 20 less than the current animation speed which will give an effect of acceleration (you will probably need to play with the numbers a bit though). Use +20 for deceleration effect.