#include "IPhysicsBodyModifier.h"

IPhysicsBodyModifier::IPhysicsBodyModifier()
{

}

IPhysicsBodyModifier::~IPhysicsBodyModifier()
{
	NewtonDestroyBody(getWorld(), m_body);
}

void IPhysicsBodyModifier::setTransformCallback(NewtonSetTransform callback)
{
	NewtonBodySetTransformCallback(m_body, callback);
}

void IPhysicsBodyModifier::setAutoactiveCallback(NewtonBodyActivationState callback)
{
	NewtonBodySetAutoactiveCallback(m_body, callback);
}

void IPhysicsBodyModifier::setForceAndTorqueCallback(NewtonApplyForceAndTorque callback)
{
	NewtonBodySetForceAndTorqueCallback(m_body, callback);
}

void IPhysicsBodyModifier::setDestructorCallback(NewtonBodyDestructor callback)
{
	NewtonBodySetDestructorCallback(m_body, callback);
}

NewtonApplyForceAndTorque IPhysicsBodyModifier::getForceAndTorqueCallback()
{
	return NewtonBodyGetForceAndTorqueCallback(m_body);
}

void IPhysicsBodyModifier::setMassMatrix(f32 mass, vector3df inertia)
{
	NewtonBodySetMassMatrix(m_body, mass, inertia.X, inertia.Y, inertia.Z);
}

f32 IPhysicsBodyModifier::getMass()
{
	f32 mass;
	NewtonBodyGetMassMatrix(m_body, &mass, NULL, NULL, NULL);
	return mass;
}

vector3df IPhysicsBodyModifier::getInertia()
{
	vector3df inertia;
	NewtonBodyGetMassMatrix(m_body, NULL, &inertia.X, &inertia.Y, &inertia.Z);
	return inertia;
}

f32 IPhysicsBodyModifier::getInvMass()
{
	f32 mass;
	NewtonBodyGetInvMass(m_body, &mass, NULL, NULL, NULL);
	return mass;
}

vector3df IPhysicsBodyModifier::getInvInertia()
{
	vector3df inertia;
	NewtonBodyGetInvMass(m_body, NULL, &inertia.X, &inertia.Y, &inertia.Z);
	return inertia;
}


void IPhysicsBodyModifier::setMatrix(matrix4 matrix)
{
	NewtonBodySetMatrix(m_body, matrix.pointer()); // Irrlicht 1.3
}

matrix4 IPhysicsBodyModifier::getMatrix()
{
	matrix4 matrix;
	NewtonBodySetMatrix(m_body, matrix.pointer()); // Irrlicht 1.3
	return matrix;
}


void IPhysicsBodyModifier::setCenterOfMass(vector3df center)
{
	NewtonBodySetCentreOfMass(m_body, &center.X);
}

vector3df IPhysicsBodyModifier::getCenterOfMass()
{
	vector3df center;
	NewtonBodyGetCentreOfMass(m_body, &center.X);
	return center;
}


void IPhysicsBodyModifier::setMatrixRecursive(matrix4 matrix)
{
	NewtonBodySetMatrixRecursive(m_body, matrix.pointer()); // Irrlicht 1.3
}


void IPhysicsBodyModifier::setForce(vector3df force)
{
	forceSet = force;
}

void IPhysicsBodyModifier::addForce(vector3df force)
{
	forceAdd = force;
}

vector3df IPhysicsBodyModifier::getForce()
{
	vector3df force;
	NewtonBodyGetForce(m_body, &force.X);
	return force;
}


void IPhysicsBodyModifier::setTorque(vector3df torque)
{
	torqueSet = torque;
}

void IPhysicsBodyModifier::addTorque(vector3df torque)
{
	torqueAdd = torque;
}

vector3df IPhysicsBodyModifier::getTorque()
{
	vector3df torque;
	NewtonBodyGetTorque(m_body, &torque.X);
	return torque;
}


void IPhysicsBodyModifier::setCollision(NewtonCollision* collision)
{
	NewtonBodySetCollision(m_body, collision);
}

NewtonCollision* IPhysicsBodyModifier::getCollision()
{
	return NewtonBodyGetCollision(m_body);
}


