Ok i figured it out its actually not that hard.
To start you'll need 3 functions...
void MakePlayer()
void MovePlayer()
void PositionCameraToObject(int obj)
first we'll need some variables above our function declarations first.
float vx = 0;
float vy = 0;
float vz = 0;
float slope = 0.5f;
MakePlayer is probably the simplest we'll use.
All the function does is create a sphere and position it in the world and setup its collision properties.
void MakePlayer()
{
dbMakeObjectSphere(2,14); //Creates the sphere
dbPositionObject(2,-80,15,-20); //Positions the object
SC_SetupObject(2,0,1); //sets our player object to collision group 0 and type 1
}
MovePlayer is where the magic happens.
First we set it to rotate the player object based on the mouse movement.
dbYRotateObject(2,dbObjectAngleY(2) + dbMouseMoveX()/3.0f);
dbXRotateObject(2,dbObjectAngleX(2) + dbMouseMoveY()/3.0f);
Then we create our position variables
float oldx = dbObjectPositionX(2);
float oldy = dbObjectPositionY(2);
float oldz = dbObjectPositionZ(2);
float angy = dbObjectAngleY(2);
vx = 0;
vz = 0;
Next we create our keybindings I'm not exactly sure how the Cos and sin comes in but it works lol
if(dbKeyState(32)) {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); }
Now we create our collision variables
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
int collide = SC_SphereCastGroup( 1, oldx,oldy,oldz,oldx,oldy+vy,oldz,7.0f,0);
Now heres the real collision code!
if(collide > 0)
{
float ny = SC_GetCollisionNormalY();
if(dbAbs(ny) > slope)
{
oldy = SC_GetStaticCollisionY();
}
else
{
x = x - oldx; z = z - oldz;
oldx = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
oldz = SC_GetCollisionSlideZ();
x = x + oldz; z = z + oldz;
}
}
collide = SC_SphereSlideGroup(1,oldx,oldy,oldz,x,oldy,z,7.0f,0);
if(collide > 0)
{
x = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
z = SC_GetCollisionSlideZ();
vx = 0;
vz = 0;
}
I don't fully understand exactly what its doing so I wont try and explain it.
after all of this we just use the last 2 lines
dbPositionObject(2,x,oldy,z);
SC_UpdateObject(2);
Now we're all done with our player movement function!
Lastly we will create our camera function
void PositionCameraToObject(int obj)
{ dbPositionCamera(dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2));
dbRotateCamera(dbObjectAngleX(2),dbObjectAngleY(2),dbObjectAngleZ(2));
}
All it does is position the camera to the player object and rotate the camera based on the objects rotation.
Now we'll work on our main loop.
First we'll have the basic functions
dbSyncOn();
dbSyncRate(60);
dbAutoCamOff();
dbRandomize(dbTimer());
SC_Start();
now we'll create a "world" what you do is create a object or load a .dbo or whatever model you want to use. example.
dbLoadObject("mapsmap.dbo",1);
SC_SetupComplexObject(1,1,2);
now we call our player function
now in your GDK loop we'll have these lines
MovePlayer();
PositionCameraToObject(2);
dbSync();
After you run all of this you should have a nicely running basic movement/collision system! heres the full bit of code!
#include "DarkGDK.h"
#include "SC_Collision.h"
float vx = 0;
float vy = 0;
float vz = 0;
float slope = 0.5f;
void MakePlayer()
{
dbMakeObjectSphere(2,14);
dbPositionObject(2,-80,15,-20);
SC_SetupObject(2,0,1);
}
void MovePlayer()
{
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(dbKeyState(32)) {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); }
if(dbKeyState(33))
oldy--;
if(dbSpaceKey())
oldy++;
float x = oldx + vx;
float y = oldy + vy;
float z = oldz + vz;
int collide = SC_SphereCastGroup( 1, oldx,oldy,oldz,oldx,oldy+vy,oldz,7.0f,0);
if(collide > 0)
{
float ny = SC_GetCollisionNormalY();
if(dbAbs(ny) > slope)
{
oldy = SC_GetStaticCollisionY();
}
else
{
x = x - oldx; z = z - oldz;
oldx = SC_GetCollisionSlideX();
oldy = SC_GetCollisionSlideY();
oldz = SC_GetCollisionSlideZ();
x = x + oldz; z = z + oldz;
}
}
collide = SC_SphereSlideGroup(1,oldx,oldy,oldz,x,oldy,z,7.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 PositionCameraToObject(int obj)
{
dbPositionCamera(dbObjectPositionX(2),dbObjectPositionY(2),dbObjectPositionZ(2));
dbRotateCamera(dbObjectAngleX(2),dbObjectAngleY(2),dbObjectAngleZ(2));
}
void DarkGDK()
{
dbSyncOn();
dbSyncRate(60);
dbAutoCamOff();
dbRandomize(dbTimer());
SC_Start();
//MakeLevel();
dbSetDir("media\");
dbLoadObject("maps\map\map.dbo",1);
SC_SetupComplexObject(1,1,2);
MakePlayer();
while(LoopGDK())
{
MovePlayer();
PositionCameraToObject(2);
dbSync();
}
}
I hope this helped if i made any mistakes please let me know and ill correct it!
NOTE: I wouldn't recommend just copying and pasting the bit of code at the end, Type it once look at it and understand what its doing. (the sparkys help file has all the commands and what to pass to them and what they do this is just to show the structure)
The only limit to creating video games is your imagination