Inexplicable Gravitational Problem

Started by Belligerent Gnome, Wed 27/04/2011 11:39:34

Previous topic - Next topic

Belligerent Gnome

Shortly after my AreThingsOverlapping ordeal, I decided to start working on the gravity for my quite possibly futile attempt at a combat system. Obviously, the very fabric of fate appears to be against it, as I have now encountered yet another nonsensical error. Here is the code for the gravity:

function repeatedly_execute_always()
{
  catchY[player.ID] = 198;
 
  if (player.y != catchY[player.ID])
  {gravity[player.ID]= -1;
  player.y=player.y-gravSpeed[player.ID];
  gravSpeed[player.ID]=gravSpeed[player.ID]+gravity[player.ID];
 
  if (gravity[player.ID] < -1)
  gravity[player.ID] = -1;
}

  if (player.y > catchY[player.ID])
  player.y = catchY[player.ID];
 
  if (IsKeyPressed(eKeyUpArrow)==1)
  player.y -= 20;
  }

Now, "catchY" is an array I use in the place of a conventional floor. The "if" line underneath it stops the downward motion if the player is at "catchY" (meaning that it only simulates gravity instead of actually making it realistic, but whatever) while moving it downwards if it isn't. The next "if" statement keeps gravity from becoming uncontrollably fast. The next one basically keeps the player from falling through the floor, and the last one is simply for jumping.

So, my problem is this: I can jump once, everything seems dandy, then the character just freezes in midair, the sprite bottom at y coordinate 177. When I release the button, I simply teleport to catchY (which is 198), and every time I try to jump after that I just teleport up to 177 again.

Oddly enough, when I deleted this line (I think): 
gravSpeed[player.ID]=gravSpeed[player.ID]+gravity[player.ID];

And replaced the gravSpeed with gravity in the line before it, this no longer happened; however, understandably enough, I would no longer fall without releasing the button.

Sorry for the long post, but I've been stumped on this for months and didn't really see an alternative.
Thanks for putting up with my bizarre problems.

Khris

This should do the trick:

Code: ags
int catchY[50];
float gravity[50];
float yp[50], ys[50];
bool standing[50];

#define jumpforce 10.0

void game_start() {
  catchY[player.ID] = 198;
  gravity[player.ID] = 0.5;
  yp[player.ID] = IntToFloat(player.y);
}

void repeatedly_execute() {
  
  // player isn't on ground
  if (!standing[player.ID]) {
    ys[player.ID] += gravity[player.ID]; // add gravity to y movement vector
    yp[player.ID] += ys[player.ID]; // move player by y movement vector
    player.y = FloatToInt(yp[player.ID], eRoundNearest);
    if (player.y >= catchY[player.ID]) { // player hit ground
      player.y = catchY[player.ID]; // move to ground
      yp[player.ID] = IntToFloat(player.y);
      ys[player.ID] = 0.0; // no y movement
      standing[player.ID] = true; // stop falling
    }
  }
  else if (IsKeyPressed(eKeyUpArrow)) {
    // init jump
    ys[player.ID] = -jumpforce;
    standing[player.ID] = false;
  }
}


Tested, working.

Belligerent Gnome


SMF spam blocked by CleanTalk