Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / I cant get Collisions to Work in my Dark GDK

Author
Message
Imran
15
Years of Service
User Offline
Joined: 28th Jun 2009
Location: Chicago,IL
Posted: 28th Jun 2009 16:53
I am trying to set collision on this 3d Object with a 3d cube and with the level. But the the player goes through the level and the cube,can somebody tell me where i am missing,
[#include "DarkGDK.h"

float ground,jumptimer;
float fCameraAngleX,fCameraAngleY;
float CameraAngleX,CameraAngleY;
float OldCamAngleX,OldCamAngleY;
float gravity;
float oldx,oldy,oldz;
float vx,vy,vz;
float angy;
float x,y,z,a,d,h,s;

void MovePlayerWithKeys(int Mdl);
void positionCameraToPlayer(int Mdl);
void MovePlayerWithMouse(int Mdl);
void LoadModel(int Mdl);
void LoadLevel(int Lvl);
void LoadSkBox(int Lvl);
void LoadBox(int Mdl);

// the main entry point for the application is this function


void DarkGDK ( void )
{
// turn on sync rate and set maximum rate to 60 fps
dbSyncOn ( );
dbSyncRate ( 60 );

dbPrint ( "Please wait loading model..." );
dbSync ( );
dbSync ( );

// 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 );
//dbAutomaticCameraCollision ( 0, 2 , 1 );

//Load a Box
LoadBox(4);

//dbSetGlobalCollisionOn ();
//dbAutomaticCameraCollision(0,2,1);


// 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);

//dbAutomaticObjectCollision ( 3, 10, 1 );

// update the screen
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);
}
void LoadLevel(int Lvl)
{
dbLoadObject ( "universe.dbo", Lvl );
dbSetObjectLight ( Lvl, 0 );
}
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);
}

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 )
{
if (dbUpKey()==1) { dbMoveObject(Mdl,-4);}
if (dbDownKey()==1) { dbMoveObject(Mdl,4);}
if (dbLeftKey()==1) { dbMoveObjectRight(Mdl,4);}
if (dbRightKey()==1) { dbMoveObjectLeft(Mdl,4);}

/*if (dbUpKey()==1) { if (dbObjectCollision(3,4)!=1)dbMoveObject(Mdl,-4);}
if (dbDownKey()==1) { if (dbObjectCollision(3,4)!=1)dbMoveObject(Mdl,4);}
if (dbLeftKey()==1) { if (dbObjectCollision(3,4)!=1)dbMoveObjectRight(Mdl,4);}
if (dbRightKey()==1) { if (dbObjectCollision(3,4)!=1)dbMoveObjectLeft(Mdl,4);}*/

if (dbSpaceKey()==1 && jumptimer==0) {dbMoveObjectUp(Mdl,2); jumptimer = 20;}
if (ground == 1 && jumptimer>0) { jumptimer--;}else{}
}
]
Bubzy
15
Years of Service
User Offline
Joined: 29th Jun 2009
Location: somewhere
Posted: 30th Jun 2009 00:08
im no expert, but it seems to be a logic problem. additionally as dbUpKey() downkey, left and right, return a 1 or a 0 you can process them as boolean values, thus shortening your code. so that

if(dbUpKey()==1){if (dbObjectCollision(3,4)!=1)dbMoveObject(mdl,-4);}

becomes
if(dbUpKey()){if(dbObjectCollision(3,4))dbMoveObject(Mdl,-4);}

im not sure about your use of Mdl in these functions either, i have encountered errors myself using variable names in these situations.

assuming that you are moving the player when he collides with an object i would write the code like this

if (dbKeyUp()&&dbObjectCollision(3,4))
{
dbMoveObject(3,-4);
}

as a side note i find it quite useful to use the dbText function
to output the effect of collisions and variables onto the screen, so that i can see if they are being affected at all.

ive typed more than 5 lines here so ive probably lost track a bit. dont know if ive helped or not. hope so. have fun.

void void void void void void void

Login to post a reply

Server time is: 2024-10-01 05:45:35
Your offset time is: 2024-10-01 05:45:35