I took the code from Sparks Slide example, it worked he jumps when I press space then goes back down, but after changing Sparks Slide code to a class, when I jump and hold space, he keeps going up???
Here is the hcaracters code:
void MovePlayer::movePlayer(int RotateX, int RotateY)
{
int numSpheres = 5;
float radius = 7.0f;
//float littleRadius = 2.0f;
//int rtimer = 0;
int stimer = 0;
int vtimer = 0;
float gravity = -0.1f;
float slope = 0.5f;
int ground = 1;
int jumptimer = 0;
int syncmode = 60;
int view = 1;
//int hide = 0;
//player movement vector
float vx = 0;
float vy = 0;
float vz = 0;
float fCharacterAngleX = 0.0f;
float fCharacterAngleY = 0.0f;
float oldx = dbObjectPositionX(50);
float oldy = dbObjectPositionY(50);
float oldz = dbObjectPositionZ(50);
//apply gravity, and user changes to movement
float angy = dbObjectAngleY(50);
vx = 0;
vz = 0;
//if player is jumping or falling then apply 'normal' gravity
//if not attempt to keep the player stuck to the floor
if ( vy == 0 && jumptimer == 0 ) vy = vy + 10*gravity;
else vy = vy + gravity;
if (dbKeyState(32) == 1 )//Right
{
dbPlayObject(50,641,659);
vx = vx - dbCos(angy);
vz = vz + dbSin(angy);
/*dbPlayObject(50,641,659);
dbSetObjectSpeed(50,5);
dbMoveObjectRight(50,10);*/
/*PlayerCharacter->animate(641,659);
PlayerCharacter->setSpeed(120);
PlayerCharacter->moveRight(-10);*/
}
if (dbKeyState(30) == 1 )//Left
{
dbPlayObject(50,641,659);
vx = vx + dbCos(angy) ;
vz = vz - dbSin(angy) ;
/*dbPlayObject(50,641,659);
dbSetObjectSpeed(50,5);
dbMoveObjectLeft(50,10);*/
/*PlayerCharacter->animate(621,640);
PlayerCharacter->setSpeed(120);
PlayerCharacter->moveLeft(-10); */
}
if (dbKeyState(31) == 1 )//Backward
{
dbPlayObject(50,641,659);
vx = vx + dbSin(angy) ;
vz = vz + dbCos(angy) ;
/*dbPlayObject(50,641,659);
dbSetObjectSpeed(50,5);
dbMoveObject(50,-10);*/
/*PlayerCharacter->animate(621,640);
PlayerCharacter->setSpeed(120);
PlayerCharacter->moveBackward(-10);*/
}
if (dbKeyState(17) == 1 )//forward
{
dbPlayObject(50,641,659);
vx = vx - dbSin(angy) ;
vz = vz - dbCos(angy) ;
/*dbPlayObject(50,641,659);
dbSetObjectSpeed(50,5);
dbMoveObjectRight(50,10);*/
/*PlayerCharacter->animate(621,640);
PlayerCharacter->setSpeed(120);
PlayerCharacter->moveForward(-10);*/
}
else
{
dbStopObject(50);
}
//only jump if on ground, and a certain time after last jump
if ( ground == 1 )
{
if ( dbSpaceKey() == 1 && jumptimer == 0 )
{
vy = vy + 3.0f;
jumptimer = 10;
}
}
//this would be the player's final position without collision
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
int collide = SC_SphereCastGroup( 1, oldx,oldy,oldz, oldx,oldy+vy,oldz, radius,0 );
if ( collide > 0 )
{
//how flat is this ground
float ny = SC_GetCollisionNormalY();
if ( dbAbs(ny) > slope )
{
//FLAT, stick
oldy = SC_GetStaticCollisionY();
}
else
{
//STEEP, slide
x = x - oldx; z = z - oldz;
oldx = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
oldz = SC_GetCollisionSlideZ();
x = x + oldx; z = z + oldz;
}
//ny#<0 means the player has hit a ceiling rather than a floor
if ( ny > slope )
{
//only on ground if standing on flat ground
ground = 1;
vy = 0;
}
else
{
ground = 0;
//if player has hit a flat ceiling then stop vy# movement
if ( ny < -slope ) vy = gravity;
}
}
else
{
//nothing below player, not on ground, add vertical speed to player
oldy = oldy + vy;
ground = 0;
}
//jumptimer will decrease only when player is back on ground
//creates a pause between two successive jumps
if ( ground == 1 && jumptimer > 0 ) jumptimer--;
//handle horizontal movement as sliding
//player only collides with group 1 (level) objs and moves freely through others
collide = SC_SphereSlideGroup( 1, oldx,oldy,oldz, x,oldy,z, radius,0 );
if ( collide > 0 )
{
//if hit, reposition player, halt movement vector
x = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
z = SC_GetCollisionSlideZ();
vx = 0;
vz = 0;
//possible code for giving the player a jumping help up stairs...
//might be useful if slope# is set very high but stairs are still required
//dy = oldy - SC_GetStaticCollisionY()
//if ( dy < slope && dy > 0 && ground == 1 ) vy = 0.5;
}
//position the player
dbPositionObject( 50,x,oldy,z );
dbYRotateObject( 50,dbObjectAngleY(50) + ( RotateX/10.0f ) );
dbXRotateObject( 50,dbObjectAngleX(50)+ ( RotateY/10.0f ) );
SC_UpdateObject( 50 );
}
Am sure he should go down after pressing jump, not fly :/