About Store Forum Documentation Contact



Post Reply 
(2D) player jumping
Author Message
DreamsInHD Offline
Member

Post: #1
(2D) player jumping
(2D game) Can someone correct my code, my character is supposed to jump and then land on the floor(rect). if i press space, he jumps and lands smoothly, but he always lands a little too high. If i double jump, he lands where he should lands in the middle of the distance he jumped. (beginner at work)
if(Kb.bp(KB_SPACE))
{
gravity = 1;
}

if(!onPlatform && gravity > -1)
{
pos.y += gravity * Time.d();
gravity -= Time.d();
}

if(Cuts(pos.y - 0.2, rect))
{
onPlatform = true;
}
09-24-2015 05:39 PM
Find all posts by this user Quote this message in a reply
georgatos7 Offline
Member

Post: #2
RE: (2D) player jumping
Hey, this might be close to what you want to do.

Code:
//----------------------------------------------------------------------------//
// DRAW ELEMENTS/VARIABLES
//----------------------------------------------------------------------------//
Rect   platform;
Circle player;

flt    gravity;

//----------------------------------------------------------------------------//
// PRE-INITIALIZE
//----------------------------------------------------------------------------//
void InitPre()
{
   EE_INIT();
   App.flag=APP_MS_EXCLUSIVE;
}

//----------------------------------------------------------------------------//
// INITIALIZE
//----------------------------------------------------------------------------//
bool Init()
{  
   platform.set(-D.w(), -0.5f, D.w(), 0.0f);
   player.set(0.2, Vec2(0.0f, 0.2f));
  
   return true;
}

//----------------------------------------------------------------------------//
// SHUT
//----------------------------------------------------------------------------//
void Shut()
{
  
}

//----------------------------------------------------------------------------//
// UPDATE
//----------------------------------------------------------------------------//
bool Update()
{
   if(Kb.bp(KB_SPACE)) // && onPlatform == true)
   {
      gravity = 1;
   }
      
   player.pos.y += gravity * Time.d();
   gravity -= Time.d();
  
   bool onPlatform = Cuts(Vec2(player.pos.x, player.pos.y - 0.2f), platform);  
   if(onPlatform && gravity<0) gravity = 0.0f;
  
  
  
   if(Kb.bp(KB_ESC))return false; // exit if escape on the keyboard pressed
   return true;                   // continue
}

//----------------------------------------------------------------------------//
// DRAW
//----------------------------------------------------------------------------//
void Draw()
{
   D.clear(BLACK);
  
   player.draw(GREEN, false);
   platform.draw(RED, false);
}

A slightly more physics based solution could be the one below.

Code:
//----------------------------------------------------------------------------//
// DRAW ELEMENTS
//----------------------------------------------------------------------------//
Rect   platform;
Circle player;

//----------------------------------------------------------------------------//
// PHYSICS VARIABLES
//----------------------------------------------------------------------------//
Vec2 gravity_Accel = Vec2(0.0f, -9.81f)/6.0f;

Vec2 total_velocity;
flt  jump_Velocity_Y = 1.2f;

//----------------------------------------------------------------------------//
// PRE-INITIALIZE
//----------------------------------------------------------------------------//
void InitPre()
{
   EE_INIT();
   App.flag=APP_MS_EXCLUSIVE;
}

//----------------------------------------------------------------------------//
// INITIALIZE
//----------------------------------------------------------------------------//
bool Init()
{  
   platform.set(-D.w(), -0.5f, D.w(), 0.0f);
   player.set(0.2, Vec2(0.0f, 0.2f));
  
   return true;
}

//----------------------------------------------------------------------------//
// SHUT
//----------------------------------------------------------------------------//
void Shut()
{
  
}

