[SOLVED] Object acceleration

Started by smartastreacle, Mon 05/12/2022 16:16:54

Previous topic - Next topic

smartastreacle

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.

 :)

Crimson Wizard

#1
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
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
}

smartastreacle

Thank you, Mr Wizard. If I understand this, it will work, if I don't, it won't. Let's see if it does ...  ;)

Crimson Wizard

Added some code example above.

smartastreacle

Yup, that works a treat.  (nod)  Had to fiddle around a bit, I never know where to put things.  (laugh)

Snarky

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).

Crimson Wizard

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.

smartastreacle

#7
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 ... 

SMF spam blocked by CleanTalk