Acceleration due to gravity (g)

You must understand the Game Editor concepts, before post here.

Acceleration due to gravity (g)

Postby bkelynch » Sun Oct 11, 2009 9:45 pm

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.
bkelynch
 
Posts: 73
Joined: Sun Dec 04, 2005 7:48 pm
Score: 0 Give a positive score

Re: Acceleration due to gravity (g)

Postby DST » Mon Oct 12, 2009 1:10 am

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 }
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score

Re: Acceleration due to gravity (g)

Postby bkelynch » Mon Oct 12, 2009 3:21 pm

Awesome, thanks a bunch.
bkelynch
 
Posts: 73
Joined: Sun Dec 04, 2005 7:48 pm
Score: 0 Give a positive score

Re: Acceleration due to gravity (g)

Postby bkelynch » Mon Oct 12, 2009 3:23 pm

Actually, I have another question. Is it posssible to be able to show a "speedometer" of the actor's velocity with a text actor?
bkelynch
 
Posts: 73
Joined: Sun Dec 04, 2005 7:48 pm
Score: 0 Give a positive score

Re: Acceleration due to gravity (g)

Postby BlarghNRawr » Mon Oct 12, 2009 7:12 pm

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...
Download Game Music
current projects:
Bold! ?% Started mechanics
Unnamed puzzle game 60% --i need a name!--
User avatar
BlarghNRawr
 
Posts: 767
Joined: Wed Jun 25, 2008 12:36 am
Location: Not using G-E anymore. Now using Source SDK.
Score: 27 Give a positive score

Re: Acceleration due to gravity (g)

Postby DST » Mon Oct 12, 2009 10:53 pm

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.
It's easier to be clever than it is to be kind.
http://www.lostsynapse.com
http://www.dstgames.com
User avatar
DST
 
Posts: 1117
Joined: Sun Apr 15, 2007 5:36 pm
Location: 20 minutes into the future
Score: 151 Give a positive score


Return to Advanced Topics

Who is online

Users browsing this forum: No registered users and 1 guest