Hi, ive seen that some people have trouble with jumping. i had nothing to do so i thought id help those with this problem. here is the code:
#include "DarkGDK.h"
//Declare Global Variables
float Vx=0.0f;
float Vy=0.0f;
float PosX=0.0f;
float PosY=0.0f;
float Gravity = 0.2f;
float GroundFriction = 0.05f;
bool Ground=true;
int JumpLeft=1;
int SpacePressed=0;
char str[128]="";
void DarkGDK(void)
{
dbSyncOn ( );
dbSyncRate ( 60 );
//Make Player
dbMakeObjectSphere(1,10);//by default objects are created at 0,0,0
//Make "Ground"
dbMakeObjectBox(2,400,10,10);//There will be no collision detection, just a simulation
dbPositionObject(2,0,-10,0);
//Position Camera
dbPositionCamera(0,20,-100);
while(LoopGDK())
{
///////////
//Jump Code
if(dbSpaceKey()){SpacePressed++;}//Pressed Jump Key
else{SpacePressed=0;}//Did not press Jump Key
if(PosY < 0.1f)//Player is touching ground, reset jumpstate
{
Ground=true;
JumpLeft=1;
Vy=0.0f;
}
else{Ground=false;}//Player not touching ground
if(SpacePressed==1)//We only want to detect when the Jumpkey was pressed.
{
if(JumpLeft>0){Vy=4;JumpLeft--;Ground=false;}//add vertical force, and will no longer be on ground
}
if(Ground==false){Vy=Vy-Gravity;}//Player is not touching ground, apply gravity
PosY=PosY+Vy;//Make changes to Y position
//End Jump code
///////////////
/////////////////////////
//Horizontal movement Code
//Add Horizontal movement depending on what key is pressed
if(dbLeftKey()){Vx=-2.0f;}
if(dbRightKey()){Vx=2.0f;}
//This code stops the player absolutely
if(Vx<0.5f && Vx>-0.5f){Vx=0.0f;}
//Ground Friction
if(Ground==true)
{
if(Vx>0.5f){Vx=Vx-GroundFriction;}
if(Vx<-0.5f){Vx=Vx+GroundFriction;}
}
PosX=PosX+Vx;
//End Horizontal movement
/////////////////////////
dbPositionObject(1,PosX,PosY,0);//position player
sprintf_s( str, 128, "Position X: %f", dbObjectPositionX(1) );dbText( 0,dbScreenHeight()-40,str);
sprintf_s( str, 128, "Position Y: %f", dbObjectPositionY(1) );dbText( 0,dbScreenHeight()-20,str);
sprintf_s( str, 128, "JumpLeft: %d", JumpLeft );dbText( dbScreenWidth()-100,dbScreenHeight()-60,str);
sprintf_s( str, 128, "Ground: %d", Ground );dbText( dbScreenWidth()-80,dbScreenHeight()-40,str);
sprintf_s( str, 128, "FPS: %d", dbScreenFPS( ) );dbText( dbScreenWidth()-60,dbScreenHeight()-20,str);
dbSync();//Update camera
}
}
hi