Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualBeerNutts

Posted 05 June 2014 - 01:41 PM

 

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right?  What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

 

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

I managed to fix it. I decided to write a logic that tells the game that the character is off the block. So I can do something like this:

if(!onBlock && getY() + idleRightAnim.getHeight() < Screen.HEIGHT - 35)
{
         if(state != ActionState.JUMPING)
         {
              state = ActionState.FALLING;
         }
}

 

That's a little better, but, you still should be trying to use a more generic solution.  Why have a jumping state, or a falling state?  Why not determine if a player is jumping or falling based on his Y velocity.  You can use some simple physics to figure out if your character is jump, falling or standing.

 

Here's simple code to get you thinking about other options

// some numbers I made up
#define GRAVITY 5.0
#define JUMP_Y_VELOCITY -50.0
#define MOVE_VELOCITY 8.0
 
void Character::Update()
{
  int tempYVelocity = m_Velocity.y;
  int tempXVelocity = m_Velocity.x;
  int tempYPos = m_Pos.y;
  int tempXPos = m_Pos.x;
 
  // all input's require used to be on the ground (unless you want character to move while in the air)
  if (m_IsOnGround)
    // if user has pressed Jump, and he's on the ground, jump
    if (Input.KeyPressed(JUMP_KEY)) {
      tempYVelocity = JUMP_Y_VELOCITY;
    }
 
    // Change X velocity based on key pressed
    if (Input.KeyPressed(MOVE_LEFT_KEY)) {
      tempXVelocity = -MOVE_VELOCITY;
    }
    if (Input.KeyPressed(MOVE_RIGHT_KEY)) {
      tempXVelocity = MOVE_VELOCITY;
    }  
  }
  // move character horizontally
  tempXPos += tempXVelocity
 
  // check if moving him caused a collision
  if (CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y)) {
    // reset location
    tempXPos = m_Pos.x;
  }
 
  // always apply gravity to the characters
  tempYVelocity += GRAVITY;  
  tempYPos += tempYVelocity;
  
  // after moving Y position, check if we've hit anything, using location and size of the character
  bool isYcollision = CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y);
 
  // check if the velocity is downwards (positive), and if the player collided with anything, then we're on the ground; reset velocity to 0
  // if velocity is downwards, then we're falling...
  if (tempYVelocity > 0) {
    // if the player collided with anything, then we're on the ground; reset velocity to 0, and move player back to original posiiton
    if (isYcollision) {
      m_IsOnGround = true;
      tempYPos = m_Pos.y;
      tempYVelocity = 0.0;
    }
    else {
      // we're falling
      m_AnimationState = FALLING;
      m_IsOnGround = false;
    }
  }
   else {
    // else we're jumping, but check if we hit our head on anything
    if (isYcollision) {
      // reset YVelocity to 0, but change state to falling
      tempYVelocity = 0.0;
      m_AnimationState = FALLING;
  }
  else {
    // we're going up, so show jumping
    m_AnimationState = Jumping;
    m_IsOnGround = false;
  }
 
 
  // set all the member variables to temp variables
  m_Velocity.y = tempYVelocity;
  m_Pos.y = tempYPos;
  m_Velocity.x = tempXVelocity;
  m_Pos.x = tempXPos;
}

This is just a sample to get you to think about generic ways of determining what a character is doing.


#2BeerNutts

Posted 05 June 2014 - 01:40 PM

 

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right?  What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

 

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

I managed to fix it. I decided to write a logic that tells the game that the character is off the block. So I can do something like this:

if(!onBlock && getY() + idleRightAnim.getHeight() < Screen.HEIGHT - 35)
{
         if(state != ActionState.JUMPING)
         {
              state = ActionState.FALLING;
         }
}

 

That's a little better, but, you still should be trying to use a more generic solution.  Why have a jumping state, or a falling state?  Why not determine if a player is jumping or falling based on his Y velocity.  You can use some simple physics to figure out if your character is jump, falling or standing.

 

Here's simple code to get you thinking about other options

// some numbers I made up
#define GRAVITY -5.0
#define JUMP_Y_VELOCITY 50.0
#define MOVE_VELOCITY 8.0
 
