I have spent a bit of time looking into this and working out exactly what the problem is. The new code looks like this:
#include "darkGDK.h"
// fDistance between 2 objects
float fDistance3D( float fX1, float fY1, float fZ1, float fX2, float fY2, float fZ2 )
{
float fDistance;
dbMakeVector3( 1 );
dbSetVector3(1, fX1 - fX2, fY1 - fY2, fZ1 - fZ2);
fDistance = dbLengthVector3( 1 );
return(fDistance);
}
void DarkGDK(void)
{
float fDistance;
// Original object positions
float fObjectX = 0.0f;
float fObjectY = 0.0f;
float fObjectZ = 5.0f;
// Original camera positions
float fCameraX = 0.0f;
float fCameraY = 0.0f;
float fCameraZ = 0.0f;
// Setup
dbAutoCamOff();
dbSyncOn();
// Create/position cube object
dbMakeObjectCube(1,50);
dbPositionObject(1,fObjectX,fObjectY,fObjectZ);
// Position camera
dbPositionCamera(fCameraX,fCameraY,fCameraZ);
// Point camera at cube
dbPointCamera(dbObjectPositionX(1),dbObjectPositionY(1),dbObjectPositionZ(1));
// Work out distance between camera and cube
fDistance = fDistance3D(dbObjectPositionX(1),dbObjectPositionY(1),dbObjectPositionZ(1),dbCameraPositionX(0),dbCameraPositionY(0),dbCameraPositionZ(0));
while(LoopGDK())
{
dbSync(); // Refresh screen
// Camera movements
if(dbUpKey() == 1)
{
dbMoveCamera(fDistance);
while(dbScanCode()>0){}
}
if(dbDownKey() == 1)
{
dbMoveCamera(-fDistance);
while(dbScanCode()>0){}
}
// Tell us whether or not camera and object positions are exactly the same
if( (dbObjectPositionX(1) == dbCameraPositionX(1)) && (dbObjectPositionY(1) == dbCameraPositionY(1)) && (dbObjectPositionZ(1) == dbCameraPositionZ(1)) )
{
dbPrint("Object and camera positions are the SAME");
}
else
{
dbPrint("Object and camera positions are DIFFERENT");
}
dbPrint("");
// Display general information
dbSetTextToBold(); dbPrint("Current camera positions"); dbSetTextToNormal();
dbPrint(dbStr(dbCameraPositionX(0)));
dbPrint(dbStr(dbCameraPositionY(0)));
dbPrint(dbStr(dbCameraPositionZ(0)));
dbPrint("");
dbSetTextToBold(); dbPrint("Object position"); dbSetTextToNormal();
dbPrint(dbStr(dbObjectPositionX(1)));
dbPrint(dbStr(dbObjectPositionY(1)));
dbPrint(dbStr(dbObjectPositionZ(1)));
dbPrint("");
dbPrint("");
dbSetTextToBold(); dbPrint("Original camera position"); dbSetTextToNormal();
dbPrint(dbStr(fCameraX));
dbPrint(dbStr(fCameraY));
dbPrint(dbStr(fCameraZ));
dbPrint("");
dbSetTextToBold(); dbPrint("Original distance between camera and object"); dbSetTextToNormal();
dbPrint(dbStr(fDistance));
}
}
After 1 up key press, the camera should move into the
same position as the cube. Printed on the screen after 1 press is as follows:
Current object positions: 0,0,
5 (XYZ)
Current camera positions: 0,0,
4.99999952316 (XYZ)
Original distance between camera and object:
5
Original camera position: 0,0,0 (XYZ)
Clearly, the object is not in
exactly the right place (it is about 0.0000005 out of place).
We can see that the value used with dbMoveCamera is 5 because this is the original distance between the camera and object. This means that there is no problem in calculating the distance between the camera and object (the distance calculated is correct).
Therefore, it appears that there is a bug with dbMoveCamera which is causing it to move the camera slightly too short.
Any second opinions?