//--------------------------------------------------------------------------------- // A simple physics simulation // Y is up. //--------------------------------------------------------------------------------- #include "TA/Physics/Physics.h" #include "TA/Physics/DynamicObject.h" #include "TA/Physics/StaticObject.h" #include "TA/Physics/CollisionObjectAABBMesh.h" const float k_fWorldExtent = 1000.0f; const float k_fGroundExtent = 10.0f; void main() { //--------------------------------------------------------------------------------- // Initialise physics. //--------------------------------------------------------------------------------- TA::Physics::CreateInstance(); // Make a reference to the physics instance for convienience. TA::Physics& physics = TA::Physics::GetInstance(); // Set the size of the world. TA::AABB aabb; aabb.Initialise( TA::Vec3(0.0f, 0.0f, 0.0f), // Center. TA::Vec3(k_fWorldExtent, k_fWorldExtent, k_fWorldExtent)); // Extent physics.SetWorldDimensions(aabb); // Set gravity. // Note: This call is unecessary since -9.81f is the default value. physics.SetGravity(TA::Vec3(0.0f, -9.81f, 0.0f)); // Initialise the simulation based on the settings set above. // All TA::Physics memory and data structures will be allocated // and initialised during this function call. // Note: True Axis calls this function automatically when needed. // In this case we choose to manually call this function // here to finish all TA::Physics initialisation in the // one spot. physics.SetupSimulation(); //--------------------------------------------------------------------------------- // Add a dynamic object. //--------------------------------------------------------------------------------- TA::DynamicObject* pDynamicObject = TA::DynamicObject::CreateNew(); aabb.Initialise( // Make a 1 by 1 by 1 box. TA::Vec3(0.0f, 0.0f, 0.0f), // Center. TA::Vec3(0.5f, 0.5f, 0.5f)); // Extent pDynamicObject->InitialiseAsABox(aabb); // Place the dynamic object pDynamicObject->SetPosition(TA::Vec3(0.0f, 2.0f, 0.0f)); // Add the dynamic object to the simulation physics.AddDynamicObject(pDynamicObject); //--------------------------------------------------------------------------------- // Add a static object for the ground. //--------------------------------------------------------------------------------- TA::StaticObject* pStaticObject = TA::StaticObject::CreateNew(); // Create a collision object to add to the static object. // NOTE: All collision on polygons is single sided. Front facing polygons have counter clockwise winding. TA::CollisionObjectAABBMesh* pStaticCollisionObject = TA::CollisionObjectAABBMesh::CreateNew(); pStaticCollisionObject->Initialise( 4, // Num vertices. 1, // Num polygons. 4); // Num polygon indices. pStaticCollisionObject->AddVertex(TA::Vec3(k_fGroundExtent, 0.0f, k_fGroundExtent)); pStaticCollisionObject->AddVertex(TA::Vec3(-k_fGroundExtent, 0.0f, k_fGroundExtent)); pStaticCollisionObject->AddVertex(TA::Vec3(-k_fGroundExtent, 0.0f, -k_fGroundExtent)); pStaticCollisionObject->AddVertex(TA::Vec3(k_fGroundExtent, 0.0f, -k_fGroundExtent)); int pnPolygonIndexList[4] = { 0, 1, 2, 3 }; pStaticCollisionObject->AddPolygon(4, pnPolygonIndexList); pStaticCollisionObject->FinishedAddingGeometry(); // Initialise the static object with the collision object. pStaticObject->Initialise(pStaticCollisionObject); pStaticCollisionObject->Release(); // We no long need the reference. pStaticCollisionObject = 0; // Add the static object to the simulation. physics.AddStaticObject(pStaticObject); pStaticObject->Release(); // We no long need the reference. pStaticObject = 0; //--------------------------------------------------------------------------------- // Other initialisation. //--------------------------------------------------------------------------------- //. //. //. //--------------------------------------------------------------------------------- // Main loop. //--------------------------------------------------------------------------------- for (;;) { float fDt = 1.0f / 60.0f; // Time of current frame // Caculate value for fDt //. //. //. // Update the simulation physics.Update(fDt); // Do game logic and render the scene //. //. //. // Use pDynamicObject->GetFrame() to render // the dynamic object. //. //. //. } //--------------------------------------------------------------------------------- // Finalise physics. //--------------------------------------------------------------------------------- TA::Physics::DestroyInstance(); pDynamicObject->Release(); pDynamicObject = 0; //--------------------------------------------------------------------------------- // Other finalisation. //--------------------------------------------------------------------------------- //. //. //. }