diff --git a/examples/fallingbox/Box.cpp b/examples/fallingbox/Box.cpp deleted file mode 100644 index 1ded020e..00000000 --- a/examples/fallingbox/Box.cpp +++ /dev/null @@ -1,87 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * -* Copyright (c) 2010 Daniel Chappuis * -********************************************************************************* -* * -* Permission is hereby granted, free of charge, to any person obtaining a copy * -* of this software and associated documentation files (the "Software"), to deal * -* in the Software without restriction, including without limitation the rights * -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * -* copies of the Software, and to permit persons to whom the Software is * -* furnished to do so, subject to the following conditions: * -* * -* The above copyright notice and this permission notice shall be included in * -* all copies or substantial portions of the Software. * -* * -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * -* THE SOFTWARE. * -********************************************************************************/ - -// Libraries -#include "Box.h" -#include - -// Use the ReactPhysics3D namespace -using namespace reactphysics3d; - -// Constructor -Box::Box(double size, double mass, Vector3D& position, rp3d::Quaternion& orientation) { - this->size = size; - - // Local inertia tensor of a cube - rp3d::Matrix3x3 inertiaTensor(1.0/12.0*mass*2*size*size, 0.0, 0.0, - 0.0, 1.0/12.0*mass*2*size*size, 0.0, - 0.0, 0.0, 1.0/12.0*mass*2*size*size); - - // Creation of the bounding volume for the collision - // The bounding volume is an Oriented Bounding Box (OBB) - // The first three arguments are the three axis direction of the OBB (here the x,y and z axis) - // and the last three arguments are the corresponding half extents of the OBB in those direction. - // Here the rigid body is a cube and therefore the three half extents are the half of the size - // of the cube in all OBB directions. - rp3d::OBB* boundingVolume = new OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0), size/2.0, size/2.0, size/2); - - // Create the rigid body that will be used to simulate the physics of the box - rigidBody = new RigidBody(position, orientation, mass, inertiaTensor, boundingVolume); -} - -// Destructor -Box::~Box() { - // Delete the physics body - delete rigidBody; -} - -// Draw the box -void Box::draw() const { - - // Get the current position of the rigid body (for animation you should use the - // getInterpolatedPosition() function instead of getPosition() - Vector3D position = rigidBody->getInterpolatedPosition(); - - // Get the current orientation of the rigid body (represented by a quaternion) - Quaternion orientation = rigidBody->getInterpolatedOrientation(); - - // Use the returned quaternion to get the rotation axis and rotation angle - Vector3D orientationAxis; - double orientationAngle; - orientation.getRotationAngleAxis(orientationAngle, orientationAxis); - - glPushMatrix(); - - // Translation of the box to its position - glTranslatef(position.getValue(0), position.getValue(1), position.getValue(2)); - - // Rotation of the box according to its orientation - glRotatef(orientationAngle/PI*180.0, orientationAxis.getX(), orientationAxis.getY(), orientationAxis.getZ()); - - // Draw the cube - glutSolidCube(size); - - glPopMatrix(); -} - diff --git a/examples/fallingbox/Box.h b/examples/fallingbox/Box.h deleted file mode 100644 index 07ffcbf8..00000000 --- a/examples/fallingbox/Box.h +++ /dev/null @@ -1,52 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * -* Copyright (c) 2010 Daniel Chappuis * -********************************************************************************* -* * -* Permission is hereby granted, free of charge, to any person obtaining a copy * -* of this software and associated documentation files (the "Software"), to deal * -* in the Software without restriction, including without limitation the rights * -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * -* copies of the Software, and to permit persons to whom the Software is * -* furnished to do so, subject to the following conditions: * -* * -* The above copyright notice and this permission notice shall be included in * -* all copies or substantial portions of the Software. * -* * -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * -* THE SOFTWARE. * -********************************************************************************/ - -// Libraries -#include - -#ifndef BOX_H -#define BOX_H - -class Box { - private: - double size; - double mass; - rp3d::RigidBody* rigidBody; - - public: - Box(double size, double mass, rp3d::Vector3D& position, rp3d::Quaternion& orientation); // Constructor - ~Box(); // Destructor - - rp3d::RigidBody* getRigidBodyPointer() const; // Return the pointer to the rigid body - - void draw() const; // Draw the box -}; - - // Return the pointer to the rigid body -inline rp3d::RigidBody* Box::getRigidBodyPointer() const { - return rigidBody; -} - -#endif - diff --git a/examples/fallingbox/main.cpp b/examples/fallingbox/main.cpp deleted file mode 100644 index 4c2f7246..00000000 --- a/examples/fallingbox/main.cpp +++ /dev/null @@ -1,194 +0,0 @@ -/******************************************************************************** -* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * -* Copyright (c) 2010 Daniel Chappuis * -********************************************************************************* -* * -* Permission is hereby granted, free of charge, to any person obtaining a copy * -* of this software and associated documentation files (the "Software"), to deal * -* in the Software without restriction, including without limitation the rights * -* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * -* copies of the Software, and to permit persons to whom the Software is * -* furnished to do so, subject to the following conditions: * -* * -* The above copyright notice and this permission notice shall be included in * -* all copies or substantial portions of the Software. * -* * -* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * -* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * -* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * -* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * -* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * -* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * -* THE SOFTWARE. * -********************************************************************************/ - -// Libraries -#include -#include -#include -#include "Box.h" -#include - -// Prototypes -void init(); -void display(); -void simulate(); -void clean(); -void reshape(int w, int h); - -// Use the ReactPhysics3D namespace -using namespace reactphysics3d; - -// Constants -const double FLOOR_SIZE = 20; -const double FLOOR_THICKNESS = 0.2; - -// Global variables -PhysicsWorld* physicsWorld; // Physics world -PhysicsEngine* physicsEngine; // Physics engine -Box* boxes[2]; // Falling boxes -RigidBody* floorRigidBody; // Rigid body corresponding the floor - - -// Simulation function -void simulate() { - - // Update the physics simulation - physicsEngine->update(); - - // Display the scene - display(); -} - -// Main function -int main(int argc, char** argv) { - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); - glutInitWindowSize(600, 600); - glutInitWindowPosition(100, 100); - glutCreateWindow("ReactPhysics3D Example - Falling Cubes"); - init(); - glutIdleFunc(simulate); - glutReshapeFunc(reshape); - glutMainLoop(); - physicsEngine->stop(); // Stop the physics simulation - clean(); - - return 0; -} - -// Initialization function -void init() { - glClearColor(0.0, 0.0, 0.0, 0.0); - - // Light - glShadeModel(GL_SMOOTH); - GLfloat light_position[] = {5.0f, 5.0f, 5.0f, 1.0f}; - glLightfv(GL_LIGHT0, GL_POSITION, light_position); - - glEnable(GL_LIGHTING); - glEnable(GL_LIGHT0); - glEnable(GL_DEPTH_TEST); - - // Gravity vector of the physics world - Vector3D gravity(0.0, -9.81, 0.0); - - // Create the physics world - physicsWorld = new PhysicsWorld(gravity); - - // Create the physics engine with the previous physis world and a timestep - // of 0.005 seconds - physicsEngine = new PhysicsEngine(physicsWorld, 0.005); - - // Create a falling box with a size of 1m and weight of 3kg - Vector3D position1(-2.0, 15.0, 0.0); // Position of the box - Quaternion orientation(0.2, 1.0, 0.6, 0.0); // Orientation of the box - boxes[0] = new Box(1.0, 3.0, position1, orientation); // Creation of the box - boxes[0]->getRigidBodyPointer()->setRestitution(0.5); // How bouncy is the rigid body - physicsWorld->addBody(boxes[0]->getRigidBodyPointer()); // Add the rigid body created in the constructor - // of the Box to the physics world - - - // Create a second falling box with a size of 2m and weight of 4.5kg - Vector3D position2 = Vector3D(2.0, 10.0, 0.0); // Position of the box - orientation = Quaternion(1.0, 1.0, 0.5, 0.0); // Orientation of the box - boxes[1] = new Box(2.0, 4.5, position2, orientation); // Creation of the box - boxes[1]->getRigidBodyPointer()->setRestitution(0.5); // How bouncy is the rigid body - physicsWorld->addBody(boxes[1]->getRigidBodyPointer()); // Add the rigid body created in the constructor - // of the Box to the physics world - - // Create the rigid body corresponding to the floor - Vector3D positionFloor = Vector3D(0.0, 0.0, 0.0); - orientation = Quaternion(0.0, 1.0, 0.0, 0.0); - double mass = 100.0; - - // Local inertia tensor of a cube - rp3d::Matrix3x3 inertiaTensor(1.0/12.0*mass*2*FLOOR_SIZE*FLOOR_SIZE, 0.0, 0.0, - 0.0, 1.0/12.0*mass*2*FLOOR_SIZE*FLOOR_SIZE, 0.0, - 0.0, 0.0, 1.0/12.0*mass*2*FLOOR_SIZE*FLOOR_SIZE); - - // Creation of the bounding volume for the collision - // The bounding volume is an Oriented Bounding Box (OBB) - // The first three arguments are the three axis direction of the OBB (here the x,y and z axis) - // and the last three arguments are the corresponding half extents of the OBB in those direction. - // Here the rigid body is a cube and therefore the three half extents are the half of the size - // of the cube in all OBB directions. - rp3d::OBB* boundingVolume = new OBB(positionFloor, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0), FLOOR_SIZE, FLOOR_THICKNESS, FLOOR_SIZE); - - // Create the rigid body that will be used to simulate the physics of the box - floorRigidBody = new RigidBody(positionFloor, orientation, mass, inertiaTensor, boundingVolume); - - // The floor is a rigid body that cannot move - floorRigidBody->setIsMotionEnabled(false); - - // Set the bouncing restitution factor of the floor - floorRigidBody->setRestitution(0.5); - - // Add the floor rigid body to the physics world - physicsWorld->addBody(floorRigidBody); - - // Start the physics simulation - physicsEngine->start(); -} - -// Display function -void display() { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); - - // Display each falling box of the scene - for (int i=0; i<2; i++) { - boxes[i]->draw(); - } - - // Display the plane for the floor - glBegin(GL_POLYGON); - glNormal3f(0.0, 1.0, 0.0); - glVertex3f(-FLOOR_SIZE/2, 0.0, -FLOOR_SIZE/2); - glVertex3f(-FLOOR_SIZE/2, 0.0, FLOOR_SIZE/2); - glVertex3f(FLOOR_SIZE/2, 0.0, FLOOR_SIZE/2); - glVertex3f(FLOOR_SIZE/2, 0.0, -FLOOR_SIZE/2); - glEnd(); - - glutSwapBuffers(); -} - -// Reshape function -void reshape(int w, int h) { - float ratio = ((float)w / h); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glViewport(0, 0, w, h); - gluPerspective(45, ratio,1,1000); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - gluLookAt(20.0, 5.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); -} - -// Clean the memory allocation -void clean() { - delete physicsEngine; - delete physicsWorld; - delete boxes[0]; - delete boxes[1]; - delete floorRigidBody; -} \ No newline at end of file diff --git a/examples/fallingcubes/Box.cpp b/examples/fallingcubes/Box.cpp new file mode 100644 index 00000000..6aa3a675 --- /dev/null +++ b/examples/fallingcubes/Box.cpp @@ -0,0 +1,63 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * +* Copyright (c) 2010-2013 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +// Libraries +#include "Box.h" + +// Use the ReactPhysics3D namespace +using namespace reactphysics3d; + +// Constructor +Box::Box(double size, RigidBody* rigidBody) { + mSize = size; + mRigidBody = rigidBody; +} + +// Destructor +Box::~Box() { + +} + +// Draw the box +void Box::draw() const { + + // Get the interpolated transform of the rigid body + Transform transform = mRigidBody->getInterpolatedTransform(); + + // Get the corresponding OpenGL matrix + float openGLMatrix[16]; + transform.getOpenGLMatrix(openGLMatrix); + + glPushMatrix(); + + // Multiply by the OpenGL transform matrix + glMultMatrixf(openGLMatrix); + + // Draw the cube + glutSolidCube(mSize); + + glPopMatrix(); +} + diff --git a/examples/fallingcubes/Box.h b/examples/fallingcubes/Box.h new file mode 100644 index 00000000..e8b9bd98 --- /dev/null +++ b/examples/fallingcubes/Box.h @@ -0,0 +1,82 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * +* Copyright (c) 2010-2013 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +// Libraries +#include +#ifdef APPLE_OS + #include +#else + #include +#endif +#ifdef USE_FREEGLUT + #ifdef APPLE_OS + #include + #else + #include + #endif +#else + #ifdef APPLE_OS + #include + #else + #include + #endif +#endif + +#ifndef BOX_H +#define BOX_H + +/// This class represents a cube box +class Box { + + private: + + /// Size of the box + double mSize; + + /// Pointer to the rigid body associated to the box + rp3d::RigidBody* mRigidBody; + + public: + + /// Constructor + Box(double size, rp3d::RigidBody* rigidBody); + + /// Destructor + ~Box(); + + /// Return the pointer to the rigid body + rp3d::RigidBody* getRigidBodyPointer() const; + + /// Draw the box + void draw() const; +}; + +// Return the pointer to the rigid body +inline rp3d::RigidBody* Box::getRigidBodyPointer() const { + return mRigidBody; +} + +#endif + diff --git a/examples/fallingbox/CMakeLists.txt b/examples/fallingcubes/CMakeLists.txt similarity index 78% rename from examples/fallingbox/CMakeLists.txt rename to examples/fallingcubes/CMakeLists.txt index 8efa2d44..b8d87280 100755 --- a/examples/fallingbox/CMakeLists.txt +++ b/examples/fallingcubes/CMakeLists.txt @@ -4,14 +4,9 @@ cmake_minimum_required(VERSION 2.6) # Project configuration PROJECT(FallingCubes) -# Headers -INCLUDE_DIRECTORIES(${REACTPHYSICS3D_SOURCE_DIR}/src) - # Find Glut or Freeglut -if(WIN32 OR APPLE) - # Find the necessary libraries - FIND_PACKAGE(GLUT) -else() +FIND_PACKAGE(GLUT) +if (NOT GLUT_FOUND) # Find the Freeglut library FIND_PATH(FREEGLUT_INCLUDE_DIR NAMES GL/freeglut.h /usr/include/GL @@ -38,12 +33,23 @@ else() MESSAGE("OpenGL not found") endif() +# Headers +if(GLUT_FOUND) + INCLUDE_DIRECTORIES(${REACTPHYSICS3D_SOURCE_DIR}/src ${GLUT_INCLUDE_DIR}) +elseif(FREEGLUT_FOUND) + INCLUDE_DIRECTORIES(${REACTPHYSICS3D_SOURCE_DIR}/src ${FREEGLUT_INCLUDE_DIRS}) +endif() + +# Definitions +if (FREEGLUT_FOUND) + add_definition(-DUSE_FREEGLUT) +endif() # Create the example executable using the # compiled reactphysics3d static library ADD_EXECUTABLE(fallingcubes main.cpp Box.cpp Box.h) -if(WIN32 OR APPLE) +if(GLUT_FOUND) TARGET_LINK_LIBRARIES(fallingcubes reactphysics3d ${GLUT_LIBRARY} ${OPENGL_LIBRARY}) elseif(FREEGLUT_FOUND) TARGET_LINK_LIBRARIES(fallingcubes reactphysics3d ${FREEGLUT_LIBRARY} ${OPENGL_LIBRARY}) diff --git a/examples/fallingcubes/main.cpp b/examples/fallingcubes/main.cpp new file mode 100644 index 00000000..c3f05100 --- /dev/null +++ b/examples/fallingcubes/main.cpp @@ -0,0 +1,248 @@ +/******************************************************************************** +* ReactPhysics3D physics library, http://code.google.com/p/reactphysics3d/ * +* Copyright (c) 2010-2013 Daniel Chappuis * +********************************************************************************* +* * +* This software is provided 'as-is', without any express or implied warranty. * +* In no event will the authors be held liable for any damages arising from the * +* use of this software. * +* * +* Permission is granted to anyone to use this software for any purpose, * +* including commercial applications, and to alter it and redistribute it * +* freely, subject to the following restrictions: * +* * +* 1. The origin of this software must not be misrepresented; you must not claim * +* that you wrote the original software. If you use this software in a * +* product, an acknowledgment in the product documentation would be * +* appreciated but is not required. * +* * +* 2. Altered source versions must be plainly marked as such, and must not be * +* misrepresented as being the original software. * +* * +* 3. This notice may not be removed or altered from any source distribution. * +* * +********************************************************************************/ + +// Libraries +#include +#include +#include "Box.h" + +// Prototypes +void init(); +void display(); +void simulate(); +void clean(); +void reshape(int w, int h); + +// Use the ReactPhysics3D namespace +using namespace reactphysics3d; + +// Constants +const double FLOOR_SIZE = 20; +const double FLOOR_THICKNESS = 0.02; + +// Global variables +DynamicsWorld* dynamicsWorld; // Dynamics world +Box* boxes[2]; // Falling boxes +BoxShape* collisionShapeBox1; // Collision shape of the first box +BoxShape* collisionShapeBox2; // Collision shape of the second box +BoxShape* collisionShapeFloor; // Collision shape of the floor +RigidBody* floorRigidBody; // Rigid body corresponding the floor + + +// Simulation function +void simulate() { + + // Update the physics simulation + dynamicsWorld->update(); + + // Display the scene + display(); +} + +// Main function +int main(int argc, char** argv) { + + // Initialize GLUT + glutInit(&argc, argv); + glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH); + glutInitWindowSize(800, 600); + glutInitWindowPosition(100, 100); + glutCreateWindow("ReactPhysics3D Example - Falling Cubes"); + + init(); + + glutIdleFunc(simulate); + glutDisplayFunc(display); + glutReshapeFunc(reshape); + glutMainLoop(); + + // Stop the physics simulation + dynamicsWorld->stop(); + + clean(); + + return 0; +} + +// Initialization function +void init() { + + glClearColor(0.0, 0.0, 0.0, 0.0); + + // Light + glShadeModel(GL_SMOOTH); + GLfloat light_position[] = {5.0f, 5.0f, 5.0f, 1.0f}; + glLightfv(GL_LIGHT0, GL_POSITION, light_position); + + glEnable(GL_LIGHTING); + glEnable(GL_LIGHT0); + glEnable(GL_DEPTH_TEST); + + // Gravity vector of the physics world + Vector3 gravity(0.0, -9.81, 0.0); + + // Timestep of the simulation + decimal timeStep = 1.0/60.0; + + // Create the dynamics world + dynamicsWorld = new DynamicsWorld(gravity, timeStep); + + // --- Create a falling box with a size of 1m and weight of 3kg --- // + + float size = 1.0f; + + // Initial position and orientation of the box + Vector3 positionBox1(-2.0f, 7.0f, 0.0f); + Quaternion orientationBox1(0.3, 1.0, 0.8, 0.0); + Transform initTransform(positionBox1, orientationBox1); + + // Create a box collision shape for the box (used for collision detection) + collisionShapeBox1 = new BoxShape(Vector3(size/2.0f, size/2.0f, size/2.0f)); + + // Compute the inertia tensor of the box using the collision shape + Matrix3x3 inertiaTensorBox1; + float massBox1 = 3.0f; + collisionShapeBox1->computeLocalInertiaTensor(inertiaTensorBox1, massBox1); + + // Create the rigid body associated with the box in the dynamics world + RigidBody* rigidBody = dynamicsWorld->createRigidBody(initTransform, massBox1, + inertiaTensorBox1, collisionShapeBox1); + + // Set the contact velocity restitution factor of the rigid body + rigidBody->setRestitution(0.5f); + + // Create the box object (used for display) + boxes[0] = new Box(size, rigidBody); + + // --- Create a second falling box with a size of 1.5m and weight of 4.5kg --- // + + size = 1.5; + + // Initial position and orientation of the box + Vector3 positionBox2(2.0, 4.0, 0.0); + Quaternion orientationBox2(1.0, 1.0, 0.5, 0.0); + Transform initTransform2(positionBox2, orientationBox2); + + // Create a box collision shape for the box (used for collision detection) + collisionShapeBox2 = new BoxShape(Vector3(size/2.0f, size/2.0f, size/2.0f)); + + // Compute the inertia tensor using the collision shape + Matrix3x3 inertiaTensorBox2; + float massBox2 = 4.5f; + collisionShapeBox2->computeLocalInertiaTensor(inertiaTensorBox2, massBox2); + + // Create the rigid body associated with the box in the dynamcis world + RigidBody* rigidBody2 = dynamicsWorld->createRigidBody(initTransform2, massBox2, + inertiaTensorBox2, collisionShapeBox2); + + // Set the contact velocity restitution factor of the rigid body + rigidBody2->setRestitution(0.5); + + // Create the box object (used for display) + boxes[1] = new Box(size, rigidBody2); + + // --- Create the rigid body corresponding to the floor --- // + + // Initial position and orientation of the floor + Vector3 positionFloor(0.0, 0.0, 0.0); + Quaternion orientationFloor(0.0, 1.0, 0.0, 0.0); + Transform initTransformFloor(positionFloor, orientationFloor); + + // Create a box collision shape for the floor (used for collision detection) + collisionShapeFloor = new BoxShape(Vector3(FLOOR_SIZE, FLOOR_THICKNESS, FLOOR_SIZE)); + + // Compute the inertia tensor of the floor using the collision shape + float massFloor = 100.0f; + rp3d::Matrix3x3 inertiaTensorFloor; + collisionShapeFloor->computeLocalInertiaTensor(inertiaTensorFloor, massFloor); + + // Create the rigid body associated with the floor in the dynamcis world + floorRigidBody = dynamicsWorld->createRigidBody(initTransformFloor, massFloor, + inertiaTensorFloor, collisionShapeFloor); + + // The floor is a rigid body that cannot move + floorRigidBody->setIsMotionEnabled(false); + + // Set the contact velocity restitution factor of the floor + floorRigidBody->setRestitution(0.5); + + // Start the dynamics simulation + dynamicsWorld->start(); +} + +// Display function +void display() { + + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + + // Display each falling box of the scene + for (int i=0; i<2; i++) { + boxes[i]->draw(); + } + + // Display the plane for the floor + glBegin(GL_POLYGON); + glNormal3f(0.0, 1.0, 0.0); + glVertex3f(-FLOOR_SIZE/2, 0.0, -FLOOR_SIZE/2); + glVertex3f(-FLOOR_SIZE/2, 0.0, FLOOR_SIZE/2); + glVertex3f(FLOOR_SIZE/2, 0.0, FLOOR_SIZE/2); + glVertex3f(FLOOR_SIZE/2, 0.0, -FLOOR_SIZE/2); + glEnd(); + + glutSwapBuffers(); +} + +// Reshape function +void reshape(int w, int h) { + float ratio = ((float)w / h); + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glViewport(0, 0, w, h); + gluPerspective(45, ratio,1,1000); + glMatrixMode(GL_MODELVIEW); + glLoadIdentity(); + gluLookAt(20.0, 4.0, 20.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); +} + +// Clean the memory allocation +void clean() { + + // Destroy the rigid bodies from the dynamics world + dynamicsWorld->destroyRigidBody(boxes[0]->getRigidBodyPointer()); + dynamicsWorld->destroyRigidBody(boxes[1]->getRigidBodyPointer()); + dynamicsWorld->destroyRigidBody(floorRigidBody); + + // Destroy the dynamics world + delete dynamicsWorld; + + // Destroy the boxes + delete boxes[0]; + delete boxes[1]; + + // Destroy the collision shapes + delete collisionShapeBox1; + delete collisionShapeBox2; + delete collisionShapeFloor; +}