heres the code. I can't find any errors but when I press escape it crashes.
#include "DarkGDK.h"
#include "SC_Collision.h"
int numSpheres = 5;
float radius = 7.0f;
float littleRadius = 2.0f;
//menu variables
bool Omenu = false;
bool game = false;
bool Mmenu = true;
int rtimer = 0;
int stimer = 0;
int vtimer = 0;
int flyTimer=0;
int x_posi=float(dbObjectPositionX(2)*dbObjectPositionX(2))/2;
float gravity = -0.1f;
float slope = 0.5f;
int ground = 1;
int jumptimer = 0;
int syncmode = 60;
int view = 1;
int hide = 0;
bool allow_move=true;
//player movement vector
float vx = 0;
float vy = 0;
float vz = 0;
// if in air for more than 1 seconds then bounce when land*********
void makeLevel()
{
dbLoadObject( "Media//level_one_3dws.x",1 );
dbScaleObject(1,20,20,20);
//dbRotateObject(1,90,0,0);
dbSetObjectSpeed(1,1000);
SC_SetupObject( 1,1,0 );
dbLoadObject("Media//sky//555.x",17);
//dbSetObjectTexture ( 17, 3, 1 ); // set texture properties
dbPositionObject ( 17, 1000, 1900, 200 ); // position the skybox
dbScaleObject ( 17, 40000, 40000, 40000 ); // and finally scale
}
void menu()
{
if(Mmenu==true)
{
dbSprite (18, 0,0, 18);
dbSizeSprite( 18, (dbScreenWidth()),(dbScreenHeight()));
dbSetSprite(18, 1, 0);
dbShowSprite(18);
dbHideSprite(19);
//backgraund
//dbSprite(2, 97, 230, 2);
//buttons
//exit
if(dbMouseX() > 117 && dbMouseX() < 117+55 && dbMouseY() > 255&&dbMouseY() <255+26&&dbMouseClick())
{
return;
}
//Options
if(dbMouseX() > 95 && dbMouseX() < 105+95 && dbMouseY() > 225&&dbMouseY() <225+26&&dbMouseClick())
{
Mmenu = false;
Omenu = true;
}
}
if(Omenu==true)
{
dbSprite (19, 0,0, 20);
dbSizeSprite( 19, (dbScreenWidth()),(dbScreenHeight()));
dbSetSprite(19, 1, 0);
dbHideSprite(18);
dbShowSprite(19);
////////////////////////////////////////////////////////////////////
//back
if(dbMouseX() > 0 && dbMouseX() < 200 && dbMouseY() > 300&&dbMouseY() <500&&dbMouseClick())
{
Mmenu = true;
Omenu = false;
}
}
}
void makePlayer()
{
dbMakeObjectSphere( 2,radius*2.0f );
dbPositionObject( 2,dbObjectPositionX(1)-42,dbObjectPositionY(1)+300,dbObjectPositionZ(1)+5 );
SC_SetupObject( 2,0,1 );
dbLoadImage("Media//ball_texture.jpg",60);
dbTextureObject ( 2, 60 ) ;
//player has no group
}
void makeTarget()
{
dbMakeObjectCube(13,10);
dbPositionObject(13,dbObjectPositionX(1)-45,dbObjectPositionY(1)-120,dbObjectPositionZ(1)+200);
SC_SetupObject(13,1,2);
dbLoadImage("Media//earth.jpg",14);
dbTextureObject(13,14);
}
void handleExtraSpheres( )
{
//move all the little spheres towards the player and slide them
float oldx = dbObjectPositionX(13);
float oldy = dbObjectPositionY(13);
float oldz = dbObjectPositionZ(13);
//dbPointObject( 13,dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2) );
//dbMoveObject( 13,0.5 );
float x = dbObjectPositionX(13);
float y = dbObjectPositionY(13);
float z = dbObjectPositionZ(13);
//little spheres collide with all (0)
int collide = SC_SphereSlide( 0, oldx,oldy,oldz, x,y,z, littleRadius, 13 );
if ( collide > 0 )
{
dbPositionObject( 13, SC_GetCollisionSlideX(),SC_GetCollisionSlideY(),SC_GetCollisionSlideZ() );
}
SC_UpdateObject( 13 );
//show/hide the extra spheres
}
void level_complete(void)
{
flyTimer++;
allow_move=false;
if (flyTimer > 30)
{
gravity=0.1f;
}
}
void movePlayer()
{
//rotate player with mouse
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);
//apply gravity, and user changes to movement
float angy = dbObjectAngleY(2);
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 (allow_move==true)
{
if (dbKeyState(32) == 1 ) {dbScrollObjectTexture ( 2, -0.01, -0.01 ) ; //right
vx = vx + dbCos(angy); vz = vz - dbSin(angy); }
if (dbKeyState(30) == 1 ) {dbScrollObjectTexture ( 2, 0.01, 0.01 ); //left
dbScrollObjectTexture ( 2, 0.01, 0 ) ;
vx = vx - dbCos(angy); vz = vz + dbSin(angy); }
if (dbKeyState(31) == 1 ) {dbScrollObjectTexture ( 2, 0, -0.01 ); // back
vx = vx - dbSin(angy); vz = vz - dbCos(angy); }
if (dbKeyState(17) == 1 ) {dbScrollObjectTexture ( 2, 0, 0.01 ); //forward
vx = vx + dbSin(angy); vz = vz + dbCos(angy); }
}
//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;
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;
}
if ( ground == 1 && jumptimer > 0 ) jumptimer--;
collide = SC_SphereSlideGroup( 1, oldx,oldy,oldz, x,oldy,z, radius,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 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( -50 );
}
}
void DarkGDK( )
{
dbSyncOn( );
dbSyncRate( 60 );
//dbMakeLight(1);
//dbPositionLight(1,dbObjectPositionX(1),dbObjectPositionY(1),dbObjectPositionZ(1));
dbSetAmbientLight(100);
dbAutoCamOff( );
dbSetCameraRange ( 0.5f, 30000 );
dbRandomize( dbTimer() );
dbLoadImage("Media//menu.jpg", 18);
dbLoadImage("Media//plane.jpg", 19);
dbLoadImage("Media//Omenu.jpg", 20);
SC_Start( );
if (game==true)
{
makeLevel();
makePlayer();
makeTarget();
}
while( LoopGDK( ) )
{
if (game==false)
{
menu();
}
if (game==true)
{
SC_UpdateObject( 1 );
SC_UpdateObject( 2 );
movePlayer();
handleExtraSpheres( );
positionCameraToObject( 2, view );
if (dbObjectCollision(2,13)==1)
{
level_complete();
}
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 );
sprintf_s( str, 128, "x position: %d",x_posi);dbText(0,120,str);
}
dbSync( );
}
}