yeah.. its not gonna work...
you see at the end of the function it says:
dbPositionObject ( 2, x, oldy, z );
Therefore it will always position the object there, not allowing it to move.
You have replaced a vital part... You cant say
if(dbDownKey())
{
dbMoveObject ( 2, 1.0f);
}
the sliding demo would do something like this:
if (dbDownKey()== 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
thus you get a value that would be used in the end for
dbPositionObject( 2,x,oldy,z );
that is how the object moves...
You don't have to understand it that much, just be aware if it
here is your same code with that thing in it, look closely at the diffrences:
#include "DarkGDK.h"
#include "SC_Collision.h"
int jumptimer = 0;
int ground = 1;
float slope = 0.5f;
float gravity = -0.1f;
float vx = 0;
float vy = 0;
float vz = 0;
void Gravity()
{
dbYRotateObject( 2,dbObjectAngleY(2) + dbMouseMoveX()/3.0f );
dbXRotateObject( 2,dbObjectAngleX(2) + dbMouseMoveY()/3.0f );
float oldx = dbObjectPositionX(2);
float oldy = dbObjectPositionY(2);
float oldz = dbObjectPositionZ(2);
float angy = dbObjectAngleY(2);
vx = 0;
vz = 0;
if ( vy == 0 && jumptimer == 0 ) vy = vy + 10*gravity;
else vy = vy + gravity;
if (dbRightKey()== 1 ) { vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbLeftKey()== 1 ) { vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbDownKey()== 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbUpKey()== 1 ) { vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
if ( ground == 1 )
{
if ( dbSpaceKey() == 1 && jumptimer == 0 )
{
vy = vy + 3.0f;
jumptimer = 20;
}
}
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
int collide = SC_SphereCastGroup( 1, oldx,oldy,oldz, oldx,oldy+vy,oldz, 10.0f,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;
}
if ( ny > slope )
{
ground = 1;
vy = 0;
}
else
{
ground = 0;
if ( ny < -slope ) vy = gravity;
}
}
else
{
oldy = oldy + vy;
ground = 0;
}
if ( ground == 1 && jumptimer > 0 ) jumptimer--;
collide = SC_SphereSlideGroup( 1, oldx,oldy,oldz, x,oldy,z, 10.0f,0 );
if ( collide > 0 )
{
x = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
z = SC_GetCollisionSlideZ();
vx = 0;
vz = 0;
}
dbPositionObject( 2,x,oldy,z );
SC_UpdateObject( 2 );
}
void SetUpTerrain()
{
dbMakeObjectBox ( 1, 100.0f, 1.0f, 100.0f );
dbLoadImage ( "s.jpg", 3 );
dbTextureObject ( 1, 3 );
dbScaleObject ( 1, 10000.0f, 10000.0f, 10000.0f );
dbSetObjectCull ( 1, false );
SC_SetupObject ( 1, 1, 0 );
}
void SetUpPlayer()
{
dbMakeObjectSphere ( 2, 10.0f );
dbPositionObject ( 2, 0.0f, 100.0f, 0.0f );
SC_SetupObject ( 2, 0, 1 );
}
void positionCameraToObject ( int obj, int thirdPerson)
{
dbPositionCamera( dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2) );
dbRotateCamera( dbObjectAngleX(2),dbObjectAngleY(2),dbObjectAngleZ(2) );
if ( thirdPerson == 1 )
{
dbPitchCameraDown( 10 );
dbMoveCamera( -30 );
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbAutoCamOff ();
dbRandomize ( dbTimer());
SC_Start ();
SetUpTerrain();
SetUpPlayer();
while ( LoopGDK ( ) )
{
Gravity();
positionCameraToObject ( 2, 1 );
if ( dbEscapeKey () == 1 )
{
return;
}
dbSync ( );
}
}