Thanks for the help. Do the cosine and sine functions work with radians or degrees? Because, if they work with degrees, then i've a feeling something strange is going on here:
#include "DarkGDK.h"
#include <math.h>
void DarkGDK ( void )
{
dbSetDisplayMode(1280,1024,32);
dbMaximiseWindow();
dbSetWindowTitle("Cosine");
float angle = 180;
float value = cos(angle);
dbPrint(value);
dbSuspendForKey();
}
The function returns a value of '-0.59', it should return -1, shouldn't it?
[edit] ahhh, the math sine and cosine functions use radians, hence cos(PI) should equal 1, which it does. Here is the updated working code:
#include "DarkGDK.h"
#include <math.h>
#define PI 3.141592654
// A simple position class:
struct location
{
public:
float x;
float y;
float z;
};
float degrees_to_radians ( float deg )
{
float radians = (deg/360)*2*PI;
return radians;
}
void DarkGDK ( void )
{
// Set up the window:
dbSyncOn();
dbSyncRate(40);
dbAutoCamOff();
dbSetDisplayMode(1024,768,32);
dbMaximiseWindow();
// Create a simple matrix and texture it:
dbLoadImage("grass.bmp",1);
dbMakeMatrix(1,10000,10000,25,25);
dbPrepareMatrixTexture(1,1,1,1);
dbPositionCamera(0,60,-250);
// Player variables:
location player;
player.x = 0.0;
player.y = 60.0;
player.z = 0;
float player_angle = 0;
float speed = 10.0;
// Create a box to represent the player in collision calculations:
dbMakeObjectBox(1,100,200,100);
dbHideObject(1);
dbPositionObject(1,player.x,player.y,player.z);
while ( LoopGDK() && !dbEscapeKey() )
{
// Set the cursor to the top left corner of the screen:
dbSetCursor(0,0);
// Display co-ordinates to the screen:
dbPrintC("X position: ");
dbPrint(player.x);
dbPrintC("Y position: ");
dbPrint(player.y);
dbPrintC("Z position: ");
dbPrint(player.z);
dbPrintC("Player angle: ");
dbPrint(player_angle);
dbPrintC("CamPosX(GDK) = ");
dbPrint(dbCameraPositionX());
dbPrintC("CamPosY(GDK) = ");
dbPrint(dbCameraPositionY());
dbPrintC("CamPosZ(GDK) = ");
dbPrint(dbCameraPositionZ());
dbPrintC("CamYAngle(GDK) = ");
dbPrint(dbCameraAngleY());
dbPrintC("The value that would be added to the x position if the up arrow was pressed = ");
dbPrint(sin(degrees_to_radians(player_angle))*speed);
dbPrintC("The value that would be added to the z position if the up arrow was pressed = ");
dbPrint(cos(degrees_to_radians(player_angle))*speed);
// Allow the player to move the camera with the up and down arrow keys:
if ( dbUpKey()==1 )
{
player.x = player.x + ( sin(degrees_to_radians(player_angle))*speed ); // sin0 degrees is 0, this should not be increased when the angle is 0
player.z = player.z + ( cos(degrees_to_radians(player_angle))*speed ); // cos90 degrees is 0, this should not be increased when the angle is 90
}
if ( dbDownKey()==1 )
{
player.x = player.x - ( sin(degrees_to_radians(player_angle))*speed );
player.z = player.z - ( cos(degrees_to_radians(player_angle))*speed );
}
// Allow the player to rotate the camera with the left and right arrow keys:
if ( dbLeftKey()==1 )
{
player_angle = dbWrapValue(player_angle-7.3);
}
if ( dbRightKey()==1 )
{
player_angle = dbWrapValue(player_angle+7.3);
}
dbYRotateCamera(player_angle); // Rotate the camera
dbPositionObject(1,player.x,player.y,player.z); // The box will be positioned at the player's location
dbPositionCamera(player.x,player.y,player.z); // Position the camera at the box's location
dbSync(); // Update the screen
}
return;
}
Just one more question
I found this line '#define M_PI 3.14159265358979323846' in the math.h file. However, when I try to use M_PI in my degrees_to_radians function instead of the PI I defined myself, I get an undeclared identifier error. Does anyone know how to fix this?