//----------------------------------------------------------------------------//
// UPDATE
//----------------------------------------------------------------------------//
bool Update()
{
   // HORIZONTAL VELOCITY
   flt left_right = 0.0f;
   if(Kb.b(KB_RIGHT)) left_right += 1.0f;
   if(Kb.b(KB_LEFT))  left_right -= 1.0f;
   total_velocity.x = left_right;
  
   // VERTICAL VELOCITY
   Vec2 delta_velocity = gravity_Accel * Time.d();
   total_velocity     += delta_velocity;
  
   if(Kb.bp(KB_SPACE)) total_velocity.y = jump_Velocity_Y; // Jump
      
   Vec2 delta_position = total_velocity * Time.d();
   player.pos      += delta_position;
  
   if(Cuts(player, platform))
   {
      total_velocity.y = 0.0f;
      player.pos.y  = platform.max.y + 0.2f;
   }
  
  
  
   if(Kb.bp(KB_ESC))return false; // exit if escape on the keyboard pressed
   return true;                   // continue
}

//----------------------------------------------------------------------------//
// DRAW
//----------------------------------------------------------------------------//
void Draw()
{
   D.clear(BLACK);
  
   player.draw(GREEN, false);
   platform.draw(RED, false);
}

Since it looks like you want to develop your own 2D Character Controller, you should keep in mind that both methods above would need you to develop your own collision handling system that imo seems like a big task.

So, i would propose using the existing physics system (but locked in 2 axis) to handle the player movement. This way you can take advantage of the PhysX built-in collision handling.

You can either use the "Controller" or "Character" Classes or you can build your own character controller by applying forces and velocities on actors.

Code:
//----------------------------------------------------------------------------//
// PHYSICS ELEMENTS (ACTORS)
//----------------------------------------------------------------------------//
Actor platform_A, platform_B;
Controller player;

//----------------------------------------------------------------------------//
// PRE-INITIALIZE
//----------------------------------------------------------------------------//
void InitPre()
{
   EE_INIT();
   App.flag=APP_MS_EXCLUSIVE;
}

//----------------------------------------------------------------------------//
// INITIALIZE
//----------------------------------------------------------------------------//
bool Init()
{
   // PHYSICS
   Physics.create(EE_PHYSX_DLL_PATH);
  
   // PLAYER
   player.create(   Capsule(0.4f, 1.7f, Vec(0.0f, 1.0f, 0.0f))   );
   player.fall_control = true;
  
   // PLATFORMS
   platform_A.create( Box(5.0f, 1.0f, 1.0f, Vec(0.0f, 0.0f, 0.0f)) ).gravity(false).freezePos(true).freezeRot(true);
   platform_B.create( Box(1.0f, 1.0f, 1.0f, Vec(4.0f, 0.0f, 0.0f)) ).gravity(false).freezePos(true).freezeRot(true);
  
   return true;
}

//----------------------------------------------------------------------------//
// SHUT
//----------------------------------------------------------------------------//
void Shut()
{
  
}

//----------------------------------------------------------------------------//
// UPDATE
//----------------------------------------------------------------------------//
bool Update()
{
   // PLAYER CONTROLLER
   flt jump = 0.0f;
   if(Kb.bp(KB_SPACE)) jump = 5.0f; // Jump
  
   flt left_right = 0.0f;
   if(Kb.b(KB_RIGHT)) left_right += 1.0f;
   if(Kb.b(KB_LEFT )) left_right -= 1.0f;
  
   player.update(Vec(2.0f, 0.0f, 0.0f)*left_right, false, jump);
  
  
  
   // PHYSICS UPDATE
   Physics.startSimulation().stopSimulation();
  
   // CAMERA
   Cam.setFromAt(Vec(player.actor.pos().x, player.actor.pos().y, -5.0f), Vec(player.actor.pos().x, player.actor.pos().y, 0.0f));
   Cam.set();
  
   // END PROGRAM
   if(Kb.bp(KB_ESC))return false; // exit if escape on the keyboard pressed
   return true;                   // continue
}

//----------------------------------------------------------------------------//
// DRAW
//----------------------------------------------------------------------------//
void Draw()
{
   D      .clear();
   Physics.draw ();
}
09-25-2015 12:33 AM
Find all posts by this user Quote this message in a reply
DreamsInHD Offline
Member

Post: #3
RE: (2D) player jumping
This helped me a lot! Thanks.
09-26-2015 05:47 PM
Find all posts by this user Quote this message in a reply
Post Reply