#include "PhysicalObject.h"
#include "Model.h"

#include <btBulletDynamicsCommon.h>

//////////////////////////////////////////////////////////////
namespace Dubious {
namespace Physics {

PhysicalObject::PhysicalObject( ModelPtr pModel, float Mass, bool StartSleeping )
	: m_pModel( pModel )
{
	btVector3 localInertia(0,0,0);
	m_pModel->m_pShape->calculateLocalInertia( Mass, localInertia );

	btTransform Transform;
	Transform.setIdentity();

	btDefaultMotionState* DefaultMotionState = new btDefaultMotionState(Transform);
	btRigidBody::btRigidBodyConstructionInfo rbInfo( Mass, DefaultMotionState, m_pModel->m_pShape, localInertia);
	m_pBody = new btRigidBody( rbInfo );
	m_pBody->updateInertiaTensor();
	if (StartSleeping) {
		m_pBody->setActivationState( ISLAND_SLEEPING );
	}
}

PhysicalObject::~PhysicalObject()
{
	delete m_pBody->getMotionState();
	delete m_pBody;
}

//////////////////////////////////////////////////////////////
Math::CoordinateSpace PhysicalObject::GetCoordinateSpace() const
{
	return m_pBody->getWorldTransform();
}

void PhysicalObject::SetCoordinateSpace( const Math::CoordinateSpace& Coord )
{
	m_pBody->getWorldTransform() = Coord.Transform();
}

//////////////////////////////////////////////////////////////
void PhysicalObject::SetVelocity( const Math::Vector& V )
{
	m_pBody->setLinearVelocity( V.Vector() );
}

}
}
