Hello Guys,
I am trying to make a FPS,But th problem in my game is that the player collides with the level and stops,But when it collides with a simple 3D cube(which i created in the level beside the player),it doesn't collide with it,It goes through it,Can anybody Plz Help.
// Dark GDK - The Game Creators - www.thegamecreators.com
// whenever using Dark GDK you must ensure you include the header file
#include "DarkGDK.h"
#include "SC_Collision.h"
// the main entry point for the application is this function
float fCameraAngleX,fCameraAngleY;
float CameraAngleX,CameraAngleY;
float OldCamAngleX,OldCamAngleY;
float x,y,z,a,d,h,s;
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 positionCameraToPlayer(int Mdl);
void MovePlayerWithMouse(int Mdl);
void MovePlayerWithKeys(int Mdl);
void LoadModel(int Mdl);
void LoadLevel(int Lvl);
void LoadSkBox(int Lvl);
void LoadBox(int Mdl);
void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );
dbPrint ( "Please wait loading model..." );
dbSync ( );
dbSync ( );
SC_Start( );
// switch to the media directory, load our world
// and turn lighting off
//SetCurrentDirectory ( "media" );
//Load Player
LoadModel(3);
//Load level
LoadLevel(1);
// load a model for our sky
LoadSkBox(2);
// position the camera
dbPositionCamera ( 434, 42, -467 );
//Load a Box
LoadBox(4);
// camera variables
fCameraAngleX = 0.0f;
fCameraAngleY = 0.0f;
gravity = -0.1f;
jumptimer=1;
ground=1;
// our main loop
while ( LoopGDK ( ) )
{
MovePlayerWithMouse(3);
MovePlayerWithKeys(3);
positionCameraToPlayer(3);
dbSync ( );
}
// return back to windows
return;
}
void LoadModel(int Mdl)
{
dbLoadObject ( "Colonel-X.X", Mdl );
dbSetObjectSpecular(Mdl,0);
dbPointObject(Mdl,0,0,0);
dbFixObjectPivot(Mdl);
dbRotateObject(Mdl,0,180,-0);
dbScaleObject(Mdl,100,75,100);
dbPositionObject(Mdl,434,42,-517);
//dbSetShadowShadingOn(Mdl);
dbLoopObject(Mdl,2497,2696);
dbSetObjectSpeed(Mdl,50);
//dbSetObjectCollisionOn(Mdl);
//dbSetObjectCollisionToBoxes(Mdl);
SC_SetupObject ( Mdl, 0 ,2 );
SC_SetObjectCollisionOn(Mdl);
}
void LoadLevel(int Lvl)
{
dbLoadObject ( "universe.dbo", Lvl );
dbSetObjectLight ( Lvl, 0 );
SC_SetupComplexObject ( Lvl, 1, 10 );
}
void LoadSkBox(int Lvl)
{
dbLoadObject ( "skybox2.x", Lvl );
dbSetObjectLight ( Lvl, 0 );
dbSetObjectTexture ( Lvl, 3, 2 );
dbScaleObject ( Lvl, 5000, 5000, 5000 );
}
void LoadBox(int Mdl)
{
dbMakeObjectCube(Mdl,50);
dbLoadImage("boxtexture.jpg",1);
dbTextureObject(Mdl,1);
dbPositionObject(Mdl,484,42,-567);
//dbSetObjectCollisionOn(Mdl);
//dbSetObjectCollisionToBoxes(Mdl);
SC_SetupObject( Mdl, 2 ,2 );
//SC_SetObjectCollisionOn(Mdl);
}
void positionCameraToPlayer(int Mdl)
{
x=dbObjectPositionX(Mdl);
y=dbObjectPositionY(Mdl);
z=dbObjectPositionZ(Mdl);
a=dbObjectAngleY(Mdl);
d=-65.0;
h=55.0;
s=50.0;
dbSetCameraToFollow(x,y,z,a,d,h,s,1);
}
void MovePlayerWithMouse(int Mdl)
{
OldCamAngleY = CameraAngleY;
OldCamAngleX = CameraAngleX;
CameraAngleY = dbWrapValue ( CameraAngleY + dbMouseMoveX ( ) * 0.4 );
CameraAngleX = dbWrapValue ( CameraAngleX + dbMouseMoveY ( ) * 0.4 );
dbYRotateObject( Mdl, dbCurveAngle ( CameraAngleY, OldCamAngleY, 24 ));
}
void MovePlayerWithKeys(int Mdl )
{
float oldx = dbObjectPositionX(Mdl);
float oldy = dbObjectPositionY(Mdl);
float oldz = dbObjectPositionZ(Mdl);
//apply gravity, and user changes to movement
float angy = dbObjectAngleY(Mdl);
vx = 0;//the movement vectors are 0 until the player doesnt press any key
vz = 0;//the movement vectors are 0 until the player doesnt press any key
//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; //i.e vy = vy - 1
else vy = vy + gravity;//i.e vy = vy + 1
if (dbKeyState(30) == 1 ) { vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbKeyState(32) == 1 ) { vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbKeyState(17) == 1 ) { vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbKeyState(31) == 1 ) { vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
if (dbUpKey()==1) { dbMoveObject(Mdl,-4);}
if (dbDownKey()==1) { dbMoveObject(Mdl,4);}
if (dbLeftKey()==1) { dbMoveObjectRight(Mdl,4);}
if (dbRightKey()==1) { dbMoveObjectLeft(Mdl,4);}
//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 = 20;
}
}
//this would be the player's final position 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( 0, oldx,oldy,oldz, oldx,oldy+vy,oldz, radius,0 );
//int collide2 = SC_SphereSlide( 0, oldx,oldy,oldz, x,y,z, 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( 0, 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;
}
collide = SC_SphereSlideGroup( 2, 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( Mdl,x,oldy,z );
SC_UpdateObject( Mdl );
}