//	Original Author: Mark Dehus
//	Description:	Projectile management class. 
//					Calculates projectile position based on time

//	Copyright (C) 2005 Mark Dehus

//	This program is free software; you can redistribute it and/or
//	modify it under the terms of the GNU General Public License
//	as published by the Free Software Foundation; either version 2
//	of the License, or (at your option) any later version.

//	For more information about this project and gpl see readme.htm

//	RECENT MODIFICATIONS:

#include "projectile.h"
#include <cmath>

using namespace Physics;

Projectile::Projectile(double curTime, double g)
{
	initVelocity = 0;
	initHeight = 0;
	direction = 0;
	angle = 0;
	startTime = curTime;
	gravity = g;
}

Projectile::Projectile(double vel, double height, double curTime, double g)
{
	initVelocity = vel;
	initHeight = height;
	direction = 0;
	angle = 0;
	startTime = curTime;
	gravity = g;
}

Projectile::Projectile(double vel, double height, double d, double a, double curTime, double g)
{
	initVelocity = vel;
	initHeight = height;
	direction = d * (3.141593/180);
	angle = a * (3.141593/180);
	startTime = curTime;
	gravity = g;
}

void Projectile::setInitHeight(double h)
{
	initHeight = h;
}

void Projectile::setInitVelocity(double v)
{
	initVelocity = v;
}

void Projectile::setAngle(double a)
{
	angle = a * (3.141593/180);
}

void Projectile::setDirection(double d)
{
	direction = d * (3.141593/180);
}

void Projectile::disable()
{
	active = false;
}

void Projectile::enable()
{
	active = true;
}

bool Projectile::isActive()
{
	return active;
}

double Projectile::getPositionHeight(double tStep)
{
	tStep = adjustTime(tStep);
	double x = (initVelocity * cos (angle)) * tStep;
	return initHeight + (initVelocity * sin (angle)) * tStep - (.5 * gravity * pow(tStep, 2));
}

double Projectile::getPositionX(double tStep)
{
	tStep = adjustTime(tStep);
	double x = (initVelocity * cos (angle)) * tStep;
	return x * cos (direction);
}

double Projectile::getPositionZ(double tStep)
{
	tStep = adjustTime(tStep);
	double x = (initVelocity * cos (angle)) * tStep;
	return x * sin (direction);
}

double Projectile::adjustTime(double t)
{
	return t - startTime;
}