void IPhysicsBodyModifier::enableCoriolisForcesMode(bool mode)
{
	NewtonBodyCoriolisForcesMode(m_body, mode);
}


NewtonWorld* IPhysicsBodyModifier::getWorld()
{
	return NewtonBodyGetWorld(m_body);
}


void IPhysicsBodyModifier::setMaterial(int material)
{
	NewtonBodySetMaterialGroupID(m_body, material);
}

int IPhysicsBodyModifier::getMaterial()
{
	return NewtonBodyGetMaterialGroupID(m_body);
}


void IPhysicsBodyModifier::setContinuousCollisionMode(bool state)
{
	NewtonBodySetContinuousCollisionMode(m_body, state);
}

bool IPhysicsBodyModifier::getContinuousCollisionMode()
{
	return NewtonBodyGetContinuousCollisionMode(m_body)==1 ? true : false;
}


void IPhysicsBodyModifier::setJointRecursiveCollision(bool state)
{
	NewtonBodySetJointRecursiveCollision(m_body, state);
}

bool IPhysicsBodyModifier::getJointRecursiveCollision()
{
	return NewtonBodyGetJointRecursiveCollision(m_body) == 1 ? true : false;
}


void IPhysicsBodyModifier::setAutofreeze(bool state)
{
	NewtonBodySetAutoFreeze(m_body, state);
}

bool IPhysicsBodyModifier::getAutofreeze()
{
	return NewtonBodyGetAutoFreeze(m_body) == 1 ? true : false;
}


bool IPhysicsBodyModifier::getSleepingState()
{
	return NewtonBodyGetSleepingState(m_body) == 1 ? true : false;
}


void IPhysicsBodyModifier::setFreezeTreshold(f32 freezeSpeed, f32 freezeOmega, int framesCount)
{
	NewtonBodySetFreezeTreshold(m_body, freezeSpeed, freezeOmega, framesCount);
}

f32 IPhysicsBodyModifier::getFreezeSpeed()
{
	f32 speed;
	NewtonBodyGetFreezeTreshold(m_body, &speed, NULL);
	return speed;
}

f32 IPhysicsBodyModifier::getFreezeOmega()
{
	f32 omega;
	NewtonBodyGetFreezeTreshold(m_body, NULL, &omega);
	return omega;
}


aabbox3d<f32> IPhysicsBodyModifier::getAABB()
{
	vector3df min, max;
	NewtonBodyGetAABB(m_body, &min.X, &max.X);
	return aabbox3d<f32>(min,max);
}


void IPhysicsBodyModifier::setVelocity(vector3df velocity)
{
	NewtonBodySetVelocity(m_body, &velocity.X);
}

vector3df IPhysicsBodyModifier::getVelocity()
{
	vector3df velocity;
	NewtonBodyGetVelocity(m_body, &velocity.X);
	return velocity;
}


void IPhysicsBodyModifier::setOmega(vector3df omega)
{
	NewtonBodySetOmega(m_body, &omega.X);
}

vector3df IPhysicsBodyModifier::getOmega()
{
	vector3df omega;
	NewtonBodyGetOmega(m_body, &omega.X);
	return omega;
}


void IPhysicsBodyModifier::setLinearDamping(f32 linearDamp)
{
	NewtonBodySetLinearDamping(m_body, linearDamp);
}

f32 IPhysicsBodyModifier::getLinearDamping()
{
	return NewtonBodyGetLinearDamping(m_body);
}


void IPhysicsBodyModifier::setAngularDamping(vector3df angularDamp)
{
	NewtonBodySetAngularDamping(m_body, &angularDamp.X);
}

vector3df IPhysicsBodyModifier::getAngularDamping()
{
	vector3df angularDamp;
	NewtonBodyGetAngularDamping(m_body, &angularDamp.X);
	return angularDamp;
}


void IPhysicsBodyModifier::forEachPolygonDo(NewtonCollisionIterator callback)
{
	NewtonBodyForEachPolygonDo(m_body, callback);
}


void IPhysicsBodyModifier::addBodyImpulse(vector3df pointDeltaVeloc, vector3df pointPosit)
{
	NewtonAddBodyImpulse(m_body, &pointDeltaVeloc.X, &pointPosit.X);
}
