Gravity constant

Started by G, Tue 28/02/2006 21:28:34

Previous topic - Next topic

G

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.

Alun

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?

Soup - The Comic Strip
http://www.soupcomic.com
Gods, heroes, monsters, and soup


G

Oops, I did not.

Thank you

Besh

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

"Spread our codes to the stars,
You can rescue us all"
- Muse

Bernie

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:

Code: ags

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! :))

Snarky

A couple of years back I wrote some functions for ballistics. Let's see if I can find them...

Code: ags

//   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:
Code: ags

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.

G

Thank you so much, people.

Now I have a more credible gravity.

fovmester

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:

Code: ags


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;
}



Alun

#8
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!).]

Soup - The Comic Strip
http://www.soupcomic.com
Gods, heroes, monsters, and soup


Kweepa

#9
fov, you forgot the time deltas:

Code: ags


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.
Still waiting for Purity of the Surf II

R4L

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

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


But then again, this doesn't really work.

Bernie

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

R4L

Wow... awesome! Thanks alot dude!

SMF spam blocked by CleanTalk