collision detection

Started by Vincent, Sat 23/11/2013 15:36:14

Previous topic - Next topic

Vincent

Performing A Jump Form

header:
// header
Code: ags


struct str_keys {
  eKeyCode Jump;
  eKeyCode Up;
  eKeyCode Left;
  eKeyCode Right;
  eKeyCode Down;
};

import str_keys keys;


main script:
Code: ags

str_keys keys;
export keys;

float px, py, pz, pmx, pmy, pmz;

void game_start() {
  keys.Jump = eKeySpace;
  keys.Up = eKeyW;
  keys.Left = eKeyA;
  keys.Right = eKeyD;
  keys.Down = eKeyS;
  
  px = 160.0;
  py = 160.0;
}

float gravity = -0.5;
bool on_ground = true;

#define sideways 2.0
#define updown 1.0

int frame_timer;

void repeatedly_execute() {
  
  bool walking = true;
  
  if (on_ground) {
    if (IsKeyPressed(keys.Jump)) {
      pmz = 8.0;  // jump force
      on_ground = false;
    }
    pmx = 0.0;
    pmy = 0.0;
    if (IsKeyPressed(keys.Up)) pmy -= updown;
    if (IsKeyPressed(keys.Left)) pmx -= sideways;
    if (IsKeyPressed(keys.Right)) pmx += sideways;      
    if (IsKeyPressed(keys.Down)) pmy += updown;
    if (pmx == 0.0 && pmy == 0.0) walking = false;
  }
  else {  // in the air
    pmz += gravity;    // gravity changes z vector
  }
  
  int x = FloatToInt(px + pmx, eRoundNearest)-GetViewportX();
  int y = FloatToInt(py + pmy, eRoundNearest)-GetViewportY();
  if (GetWalkableAreaAt(x, y)) {
    px += pmx;
    py += pmy;
  }
  pz += pmz;
  if (pz <= 0.0) {
    pz = 0.0;
    on_ground = true;
  }
  player.x = FloatToInt(px, eRoundNearest);
  player.y = FloatToInt(py, eRoundNearest);
  player.z = FloatToInt(pz, eRoundNearest);
  
  if (pmx < 0.0) {
    if (pmy < 0.0) player.Loop = 7;
    else if (pmy == 0.0) player.Loop = 1;
    else player.Loop = 6;
  }
  else if (pmx == 0.0) {
    if (pmy < 0.0) player.Loop = 3;
    else if (pmy > 0.0) player.Loop = 0;
  }
  else {
    if (pmy < 0.0) player.Loop = 5;
    else if (pmy == 0.0) player.Loop = 2;
    else player.Loop = 4;
  }
  
  frame_timer++;
  
  if (frame_timer >= player.AnimationSpeed - 1) {  
  frame_timer = 0;
    int f = player.Frame;
    if (on_ground) {
      if (walking) {
        f++;
        if (f >= Game.GetFrameCountForLoop(player.NormalView, player.Loop)) f = 1;
      }
      else f = 0;
    }
    player.Frame = f;
  }
}


I Tried Lately To Add A New Walkable Area In The Room (Like An Above Platform To Reach)
But Jumping On It, I Can Not Reach It In Anyhow

So The Intention Was,
How Or Where I Should GetWalkableAreaAt To The New Walkable Area To Reach/Stand ?

SMF spam blocked by CleanTalk