here is the DarkSDK code. For those interested in how easily code ports between db pro and the DarkSDK, this took about 20 minutes to convert. However, I am using Visual Assist, so it made life a whole lot easier. There are 1 or 2 funnies though, but here is the code.
Thanks gman and LIT for some awsome code.
#include<DarkSDK.h>
float MovesPerSec=240;
bool UseReticle = true;
int EyeHeight = 64;
int MouseSens = 3;
int AirBorne;
float Gravity = 512;
int ColliderObjNum = 2;
void CheckFall()
{
float ox = dbObjectPositionX(ColliderObjNum);
float oy = dbObjectPositionY(ColliderObjNum);
float oz = dbObjectPositionZ(ColliderObjNum);
float ny=EyeHeight + (oy-dbIntersectObject(1,ox,oy,oz,ox,-1000,oz));
dbPositionObject(ColliderObjNum,ox,ny,oz);
if(dbObjectCollision(ColliderObjNum,0)!=0)
{
AirBorne=0;
}
}
void CheckCollisions()
{
bool Moving = false;
float ox=0;
float oy=0;
float oz=0;
float delta=0;
ox = dbObjectPositionX(ColliderObjNum);
oy = dbObjectPositionY(ColliderObjNum);
oz = dbObjectPositionZ(ColliderObjNum);
delta = MovesPerSec/dbScreenFPS();
if(delta>EyeHeight) delta = EyeHeight;
dbRotateObject(ColliderObjNum,dbObjectAngleX(ColliderObjNum)+(dbMouseMoveY()/MouseSens),dbObjectAngleY(ColliderObjNum)+(dbMouseMoveX()/MouseSens),0);
if(dbObjectAngleX(ColliderObjNum) > 90) dbXRotateObject(ColliderObjNum,90);
if(dbObjectAngleX(ColliderObjNum) < -90) dbXRotateObject(ColliderObjNum,-90);
float cx=dbObjectAngleX(ColliderObjNum);
float cy=dbObjectAngleY(ColliderObjNum);
float cz=dbObjectAngleZ(ColliderObjNum);
dbRotateCamera(cx,cy,cz);
dbRotateObject(ColliderObjNum,cx,cy,cz);
if(UseReticle) dbCircle(dbScreenWidth()/2,dbScreenHeight()/2,8);
char* k = dbInKey();
if((dbUpKey())/* || dbUpper(k)=="W"*/)
{
dbXRotateObject(ColliderObjNum,0);
dbMoveObject(ColliderObjNum,delta);
dbXRotateObject(ColliderObjNum,cx);
}
if((dbDownKey())/* || k=="s"*/)
{
dbXRotateObject(ColliderObjNum,0);
dbMoveObject(ColliderObjNum,0-delta);
dbXRotateObject(ColliderObjNum,cx);
}
if((dbLeftKey())/* || k=="a"*/)
{
dbXRotateObject(ColliderObjNum,0);
dbYRotateObject(ColliderObjNum,cy-90);
dbMoveObject(ColliderObjNum,delta);
dbXRotateObject(ColliderObjNum,cx);
dbYRotateObject(ColliderObjNum,cy);
dbXRotateObject(ColliderObjNum,cx);
}
if((dbLeftKey())/* || k=="d"*/)
{
dbXRotateObject(ColliderObjNum,0);
dbYRotateObject(ColliderObjNum,cy-90);
dbMoveObject(ColliderObjNum,0-delta);
dbXRotateObject(ColliderObjNum,cx);
dbYRotateObject(ColliderObjNum,cy);
dbXRotateObject(ColliderObjNum,cx);
}
float nx = dbObjectPositionX(ColliderObjNum);
float nz = dbObjectPositionZ(ColliderObjNum);
float ny = EyeHeight+(oy-dbIntersectObject(1,nx,oy,nz,nx,-1000,nz));
float newxeast=0;
if(nx<ox)
{
newxeast = dbIntersectObject(1,nx,ny,nz,nx-(EyeHeight/2),ny,nz);
}
if(newxeast>0)
{
if(newxeast < dbObjectSizeX(ColliderObjNum)/2)
{
nx=ox;
}
}
if(dbObjectCollision(ColliderObjNum,0)!=0)
{
dbPositionObject(ColliderObjNum,ox,ny,nz);
if(dbObjectCollision(ColliderObjNum,0)!=0)
{
dbPositionObject(ColliderObjNum,nx,ny,oz);
if(dbObjectCollision(ColliderObjNum,0)!=0)
{
dbPositionObject(ColliderObjNum,nx,oy,nz);
if(dbObjectCollision(ColliderObjNum,0)!=0)
{
ny=oy;
nx=ox;
nz=oz;
} else {
ny=oy;
}
} else {
nz=oz;
}
} else {
nx=ox;
}
}
if((oy-ny) > EyeHeight)
{
AirBorne=1;
ny=oy;
CheckFall();
}
dbPositionCamera(0,nx,ny,nz);
dbPositionObject(ColliderObjNum,nx,ny,nz);
}
void Fall()
{
float delta=0;
float ox=dbObjectPositionX(ColliderObjNum);
float oy=dbObjectPositionY(ColliderObjNum);
float oz=dbObjectPositionZ(ColliderObjNum);
delta=Gravity/dbScreenFPS();
float ny = EyeHeight + (oy-dbIntersectObject(1,ox,oy,oz,ox,-1000,oz));
if((oy-ny)<=delta)
{
dbPositionObject(0,ox,ny,oz);
AirBorne=0;
} else {
dbPositionObject(ColliderObjNum,ox,oy-delta,oz);
dbPositionCamera(0,ox,oy-delta,oz);
}
if(UseReticle)
{
dbCircle(dbScreenWidth()/2,dbScreenHeight()/2,8);
}
}
void LoadMap()
{
dbLoadObject("colltest.x",1);
dbSetObjectCollisionToPolygons(1);
dbMakeObjectSphere(ColliderObjNum,EyeHeight/2);
dbSetObjectCollisionOn(ColliderObjNum);
dbHideObject(ColliderObjNum);
dbPositionCamera(0,0,0,0);
dbPositionObject(ColliderObjNum,0,0,0);
dbSetAmbientLight(100);
dbFogColor(dbRgb(128,128,128));
dbFogDistance(3000);
dbFogOn();
}
void DarkSDK()
{
dbSyncOn();
dbSyncRate(0);
dbAutoCamOff();
dbHideMouse();
dbSetCameraRange(0.1,5000);
LoadMap();
while(LoopSDK())
{
if(dbEscapeKey())return;
if(AirBorne==0)
{
CheckCollisions();
} else {
Fall();
}
dbSync();
}
}