#include "particle.h"
#include "ofMain.h"
#define OF_ADDON_USING_OFXVECTORMATH
#include "ofAddons.h"

//------------------------------------------------------------
particle::particle(){
	setInitialCondition(0,0,0,0);
	damping = 0.15f;
	car.loadImage("car_27x60.png");
}

//------------------------------------------------------------
void particle::resetForce(){
    // we reset the forces every frame
    frc.set(0,0);
}

//------------------------------------------------------------
void particle::addForce(float x, float y){
    // add in a force in X and Y for this frame.
    frc.x = frc.x + x;
    frc.y = frc.y + y;
}

void particle::addEngineForce(float x, float y){
	engine_frc.x = engine_frc.x + x;
	engine_frc.y = engine_frc.y + y;
}

void particle::addVelocity(float x, float y){
	vel.x = vel.x + x;
	vel.y = vel.y + y;
}

//------------------------------------------------------------
void particle::addDampingForce(){
	
	// the usual way to write this is  vel *= 0.99
	// basically, subtract some part of the velocity 
	// damping is a force operating in the oposite direction of the 
	// velocity vector
	
    frc.x = frc.x - vel.x * damping;
    frc.y = frc.y - vel.y * damping;
}

//------------------------------------------------------------
void particle::setInitialCondition(float px, float py, float vx, float vy){
    pos.set(px,py);
	vel.set(vx,vy);
}

//------------------------------------------------------------
void particle::update(){
	engine_frc *= .97;
	vel = vel + frc + engine_frc;
	pos = pos + vel;
	if(pos.x < 0){
		pos.x = ofGetWidth();
	} else if(pos.x > ofGetWidth()){
		pos.x = 0;
	} else if(pos.y < 0){
		pos.y = ofGetHeight();
	} else if(pos.y > ofGetHeight()){
		pos.y = 0;
	}
}

bool particle::checkTarget(int x, int y, int dist){
	if(pos.x > (x-dist) && pos.x < (x+dist) && pos.y > (y-dist) && pos.y < (y+dist)){
		return true;
	} else {
		return false;
	}
}

//------------------------------------------------------------
void particle::draw(){
	float engine_angle = atan2((0 - engine_frc.x), (0 - engine_frc.y))  * 180 / PI;
	//float avg_angle;
	//if(engine_frc.x > .05 || engine_frc.x < -.05 && engine_frc.y > .05 || engine_frc.y < -.05 ){
	//	avg_angle = (atan2(((0 - engine_frc.x) + (0 - vel.x)), ((0 - engine_frc.y) + (0 - vel.y)))) * 180 / 2;
	//} else {
	//	avg_angle = (atan2(((0 - engine_frc.x) + (0 - vel.x)), ((0 - engine_frc.y) + (0 - vel.y)))) * 180 / 2;
	//}
	glPushMatrix();
	glTranslatef(pos.x,pos.y,0);
	glRotatef(-engine_angle,0,0,1);
		ofSetColor(0xFFFFFF);
		car.draw(-13.5,-30);
	glPopMatrix();
}

void particle::drawDebug(){
	float angle = (atan2((0 - engine_frc.x), (0 - engine_frc.y))) * 180/PI;
	glPushMatrix();
	glTranslatef(pos.x,pos.y,0);
		ofCircle(0,0, 3);
		ofLine(0,0,vel.x*10,vel.y*10);
		ofSetColor(0xFF0000);
		ofLine(0,0,frc.x*10,frc.y*10);
		ofSetColor(0x00FF00);
		ofLine(0,0,engine_frc.x*10,engine_frc.y*10);
		ofSetColor(0x000000);
		ofDrawBitmapString(ofToString(angle),5,0);
	glPopMatrix();
	
}

//------------------------------------------------------------
void particle::bounceOffWalls(){
	
	// sometimes it makes sense to damped, when we hit
	bool bDampedOnCollision = true;
	bool bDidICollide = false;
	
	// what are the walls
	float minx = 0;
	float miny = 0;
	float maxx = ofGetWidth();
	float maxy = ofGetHeight();
	
	if (pos.x > maxx){
		pos.x = maxx; // move to the edge, (important!)
		vel.x *= -1;
		bDidICollide = true;
	} else if (pos.x < minx){
		pos.x = minx; // move to the edge, (important!)
		vel.x *= -1;
		bDidICollide = true;
	}
	
	if (pos.y > maxy){
		pos.y = maxy; // move to the edge, (important!)
		vel.y *= -1;
		bDidICollide = true;
	} else if (pos.y < miny){
		pos.y = miny; // move to the edge, (important!)
		vel.y *= -1;
		bDidICollide = true;
	}
	
	if (bDidICollide == true && bDampedOnCollision == true){
		vel *= 0.3;
	}
	
}
