Adventure Game Studio

AGS Support => Beginners' Technical Questions => Topic started by: smartastreacle on Mon 05/12/2022 16:16:54

Title: [SOLVED] Object acceleration
Post by: smartastreacle on Mon 05/12/2022 16:16:54
Hiya,

Really simple one. I've looked around but can't quite find the answer.
I have a 'rocket' object that takes off and then explodes at a certain Y. What I want is for the rocket to accelerate after take off.
So far I have the rocket, its movement to Y and its explosion. What I haven't got yet is the acceleration bit. I know this is easy, but it's collapsing my easily challenged brain. Any ideas welcome.

 :)
Title: Re: Object acceleration
Post by: Crimson Wizard on Mon 05/12/2022 16:29:40
You need to store Velocity in a variable, and increase velocity over time too.

So each game update you:
* increase Velocity by acceleration;
* change coordinates by velocity.

An example, using floats for more precision (smoother movement):
(this code assumes blocking animation, if you need non-blocking, then it should be updated inside repeatedly_execute instead)
Code (ags) Select
float Accel = 0.2;
float Velocity = 1.0;
float YPos = IntToFloat(oRocket.Y); // current position of a rocket object
float YTop = SOME_NUMBER_YOU_WANT;

while (YPos > YTop)
{
    YPos -= Velocity;
    Velocity += Accel;
    oRocket.Y = FloatToInt(YPos, eRoundNearest);
    Wait(1); // let the game update and redraw
}
Title: Re: Object acceleration
Post by: smartastreacle on Mon 05/12/2022 16:34:39
Thank you, Mr Wizard. If I understand this, it will work, if I don't, it won't. Let's see if it does ...  ;)
Title: Re: Object acceleration
Post by: Crimson Wizard on Mon 05/12/2022 16:37:14
Added some code example above.
Title: Re: Object acceleration
Post by: smartastreacle on Tue 06/12/2022 09:36:45
Yup, that works a treat.  (nod)  Had to fiddle around a bit, I never know where to put things.  (laugh)
Title: Re: Object acceleration
Post by: Snarky on Tue 06/12/2022 10:30:03
I'm curious why you decided to make the acceleration multiplicative, @Crimson Wizard? It's not physically accurate, as now the actual acceleration is dependent on the speed (contrary to Newton's second law).
Title: Re: Object acceleration
Post by: Crimson Wizard on Tue 06/12/2022 11:47:52
Quote from: Snarky on Tue 06/12/2022 10:30:03I'm curious why you decided to make the acceleration multiplicative, @Crimson Wizard? It's not physically accurate, as now the actual acceleration is dependent on the speed (contrary to Newton's second law).

Maybe I began to forget these things. I'll change to addition.
Title: Re: Object acceleration
Post by: smartastreacle on Sun 11/12/2022 11:04:29
The giants battle above my head. Addition it is!  (laugh)

Actually I do have physics, my books are just a bit dusty at the moment.
I changed float Accel = 0.2; to float Accel = 0.005; for added drama.
Just when you thought you were going to win ...