so i sort of been playing around with the samples and i used some of its sliding code in just a practise map. but when ever it hits the building it sends the user to the same corner. i can't even get in the building at all.
here is the code and it is long
#include "DarkGDK.h"
#include "SC_Collision.h"
//needed variables
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;
void MakeLevel()
{
dbLoadObject("building1.x",1);
dbMakeObjectTerrain(1);
SC_SetupComplexObject(1,1,0);
dbPositionObject(1,0,0,0);
//level objects has group number of 1
}
void MakePlayer()
{
dbMakeObjectSphere(10,radius*2.0f);
dbPositionObject(10,0,0,0);
SC_SetupObject(10,0,1);
//player has no groups
}
void MovePlayer()
{
//rotate player with the mouse
dbYRotateObject(10,dbObjectAngleY(10)+dbMouseMoveX()/3.0f);
dbXRotateObject(10,dbObjectAngleX(10)+dbMouseMoveY()/3.0f);
float oldx = dbObjectPositionX(10);
float oldy = dbObjectPositionY(10);
float oldz = dbObjectPositionZ(10);
//apply gravity and user changes to movement
float angy = dbObjectAngleY(10);
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;
//movement key states, w,s,a,d
if (dbKeyState(32) == 1 ) { vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbKeyState(30) == 1 ) { vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbKeyState(31) == 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbKeyState(17) == 1 ) { vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
//only jump if on ground and cetain time after
if (ground == 1)
{
if (dbSpaceKey() == 1 && jumptimer == 0)
{
vy = vy + 3.0f;
jumptimer = 20;
}
}
//final position of the object without collision
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
//first handle gravity - seperated from horizontal movement
//to achieve a more realistic effect
//Method: simple - cast a sphere vertically down, if it hits the level then
// position the object there (sticky collision) then move
// on to horizontal movement
// more complex - if we were to only use the simple method the player would be
// allowed to climb almost vertical slopes. Alternative is to
// get the normalY direction to work out how flat the gorund
// below the player is, 0-slope# is flatter, slope#-1 is steeper.
// if it's flat, use sticky collision, if it's steep slide the
// player down the slope. Changing slope# determines how steep the
// player can climb. NOTE: this also effects stairs.
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();
}
//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 cieling 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 2 succesive 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,y,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;
}
dbPositionObject(10,x,oldy,z);
SC_UpdateObject(10);
}
void PositionCameraToObject(int obj, int ThirdPerson)
{
dbPositionCamera(dbObjectPositionX(10),dbObjectPositionY(10),dbObjectPositionZ(10) );
dbRotateCamera(dbObjectAngleX(10),dbObjectAngleY(10),dbObjectAngleZ(10) );
if (ThirdPerson ==1)
{
dbPitchCameraDown(10);
dbMoveCamera(-30);
}
}
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
SC_Start();//start up the collision
dbAutoCamOff();
dbRandomize( dbTimer() );
MakeLevel();//make the level
MakePlayer();//make player
// our main loop
while ( LoopGDK ( ) )
{
MovePlayer();
//change view
if ( strcmp(dbInKey(), "v") == 0 && vtimer < dbTimer() )
{
vtimer = dbTimer() + 300;
view = 1 - view;
}
//enable/disable sync limit
if ( strcmp(dbInKey(), "l") == 0 && stimer < dbTimer() )
{
stimer = dbTimer() + 300 ;
syncmode = 60 - syncmode;
dbSyncRate( syncmode );
}
PositionCameraToObject(2,view);
dbText( 0,0,"Use W/A/S/D to move, SPACE to jump and V to change view" );
dbText( 0,20,"Press L to enable/disable sync limiting" );
dbText( 0,40,"Press RETURN to show/hide colored spheres" );
char str [ 128 ];
sprintf_s( str, 128, "FPS: %d", dbScreenFPS( ) ); dbText( 0,80,str );
sprintf_s( str, 128, "Touching Ground: %d", ground ); dbText( 0,100,str );
// update the screen
dbSync ( );
}
// return back to windows
return;
}