Adventure Game Studio

AGS Support => Modules, Plugins & Tools => Topic started by: G on Tue 28/02/2006 21:28:34

Title: Gravity constant
Post by: G on Tue 28/02/2006 21:28:34
Hi there.

In the game I'm doing rigth now, I need a gravity constant. Until now I used this function:

gravity ()
{
player.y ++;
}

Using this I got my character falling each time it steps out of the floor. But now that I've finished implementing the movement, I need to improve the gravity constant.

The main problem is that the speed of the character is too slow, and lacks of realism. I trieds using SetWalkingSpeed, but doesn't work. So I'm clueless.

I'd thank you so much your help.
Title: Re: Gravity constant
Post by: Alun on Tue 28/02/2006 21:48:44
This may be stating the obvious, but...

If what you've got makes the character fall too slowly, have you tried just changing the position by larger amounts?  That is, instead of "player.y ++", using "player.y += 2" or "player.y += 3", or something like that?
Title: Re: Gravity constant
Post by: G on Tue 28/02/2006 22:02:26
Oops, I did not.

Thank you
Title: Re: Gravity constant
Post by: Besh on Tue 28/02/2006 23:02:23
Remember also that the increment is not linear because: G=m*a therefore if you want  more realism you have to increment G in this way:

player.y += 1
player.y += 2
player.y += 4
player.y += 8
...

or something similar

Title: Re: Gravity constant
Post by: Bernie on Tue 28/02/2006 23:15:26
I'm unsure what you're attempting, but if you're making a platform game, it may be worthwile to look into gradual gravity.

One way would be as follows:


int grav;
int b=0;
function repeatedly_execute() {

int a=0;
while ((grav>0)&&(a < grav)) {
Ã,  Ã, b=b+1;
Ã,  Ã, if (b==10) {player.y = player.y+1;b=0;}
Ã,  Ã, a=a+1;
}

while ((grav<0)&&(a > grav)) {
Ã,  Ã, b=b+1;
Ã,  Ã, if (b==10) {player.y = player.y-1;b=0;}
Ã,  Ã, a=a-1;
}

if (grav < 20) {grav=grav+1;}

}


This gives you a smooth gradual gravity. Thanks to the while loop, you can check for a collision every pixel the character moves, very important for clean movements. To test this, you could put a 'if (keycode==32) grav=-20' (32 = space) into your on_key_press function. It'll make your character jump.

(Beaten by besh81 who posted while I was typing! :))
Title: Re: Gravity constant
Post by: Snarky on Tue 28/02/2006 23:20:54
A couple of years back I wrote some functions for ballistics. Let's see if I can find them...


//   d is total distance of fall
//   tmax is the total number of time units in the fall
//   t is the current time unit
//   returns: the current distance fallen
function fall(int d,int t,int tmax)
{
  return (d*t*t)/(tmax*tmax);
}

// This function does jumps and throws, using the max height as its baseline
//   du is the max height reached
//   dd is the distance to fall from the max height
//   tmax is the total number of time units of the trajectory
//   t is the current time unit
function ballistic(int du, int dd, int t, int tmax)
{
  int tu = tmax*100 / ( 100 + sqrt(100*100 * dd/du) );
  if(t<tu)
    return fall(du, tu-t, tu);
  else
    return fall(dd, t-tu, tmax-tu);
}


In order to use them, you'd write something like this:

int i=0;
// This jump will take 100 cycles
while(i<=100)
{
  // The character will move left 30 pixels from x=342 to x=312
  character[EGO].x = 342 - i*30/100;

  // The character will jump from a height of y = 150 (6+144)
  // to a height of y = 6 , then fall down to y = 120 (6+114)
  character[EGO].y = 6 + ballistic(144,114,i,100);
  Wait(1);
  i++;
}


This was a bunch of AGS versions ago, and the scripting language has changed quite a bit since then, but the principle remains the same.
Title: Re: Gravity constant
Post by: G on Wed 01/03/2006 01:00:20
Thank you so much, people.

Now I have a more credible gravity.
Title: Re: Gravity constant
Post by: fovmester on Thu 02/03/2006 06:40:39
If you really want to be stringent, gravitation is an acceleration of 9.81-9.82 m/s^2 which means that if you want correct gravion in your game, you need to have a velocity (vertical at least) parameter that you change every second by this acceleration. Since AGS runs at 40 FPS (most often), you should probably set your gravitation constant to 9.82/40 = 0.2455. Assuming of course that one pixel equals one meter. In otherwords do a function like this:



float y_velocity = 0;  //velocity for cEgo
                                 //Might want to make an array of these if all
                                 //your characters need gravitation

function do_gravity() {
    float acc = -0.2455;
    y_velocity += acc;
    cEgo.y+=y_velocity;
}


Title: Re: Gravity constant
Post by: Alun on Thu 02/03/2006 08:34:48
Quote from: fovmester on Thu 02/03/2006 06:40:39Assuming of course that one pixel equals one meter.

Um...that's a heck of an assumption.  If one pixel is one meter, that makes a typical human character less than two pixels tall!  You might want to rethink your scaling...

[EDIT: Also, to be really nitpicky, it's not correct that the magnitude of the gravitational acceleration on the Earth is "9.81-9.82 m/s^2".  It varies from about 9.780 m/s^2 at the equator to 9.814 m/s^2 at the poles; nowhere on Earth is it as large as 9.82 m/s^2.  But for the purposes of a game, it's very unlikely that anywhere near that precision would be necessary; 10 m/s^2 would probably be an acceptable approximation (and one much simpler to calculate with!).]
Title: Re: Gravity constant
Post by: Kweepa on Thu 02/03/2006 15:18:41
fov, you forgot the time deltas:



float y_velocity = 0;  //velocity for cEgo
                                 //Might want to make an array of these if all
                                 //your characters need gravitation

float y_position = cEgo.y;

float gravityConstant = -9.81;
float pixelsPerMeter = 40.0; // damn, I've been in America too long

function do_gravity() {
    float timeStep = 1.0/IntToFloat(GetGameSpeed());
    y_velocity += timeStep*gravityConstant*pixelsPerMeter;
    y_position += timeStep*y_velocity;
    cEgo.y = FloatToInt(y_position);
}


I also stored the y position as a separate float to make things a bit smoother.
Title: Re: Gravity constant
Post by: R4L on Wed 08/03/2006 20:35:38
Bernie, what would you use so that when you jump onto a higher platform, you stop falling? I used this, but it stops when the character is at a certain height.

if (player.y >= 100){
b=b+10;
}


But then again, this doesn't really work.
Title: Re: Gravity constant
Post by: Bernie on Wed 08/03/2006 23:47:14
I made a module you can take a look at. It gives the player character a platform movement and uses walkable areas of any shape (I hope) as platforms. Take a look at the 'up' and 'down' code in the module's script.

http://www.origamihero.com/files/plat01.scm

Use the arrow keys and space to control your character.
Title: Re: Gravity constant
Post by: R4L on Sun 12/03/2006 05:08:37
Wow... awesome! Thanks alot dude!