#include "DarkGDK.h"
void DarkGDK ( void )
{
float MainThrottle = 0.00f, StrafeThrottle = 0.00f;//Ship accelerates forward when MainThrottle is positive and backwards when MainThrottle is negative. StrafeThrottle hasn't been implemented yet.
float PosX=0.0f, PosY=0.0f, PosZ=3000.0f;//The spacecraft's position in space.
float ResultVelX=0.0f, ResultVelY=0.0f, ResultVelZ=0.0f;//The spacecraft's velocities on their individual axises.
dbSyncOn ( );
dbSyncRate ( 60 );
SetCurrentDirectory("media");
dbLoadImage("earth_sphere.jpg",1);//used for the planet
dbMakeObjectSphere(1, 3000, 100, 100);
dbTextureObject(1, 1);
dbMakeObjectCube(2, 20);
dbColorObject(2, dbRGB(0, 255, 0));
dbRotateObject(2, 0, 180, 0);
dbPositionObject(2, PosX, PosY, PosZ);
dbBackdropOn();
dbBackdropColor(dbRGB(0,0,0));
dbSetCameraRange (1, 2000000);
while ( LoopGDK ( ) )
{
MainThrottle = 0.0; //reset MainThrottle to zero.
StrafeThrottle = 0.0;//reset StrafeThrottle to zero.
//The following function incrementally decreases the speed on each axis until the space craft is at a full stop.
if (dbKeyState(57)){//Space bar
if (ResultVelX>0)
ResultVelX = ResultVelX - .1;
else
ResultVelX = ResultVelX + .1;
if (ResultVelY>0)
ResultVelY = ResultVelY - .1;
else
ResultVelY = ResultVelY + .1;
if (ResultVelZ>0)
ResultVelZ = ResultVelZ - .1;
else
ResultVelZ = ResultVelZ + .1;
}
if (dbKeyState(17))//W key
MainThrottle = .05;
else if (dbKeyState(31))//S key
MainThrottle = -.05;
else if (dbKeyState(30))//A key
StrafeThrottle = -.02;
else if (dbKeyState(32))//D key
StrafeThrottle = .02;
// Display variables //
dbText(0, 0, "Position X, Y, Z");
dbText(0, 15, dbStr(PosX));
dbText(0, 30, dbStr(PosY));
dbText(0, 45, dbStr(PosZ));
dbText(0, 60, "Velocity X, Y, Z");
dbText(0, 75, dbStr(ResultVelX));
dbText(0, 90, dbStr(ResultVelY));
dbText(0, 105, dbStr(ResultVelZ));
dbText(0, 120, "Angle X, Y, Z");
dbText(0, 135, dbStr(dbObjectAngleX(2)));
dbText(0, 150, dbStr(dbObjectAngleY(2)));
dbText(0, 165, dbStr(dbObjectAngleZ(2)));
// Mouse //
dbTurnObjectRight(2, float(dbMouseMoveX()));
dbPitchObjectUp(2, -1*float(dbMouseMoveY()));
// Remember position of object //
PosX = dbObjectPositionX(2);
PosY = dbObjectPositionY(2);
PosZ = dbObjectPositionZ(2);
// Move object //
dbMoveObject(2, MainThrottle * 10);
// Add displacement to velocity //
ResultVelX = ResultVelX + (dbObjectPositionX(2) - PosX);
ResultVelY = ResultVelY + (dbObjectPositionY(2) - PosY);
ResultVelZ = ResultVelZ + (dbObjectPositionZ(2) - PosZ);
// Move object back to where it was and add velocity //
dbPositionObject(2, PosX + ResultVelX, PosY + ResultVelY, PosZ + ResultVelZ);
// camera //
dbPositionCamera(dbObjectPositionX(2), dbObjectPositionY(2), dbObjectPositionZ(2));
dbSetCameraToObjectOrientation(2);
dbMoveCamera(-50.0f);
dbMoveCameraUp(0, 13.0f);
dbSync ( );
}
dbDeleteObject(1);
dbDeleteObject(2);
return;
}
This code gives you Free motion Newtonian flight.
The key is to use dbMove() to give you the velocity vector, this command probably does alot of clever mathematics behind the scenes so I have found a way to use this to change velocity and not position, saving you the hassle of learning advanced 3D physics lol, although I would like to learn all that stuff someday anyway.
A nice little test is to get some sideways momentum going and then constantly accelarate toward the planet, this puts you in orbit of the planet, pretty cool.