Everything seems fine when I'm facing Right (1st and 4th Quadrant, I think), but then when I face left weird stuff happens...
Go easy on my guys - I haven't written code in 7 years and it's my first 3D project ever.
#include "DarkGDK.h"
#include <math.h>
// pi and conversion function
#define PI 3.14159265358979323846
#define DEG2RAD(DEG) ((DEG)*((PI)/(180.0)))
#define RAD2DEG(RAD) (RAD*180/PI)
class vect2 {
// Generic Vector Class
public:
float x, y; // seperate component coordinates
// ctor
vect2( float in_x = 0, float in_y = 0 ) {
x = in_x;
y = in_y;
}
float getAngle() { return (float) RAD2DEG(atan( y / x )); } // returns angle of vector based on x, y
float getLength() { return (float) sqrt( x*x + y*y ); } // returns magnitude
// set x, y based on angle , magnitude
void setXYFromAM( float a, float m ) {
x = m * cos( DEG2RAD(a) );
y = m * sin( DEG2RAD(a) );
}
vect2 operator+ ( vect2 v ) { return vect2( x + v.x, y + v.y ); } // add 2 vectors
};
class Ship {
private:
int id; // id of the ship and form object
float x, y, z; // position in space
float dir; // direction facing
float aim; // direction aiming
float speed; // current speed
public:
Ship (int i); // constructor
void reset();
float getX() { return x; } // accessors
float getY() { return y; }
float getZ() { return z; }
float getDir() { return dir; }
int getid() { return id; }
float setX( float i ) { x = i; } // set functions
float setY( float i ) { y = i; }
float setZ( float i ) { z = i; }
float setDir( float i ) { dir = i; } // set dir
void thrust( float i ); // thrust forward or backward
void turn ( float i ) { aim = aim + i; } // change facing direction of ship
void update(); // updates position of the ship based on dir, speed
};
Ship::Ship( int i ) {
id = i; // get ship id
reset(); // call the reset function to init
}
void Ship::reset() {
x = y = z = 0; // position to be 0,0,0
aim = 90; // faces 90
dir = 90; // goes 90
speed = 0; // not moving
}
void Ship::thrust(float i) {
// add a thrust vector based on aim, thrust to the current movement vector
// declare vT, vM
vect2 vT, vM, result; // declare Thrust, Cur Movement, and Result (new move) vector
vT.setXYFromAM( aim, i ); // initialize Thrust vector from aim, i
vM.setXYFromAM( dir, speed ); // initialize Move vector from dir, speed
result = vT + vM; // result vector = adding thrust to current movement
dir = result.getAngle(); // calculate new direction based on added vector (rounded)
speed = result.getLength(); // calculate new speed
}
void Ship::update() {
vect2 pos( x, y ); // make pos vector based on current x, y
vect2 v; // the movement vector
v.setXYFromAM( dir, speed );
pos = pos + v;
x = pos.x;
y = pos.y;
dbRotateObject(id, 0, 0, aim);
// update the position
dbPositionObject(id, x, y, z);
}
// the main entry point for the application is this function
void DarkGDK ( void )
{
dbSyncOn ( ); // sync
dbSyncRate ( 60 );
dbRandomize ( dbTimer ( ) ); // set our random seed to a value from the timer
Ship PlayerShip(1); // instance Player
// Load the ship and place it at 0,0,0
dbLoadObject("Ship.x", PlayerShip.getid());
dbPositionObject(PlayerShip.getid(), PlayerShip.getX(), PlayerShip.getY(), PlayerShip.getZ());
// Position the Camera
dbPositionCamera ( 0, 0, -2500 );
// GDK Loop
while ( LoopGDK() ) {
// display some text on screen
dbText ( 0, 0, "Some Text" );
// add thrust to the ship
if ( dbUpKey ( ) )
PlayerShip.thrust( 3 );
// subtract thrust to the ship
if ( dbDownKey ( ) )
PlayerShip.thrust( -3 );
// turn ship left
if ( dbLeftKey ( ) )
PlayerShip.turn( 11.25 );
// turn ship right
if ( dbRightKey ( ) )
PlayerShip.turn( -11.25 );
// reset ship
if ( dbSpaceKey ( ) )
PlayerShip.reset();
// Update Ship Position
PlayerShip.update();
// Perform Sync
dbSync();
}
dbDeleteObject ( PlayerShip.getid() ); // cleanup PlayerShip
return;
}












