I'm quite sure I'm going the wrong way about this... but here's what I came up with. It only thrusts in one direction...
void DarkGDK(void)
{
dbSyncOn();
dbSyncRate(60);
int fighterImage = GetFreeImage(1);
dbLoadImage("AssetsimageFighterPod.png", fighterImage);
int fighterSprite = GetFreeSprite(1);
dbSprite(fighterSprite, dbScreenWidth()/2, dbScreenHeight()/2, fighterImage);
dbOffsetSprite(fighterSprite, dbSpriteWidth(fighterSprite)/2, dbSpriteHeight(fighterSprite)/2);
float pi = 3.1415926535897932384626433832795028841f;
float velocityAngle = 0.0f;
float velocity = 0.0f;
float trueAngle = 0.0f;
float turnSpeed = 0.3f;
float acceleration = 0.1f;
float maxVelocity = 3.0f;
while (LoopGDK())
{
if (dbKeyState(30)==1) // turn left, pressing A
{
trueAngle = trueAngle - turnSpeed;
}
if (dbKeyState(32) == 1)
{
trueAngle = trueAngle + turnSpeed;
}
if (dbKeyState(17) == 1)
{
if (velocityAngle < trueAngle)
{
velocityAngle = velocityAngle + 0.25f;
velocity = velocity - (acceleration/2);
}
else if (velocityAngle > trueAngle)
{
velocityAngle = velocityAngle - 0.25f;
velocity = velocity - (acceleration/2);
}
else if (velocityAngle == trueAngle)
{
if (velocity < maxVelocity)
{
velocity = velocity + acceleration;
}
}
}
int X = dbSpriteX(fighterSprite) + (dbSin(velocityAngle) * velocity);
int Y = dbSpriteY(fighterSprite) + (dbCos(velocityAngle) * velocity);
dbSprite(fighterSprite, X, Y, fighterImage);
dbRotateSprite(fighterSprite, trueAngle);
dbSync();
}
}
SOLVED
#include <DarkGDK.h>
#define others 10000
void loadImages()
{
dbSetImageColorKey(255, 0, 255);
dbLoadImage("ships/LightFighter.png", 1);
dbLoadImage("ships/icons/LightFighter.png", 1001);
//dbLoadImage("ships/Transport.png", 2);
//dbLoadImage("background.png", 1 + others);
}
bool handleTimer(int start, int duration)
{
if ((dbTimer() >= (start + duration)))
return true;
return false;
}
float findSpeed(float xvel, float yvel)
{
return dbABS(dbSQRT((xvel*xvel + yvel*yvel)));
}
void DarkGDK()
{
dbSyncOn();
dbSyncRate(30);
loadImages();
//player setup//
float xPos;
float yPos;
dbSprite(1, dbScreenWidth()/2, dbScreenHeight()/2, 1);
dbOffsetSprite(1, dbSpriteWidth(1)/2, dbSpriteHeight(1)/2);
int ship = 1;
dbSprite(2, 50, 75, 1000 + ship);
xPos = dbSpriteX(1);
yPos = dbSpriteY(1);
float xvelocity;
float yvelocity;
float angle=0.0f;
float acceleration = 0.5f;
float turnSpeed = 2.5f;
int maxSpeed=70;
int speed=0; //this is only for display
float modifier=10;
xvelocity = 0.0f;
yvelocity = 0.0f;
bool step;
step = false;
int startingTime=dbTimer();
int stepTime=10;
while (LoopGDK())
{
//step = handleTimer(startingTime, stepTime);
speed = findSpeed(xvelocity, yvelocity) * modifier;
if (speed <= 1)
speed = 1;
dbText(0, 0, dbStr(speed));
//if (step)
//{
step = false;
startingTime = dbTimer();
if (dbLeftKey() || dbKeyState(30))
{
dbRotateSprite(1, -(angle+=turnSpeed));
}
if (dbRightKey() || dbKeyState(32))
{
dbRotateSprite(1, -(angle-=turnSpeed));
}
if (dbUpKey() == 1 || dbKeyState(17) == 1)
{
xvelocity = xvelocity + dbSin(angle + 180.0f) * acceleration;
yvelocity = yvelocity + dbCos(angle + 180.0f) * acceleration;
}
dbSprite (1, xPos+=xvelocity, yPos+=yvelocity, ship);
//}
dbSync();
}
return;
}
This now works. Thanks to everyone that helped, it was a great learning experience.
Timer works.
#include <DarkGDK.h>
#define others 10000
void loadImages()
{
dbSetImageColorKey(255, 0, 255);
dbLoadImage("ships/LightFighter.png", 1);
dbLoadImage("ships/icons/LightFighter.png", 1001);
//dbLoadImage("ships/Transport.png", 2);
//dbLoadImage("background.png", 1 + others);
}
void DarkGDK()
{
dbSyncOn();
dbSyncRate(30);
loadImages();
//player setup//
float xPos;
float yPos;
dbSprite(1, dbScreenWidth()/2, dbScreenHeight()/2, 1);
dbOffsetSprite(1, dbSpriteWidth(1)/2, dbSpriteHeight(1)/2);
int ship = 1;
dbSprite(2, 50, 75, 1000 + ship);
xPos = dbSpriteX(1);
yPos = dbSpriteY(1);
float xvelocity;
float yvelocity;
float angle=0.0f;
float lockedAngle = 180.0f;
float acceleration = 0.5f;
float turnSpeed = 2.5f;
int maxSpeed=70;
int speed=0; //this is only for display
float modifier=10;
xvelocity = 0.0f;
yvelocity = 0.0f;
bool step;
step = false;
int startingTime=dbTimer();
int stepTime=25;
while (LoopGDK())
{
step = (dbTimer() >= (startingTime + stepTime));
speed = dbABS(dbSQRT((xvelocity*xvelocity + yvelocity*yvelocity))) * modifier;
if (speed < 1)
speed = 0;
dbText(0, 0, dbStr(speed));
if (step)
{
step = false;
startingTime = dbTimer();
if (dbLeftKey() || dbKeyState(30))
{
dbRotateSprite(1, -(angle+=turnSpeed));
}
if (dbRightKey() || dbKeyState(32))
{
dbRotateSprite(1, -(angle-=turnSpeed));
}
if (dbUpKey() == 1 || dbKeyState(17) == 1)
{
xvelocity = xvelocity + dbSin(angle + 180.0f) * acceleration;
yvelocity = yvelocity + dbCos(angle + 180.0f) * acceleration;
}
dbSprite (1, xPos+=xvelocity, yPos+=yvelocity, ship);
}
dbSync();
}
return;
}
As you can see, I cleaned it up a little bit...
Any ideas on how to implement a max speed?