Page 1 of 1

Acceleration due to gravity (g)

PostPosted: Sun Oct 11, 2009 9:45 pm
by bkelynch
I need to know how to apply Acceleration Due to Gravity (g) to a game I am creating. The constant for g is 9.8 meters/second^2 or 32.2 feet/seconds^2.

Re: Acceleration due to gravity (g)

PostPosted: Mon Oct 12, 2009 1:10 am
by DST
I'll show you two ways: The realistic way and the easy way.

REALISTIC WAY:

First, you need to know what your scale is so you can convert. I'll make a random scale based on a 640x480 game, where every pixel=1 foot, which is an unlikely scale but good for a start. For this example, i will assume you are using 30 fps for the game speed;

Create a variable to hold the acceleration multiplier, just make it an actor variable, real, here we'll name it accel;
Create a global, real, called scale; You'll use this to adjust for the size of the game. Since our scale is 1:1 here, we'll just initialize scale at 1.
Create a global, real, called tvelocity; This is our terminal velocity, which for a human falling with no parachute, on planet earth, is 124 miles per hour. that's 5,280 * 124 divided by 3600 seconds, or 181 ft/s. So initialize tvelocity at 181*scale.

So we have a screen that's 640 feet wide and 480 feet high.

We'll accelerate 32 pixels every 30 frames, a ratio of approximately 1.07:1.

So every frame, we'll increase our multiplier by 1.07*scale (which for now is 1).

So in Actor>DrawActor>ScriptEditor>
Code: Select all
if(yvelocity<tvelocity){
accel+=1.07*scale;}
yvelocity+=accel;


The player continues to fall, but only gets faster until tvelocity is reached.

If you want 4 pixels to equal 1 foot, then you'd have a scale of 4, and it would adjust correctly.

Now you'll find that this is waaaaaaaay too fast, and that's just something you'll have to deal with as most programmers do at one time or another.


EASY WAY:
if(yvelocity<20){
yvelocity+=1 }

Re: Acceleration due to gravity (g)

PostPosted: Mon Oct 12, 2009 3:21 pm
by bkelynch
Awesome, thanks a bunch.

Re: Acceleration due to gravity (g)

PostPosted: Mon Oct 12, 2009 3:23 pm
by bkelynch
Actually, I have another question. Is it posssible to be able to show a "speedometer" of the actor's velocity with a text actor?

Re: Acceleration due to gravity (g)

PostPosted: Mon Oct 12, 2009 7:12 pm
by BlarghNRawr
Make an actor called "speed" or something like that and make it a text actor that says: 000
Under Draw actor -> scripteditor put this:
Code: Select all
speed.textNumber=speed.textNumber=yvelocity.player; //substitute 'player' for the actor that has the gravity.


If that isn't completely accurate, then make a variable named speed_var or something of the sort and under drawactor -> script editor of speed put:
Code: Select all
speed_var=yvelocity.player;
speed.textNumber=speed.textNumber=speed_var;


That should work it i'm not mistaken...

Re: Acceleration due to gravity (g)

PostPosted: Mon Oct 12, 2009 10:53 pm
by DST
It's better to clone your text actor rather than making more, using a switch inside the text actor:

switch(cloneindex){
case 0:
textNumber=speedvar;
break;
case 1:
textNumber=score;
break;
}

You also don't need to repeat a variable name when doing math, just use var*=xvar or var-=xvar;

You should not use textNumber as a variable, but rather only for display;

Your programming life will also be easier (imho) if you don't use _ in variable/actor names, its just one more special character to type. Interrupts the flow of thought.

Though you can do it either way, you'll eventually you'll have to do it this way (or similiar), if you want to make serious games. Try to make things in such a way that instead of adding them again to add stuff, you only have to duplicate them, or change a single variable to add more.