void Character::Update()
{
  int tempYVelocity = m_Velocity.y;
  int tempXVelocity = m_Velocity.x;
  int tempYPos = m_Pos.y;
  int tempXPos = m_Pos.x;
 
  // all input's require used to be on the ground (unless you want character to move while in the air)
  if (m_IsOnGround)
    // if user has pressed Jump, and he's on the ground, jump
    if (Input.KeyPressed(JUMP_KEY)) {
      tempYVelocity = JUMP_Y_VELOCITY;
    }
 
    // Change X velocity based on key pressed
    if (Input.KeyPressed(MOVE_LEFT_KEY)) {
      tempXVelocity = -MOVE_VELOCITY;
    }
    if (Input.KeyPressed(MOVE_RIGHT_KEY)) {
      tempXVelocity = MOVE_VELOCITY;
    }  
  }
  // move character horizontally
  tempXPos += tempXVelocity
 
  // check if moving him caused a collision
  if (CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y)) {
    // reset location
    tempXPos = m_Pos.x;
  }
 
  // always apply gravity to the characters
  tempYVelocity += GRAVITY;  
  tempYPos += tempYVelocity;
  
  // after moving Y position, check if we've hit anything, using location and size of the character
  bool isYcollision = CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y);
 
  // check if the velocity is downwards (negative), and if the player collided with anything, then we're on the ground; reset velocity to 0
  // if velocity is downwards, then we're falling...
  if (tempYVelocity < 0) {
    // if the player collided with anything, then we're on the ground; reset velocity to 0, and move player back to original posiiton
    if (isYcollision) {
      m_IsOnGround = true;
      tempYPos = m_Pos.y;
      tempYVelocity = 0.0;
    }
    else {
      // we're falling
      m_AnimationState = FALLING;
      m_IsOnGround = false;
    }
  }
   else {
    // else we're jumping, but check if we hit our head on anything
    if (isYcollision) {
      // reset YVelocity to 0, but change state to falling
      tempYVelocity = 0.0;
      m_AnimationState = FALLING;
  }
  else {
    // we're going up, so show jumping
    m_AnimationState = Jumping;
    m_IsOnGround = false;
}
 
 
// set all the member variables to temp variables
m_Velocity.y = tempYVelocity;
m_Pos.y = tempYPos;
m_Velocity.x = tempXVelocity;
m_Pos.x = tempXPos;
}

This is just a sample to get you to think about generic ways of determining what a character is doing.


#1BeerNutts

Posted 05 June 2014 - 09:51 AM

 

Warnexus, I assume, if your player steps off a platform and is falling, he won't be able to jump while falling right?  What I'm trying to get at is, you might need a generic solution which works whether you've just jumped or you're falling.

 

I'd suggest looking at trying to use a IsOnGround flag, unless you have a special can jump in mid air ability.

I managed to fix it. I decided to write a logic that tells the game that the character is off the block. So I can do something like this:

if(!onBlock && getY() + idleRightAnim.getHeight() < Screen.HEIGHT - 35)
{
         if(state != ActionState.JUMPING)
         {
              state = ActionState.FALLING;
         }
}

 

That's a little better, but, you still should be trying to use a more generic solution.  Why have a jumping state, or a falling state?  Why not determine if a player is jumping or falling based on his Y velocity.  You can use some simple physics to figure out if your character is jump, falling or standing.

 

Here's simple code to get you thinking about other options

// some numbers I made up
#define GRAVITY -5.0
#define JUMP_Y_VELOCITY 50.0
#define MOVE_VELOCITY 8.0
 
void Character::Update()
{
  int tempYVelocity = m_Velocity.y;
  int tempXVelocity = m_Velocity.x;
  int tempYPos = m_Pos.y;
  int tempXPos = m_Pos.x;
 
  // all input's require used to be on the ground (unless you want character to move while in the air)
  if (m_IsOnGround)
  // if user has pressed Jump, and he's on the ground, jump
  if (Input.KeyPressed(JUMP_KEY) && m_IsOnGround) {
    tempYVelocity = JUMP_Y_VELOCITY;
  }
 
  // Change X velocity based on key pressed (remove IsOnGround check if you want to mvoe in the air)
  if (Input.KeyPressed(MOVE_LEFT_KEY) && IsOnGround) {
    tempXVelocity = -MOVE_VELOCITY;
  }
  if (Input.KeyPressed(MOVE_RIGHT_KEY) && IsOnGround) {
    tempXVelocity = MOVE_VELOCITY;
  }  
  // move character horizontally
  tempXPos += tempXVelocity
 
  // check if moving him caused a collision
  if (CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y)) {
    // reset location
    tempXPos = m_Pos.x;
  }
 
  // always apply gravity to the characters
  tempYVelocity += GRAVITY;  
  tempYPos += tempYVelocity;
  
  // after moving Y position, check if we've hit anything, using location and size of the character
  bool isYcollision = CheckCollision(tempXPos, tempYPos, m_Size.x, m_Size.y);
 
  // check if the velocity is downwards (negative), and if the player collided with anything, then we're on the ground; reset velocity to 0
  // if velocity is downwards, then we're falling...
  if (tempYVelocity < 0) {
    // if the player collided with anything, then we're on the ground; reset velocity to 0, and move player back to original posiiton
    if (isYcollision) {
      m_IsOnGround = true;
      tempYPos = m_Pos.y;
      tempYVelocity = 0.0;
    }
    else {
      // we're falling
      m_AnimationState = FALLING;
      m_IsOnGround = false;
    }
  }
   else {
    // else we're jumping, but check if we hit our head on anything
    if (isYcollision) {
      // reset YVelocity to 0, but change state to falling
      tempYVelocity = 0.0;
      m_AnimationState = FALLING;
  }
  else {
    // we're going up, so show jumping
    m_AnimationState = Jumping;
    m_IsOnGround = false;
}
 
 
// set all the member variables to temp variables
m_Velocity.y = tempYVelocity;
m_Pos.y = tempYPos;
m_Velocity.x = tempXVelocity;
m_Pos.x = tempXPos;
}

 

This is just a sample to get you to think about generic ways of determining what a character is doing.


PARTNERS