git-svn-id: https://reactphysics3d.googlecode.com/svn/trunk@363 92aac97c-a6ce-11dd-a772-7fcde58d38e6

This commit is contained in:
chappuis.daniel 2010-07-19 19:42:47 +00:00
parent 35d95ddaeb
commit 7f4c6833cb
6 changed files with 184 additions and 149 deletions

View File

@ -29,19 +29,49 @@ using namespace reactphysics3d;
// Constructor of the class Context // Constructor of the class Context
Context::Context() { Context::Context() {
Cube* cube1 = new Cube(Vector3D(5.0, 13.0, 1), Quaternion(1.0, 1.0, 0.0, 0.0), 4.0, Kilogram(3.0)); /*
Cube* cube2 = new Cube(Vector3D(5.0, 13.0, 9), Quaternion(0.5, 0.5, 0.5, 0.0), 4.0, Kilogram(3.0)); Cube* cube1 = new Cube(Vector3D(0.0, 10.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 3.0, Kilogram(1.0));
cube1->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.5)); Cube* cube2 = new Cube(Vector3D(0.0, 0.0, 0.0), Quaternion(1.0, 1.0, 0.0, 0.0), 3.0, Kilogram(1.0));
cube2->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, -0.5)); //cube1->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.0));
//cube2->getRigidBody()->setLinearVelocity(Vector3D(0.0, 0.0, 0.0));
cube2->getRigidBody()->setIsMotionEnabled(false);
cube1->getRigidBody()->setRestitution(0.5);
cube2->getRigidBody()->setRestitution(0.5);
*/
//Cube* cube2 = new Cube(Vector3D(0.0, 17, 8.0), Quaternion(0.0, 1.0, 0.0, 0.0), 3.0, Kilogram(2.0));
//Cube* cube3 = new Cube(Vector3D(4.0, 17, -2.0), Quaternion(0.0, 1.0, 0.0, 0.0), 2.0, Kilogram(11.0)); for (int i=20; i>1; i=i-3) {
//Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.0, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), Kilogram(10.0)); Cube* cube = new Cube(Vector3D(3.0, i, i*0.2), Quaternion(1.0, 1.0, 0.0, 0.0), 2.0, Kilogram(1.0));
cube->getRigidBody()->setRestitution(0.7);
addObject(cube);
}
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.0 , 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), Kilogram(1.0));
plane1->getRigidBody()->setRestitution(0.7);
plane1->getRigidBody()->setIsMotionEnabled(false);
addObject(plane1);
/*
Cube* cube1 = new Cube(Vector3D(4.0, 11.0, 5.0), Quaternion(1.0, 0.3, 0.8, 0.0), 2.0, Kilogram(3.0));
//Cube* cube2 = new Cube(Vector3D(3.0, 9, 3.0), Quaternion(1.0, 1.0, 0.0, 0.0), 2.0, Kilogram(2.0));
cube1->getRigidBody()->setRestitution(0.4);
//cube2->getRigidBody()->setRestitution(0.4);
//Cube* cube3 = new Cube(Vector3D(5.0, 13, 0.0), Quaternion(1.0, 1.0, 0.3, 0.0), 2.0, Kilogram(1.0));
//cube3->getRigidBody()->setRestitution(0.8);
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), Quaternion(0.0, 1.0, 0.2, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), Kilogram(1.0));
plane1->getRigidBody()->setRestitution(0.4);
plane1->getRigidBody()->setIsMotionEnabled(false);
addObject(cube1); addObject(cube1);
addObject(cube2); //addObject(cube2);
//addObject(cube3); //addObject(cube3);
//addObject(plane1); addObject(plane1);
*/
} }

View File

@ -16,56 +16,56 @@
* You should have received a copy of the GNU Lesser General Public License * * You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
// Libraries // Libraries
#include "Objects.h" #include "Objects.h"
//#include <windows.h> // To avoid an error due to the #include <GL/glut.h> //#include <windows.h> // To avoid an error due to the #include <GL/glut.h>
#include <GL/freeglut.h> #include <GL/freeglut.h>
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
#include <cmath> #include <cmath>
#include <iostream> #include <iostream>
// ----- Class Object ----- // // ----- Class Object ----- //
// Constructor of the class Object // Constructor of the class Object
Object::Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb) Object::Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb)
:rigidBody(new RigidBody(position, orientation, mass, inertiaTensor, obb)) { :rigidBody(new RigidBody(position, orientation, mass, inertiaTensor, obb)) {
} }
// Destructor of the class Object // Destructor of the class Object
Object::~Object() { Object::~Object() {
// Delete the rigid body object // Delete the rigid body object
delete rigidBody; delete rigidBody;
} }
// Return the pointer to the rigid body // Return the pointer to the rigid body
RigidBody* Object::getRigidBody() { RigidBody* Object::getRigidBody() {
return rigidBody; return rigidBody;
} }
// ----- Class Cube ----- // // ----- Class Cube ----- //
// Static attributes // Static attributes
const Matrix3x3 Cube::inertiaTensor; const Matrix3x3 Cube::inertiaTensor;
// Constructor of the class Cube // Constructor of the class Cube
Cube::Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass) Cube::Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass)
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*2*size*size, 0.0, 0.0, :Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*2*size*size, 0.0, 0.0,
0.0, 1.0/12.0*mass.getValue()*2*size*size, 0.0, 0.0, 1.0/12.0*mass.getValue()*2*size*size, 0.0,
0.0, 0.0, 1.0/12.0*mass.getValue()*2*size*size), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0), 0.0, 0.0, 1.0/12.0*mass.getValue()*2*size*size), 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, size/2, size/2)) { size/2.0, size/2.0, size/2)) {
this->size = size; this->size = size;
} }
// Destructor of the classe Cube // Destructor of the classe Cube
Cube::~Cube() { Cube::~Cube() {
} }
// Method to draw the cube // Method to draw the cube
void Cube::draw() const { void Cube::draw() const {
// Get the interpolated state of the rigid body // Get the interpolated state of the rigid body
@ -80,44 +80,44 @@ void Cube::draw() const {
Vector3D orientationAxis; Vector3D orientationAxis;
double orientationAngle; double orientationAngle;
state.getOrientation().getRotationAngleAxis(orientationAngle, orientationAxis); state.getOrientation().getRotationAngleAxis(orientationAngle, orientationAxis);
// Translation of the cube to its position // Translation of the cube to its position
glTranslatef(x, y, z); glTranslatef(x, y, z);
// Rotation of the cube according to its orientation // Rotation of the cube according to its orientation
glRotatef(orientationAngle/PI*180.0, orientationAxis.getX(), orientationAxis.getY(), orientationAxis.getZ()); glRotatef(orientationAngle/PI*180.0, orientationAxis.getX(), orientationAxis.getY(), orientationAxis.getZ());
// Draw the cube // Draw the cube
glutSolidCube(size); glutSolidCube(size);
} }
// ----- Class Plane ----- // // ----- Class Plane ----- //
// Constructor of the class Plane // Constructor of the class Plane
Plane::Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass) Plane::Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass)
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*height*height, 0.0, 0.0, :Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*height*height, 0.0, 0.0,
0.0, 1.0/12.0*mass.getValue()*(width*width+height*height), 0.0, 0.0, 1.0/12.0*mass.getValue()*(width*width+height*height), 0.0,
0.0, 0.0, 1.0/12.0*mass.getValue()*width*width), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0), 0.0, 0.0, 1.0/12.0*mass.getValue()*width*width), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
width/2, 0.5, height/2)) { // TODO : Change the height of the OBB width/2, 0.5, height/2)) { // TODO : Change the height of the OBB
this->width = width; this->width = width;
this->height = height; this->height = height;
this->d1 = d1; this->d1 = d1;
this->d2 = d2; this->d2 = d2;
// By default Planes in the demo cannot move // By default Planes in the demo cannot move
rigidBody->setIsMotionEnabled(false); rigidBody->setIsMotionEnabled(false);
// Compute the unit normal vector of the plane by a cross product // Compute the unit normal vector of the plane by a cross product
normalVector = d1.crossProduct(d2).getUnit(); normalVector = d1.crossProduct(d2).getUnit();
} }
// Destructor of the class Plane // Destructor of the class Plane
Plane::~Plane() { Plane::~Plane() {
} }
// Method used to draw the plane // Method used to draw the plane
void Plane::draw() const { void Plane::draw() const {
// Get the interpolated state of the rigid body // Get the interpolated state of the rigid body
@ -127,26 +127,26 @@ void Plane::draw() const {
double x = state.getPosition().getX(); double x = state.getPosition().getX();
double y = state.getPosition().getY(); double y = state.getPosition().getY();
double z = state.getPosition().getZ(); double z = state.getPosition().getZ();
// Translation of the cube to its position // Translation of the cube to its position
glTranslatef(x, y, z); glTranslatef(x, y, z);
float halfWidth = width / 2.0; float halfWidth = width / 2.0;
float halfHeight = height / 2.0; float halfHeight = height / 2.0;
// Draw the plane // Draw the plane
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0); glColor3f(1.0, 1.0, 1.0);
glVertex3f(x + d1.getX() * halfWidth + d2.getX() * halfHeight , y + d1.getY() * halfWidth + d2.getY() * halfHeight glVertex3f(x + d1.getX() * halfWidth + d2.getX() * halfHeight , y + d1.getY() * halfWidth + d2.getY() * halfHeight
, z + d1.getZ() * halfWidth + d2.getZ() * halfHeight); , z + d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(x + d1.getX() * halfWidth - d2.getX() * halfHeight , y + d1.getY() * halfWidth - d2.getY() * halfHeight glVertex3f(x + d1.getX() * halfWidth - d2.getX() * halfHeight , y + d1.getY() * halfWidth - d2.getY() * halfHeight
, z + d1.getZ() * halfWidth - d2.getZ() * halfHeight); , z + d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(x - d1.getX() * halfWidth - d2.getX() * halfHeight , y - d1.getY() * halfWidth - d2.getY() * halfHeight glVertex3f(x - d1.getX() * halfWidth - d2.getX() * halfHeight , y - d1.getY() * halfWidth - d2.getY() * halfHeight
, z - d1.getZ() * halfWidth - d2.getZ() * halfHeight); , z - d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ()); glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(x - d1.getX() * halfWidth + d2.getX() * halfHeight , y - d1.getY() * halfWidth + d2.getY() * halfHeight glVertex3f(x - d1.getX() * halfWidth + d2.getX() * halfHeight , y - d1.getY() * halfWidth + d2.getY() * halfHeight
, z - d1.getZ() * halfWidth + d2.getZ() * halfHeight); , z - d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glEnd(); glEnd();
} }

View File

@ -17,60 +17,60 @@
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. * * along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/ ***************************************************************************/
#ifndef OBJECTS_H #ifndef OBJECTS_H
#define OBJECTS_H #define OBJECTS_H
// Libraries // Libraries
#include "../reactphysics3d/reactphysics3d.h" #include "../reactphysics3d/reactphysics3d.h"
// Namespaces // Namespaces
using namespace reactphysics3d; using namespace reactphysics3d;
/* /*
Here we define all the objects that can appear in the simulation like cube, sphere, plane, ... Here we define all the objects that can appear in the simulation like cube, sphere, plane, ...
*/ */
// ----- Class Object (abstract) ----- // // ----- Class Object (abstract) ----- //
// Represent an object of the simulation // Represent an object of the simulation
class Object { class Object {
protected : protected :
RigidBody* rigidBody; // Rigid Body that represents the object RigidBody* rigidBody; // Rigid Body that represents the object
public : public :
Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb); // Constructor of the class Object Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb); // Constructor of the class Object
virtual ~Object(); // Destructor of the class Object virtual ~Object(); // Destructor of the class Object
virtual void draw() const =0; // pure virtual method to draw the object virtual void draw() const =0; // pure virtual method to draw the object
RigidBody* getRigidBody(); // Return the pointer to the rigid body RigidBody* getRigidBody(); // Return the pointer to the rigid body
}; };
// ----- Class Cube ----- // // ----- Class Cube ----- //
// Represente a Cube in the simulation // Represente a Box in the simulation
class Cube : public Object { class Cube : public Object {
private : private :
float size; // Size of a side in the cube float size; // Size of a side in the box
static const Matrix3x3 inertiaTensor; // Inertia tensor of a cube static const Matrix3x3 inertiaTensor; // Inertia tensor of a box
public : public :
Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass); // Constructor of the class cube Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass); // Constructor of the class cube
virtual ~Cube(); // Destructor of the class cube virtual ~Cube(); // Destructor of the class cube
virtual void draw() const; // Method to draw the cube virtual void draw() const; // Method to draw the cube
}; };
// ----- Class Plane ---- // // ----- Class Plane ---- //
// Represent a plane in the simulation // Represent a plane in the simulation
class Plane : public Object { class Plane : public Object {
private : private :
float width; // Width of the plane float width; // Width of the plane
float height; // Height of the plane float height; // Height of the plane
Vector3D d1; // Unit vector in the plane Vector3D d1; // Unit vector in the plane
Vector3D d2; // Unit vector in the plane Vector3D d2; // Unit vector in the plane
Vector3D normalVector; // Unit normal vector of the plane Vector3D normalVector; // Unit normal vector of the plane
public : public :
Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass); // Constructor of the class Plane Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass); // Constructor of the class Plane
virtual ~Plane(); // Destructor of the class Plane virtual ~Plane(); // Destructor of the class Plane
virtual void draw() const; // Method to draw the plane virtual void draw() const; // Method to draw the plane
}; };
#endif #endif

View File

@ -25,10 +25,10 @@
OutSideCamera::OutSideCamera() { OutSideCamera::OutSideCamera() {
// Initialize the attributes // Initialize the attributes
heightFromFloor = 20.0; heightFromFloor = 10.0;
horizontalAngleRotation = 0; horizontalAngleRotation = 45;
verticalAngleRotation = 45; verticalAngleRotation = 45;
distanceFromOrigin = 40.0; distanceFromOrigin = 30.0;
lookAtPoint.setAllValues(0.0, 0.0, 0.0); lookAtPoint.setAllValues(0.0, 0.0, 0.0);
// Update the position of the camera // Update the position of the camera

View File

@ -98,6 +98,7 @@ void Scene::display(const Context& context) const {
// Draw all objects in the context // Draw all objects in the context
for(int i=0; i<context.getNbObjects(); ++i) for(int i=0; i<context.getNbObjects(); ++i)
{ {
// Copy the active matrix on the matrix stack // Copy the active matrix on the matrix stack
glPushMatrix(); glPushMatrix();
@ -112,14 +113,16 @@ void Scene::display(const Context& context) const {
// Remove the matrix on the top of the matrix stack // Remove the matrix on the top of the matrix stack
glPopMatrix(); glPopMatrix();
} }
// Draw all the contact points // Draw all the contact points
for (std::vector<Constraint*>::iterator it = world->getConstraintsBeginIterator(); it != world->getConstraintsEndIterator(); ++it) { for (std::vector<Constraint*>::iterator it = world->getConstraintsBeginIterator(); it != world->getConstraintsEndIterator(); ++it) {
RigidBody* rigidBody1 = dynamic_cast<RigidBody*>((*it)->getBody1()); RigidBody* rigidBody1 = dynamic_cast<RigidBody*>((*it)->getBody1());
RigidBody* rigidBody2 = dynamic_cast<RigidBody*>((*it)->getBody2()); RigidBody* rigidBody2 = dynamic_cast<RigidBody*>((*it)->getBody2());
rigidBody1->setIsMotionEnabled(false); //rigidBody1->setIsMotionEnabled(false);
rigidBody2->setIsMotionEnabled(false); //rigidBody2->setIsMotionEnabled(false);
Contact* contact = dynamic_cast<Contact*>((*it)); Contact* contact = dynamic_cast<Contact*>((*it));
assert(contact != 0); assert(contact != 0);
@ -131,8 +134,10 @@ void Scene::display(const Context& context) const {
glPopMatrix(); glPopMatrix();
} }
// Change the buffers glFlush();
SDL_GL_SwapBuffers();
// Swap the buffers
SDL_GL_SwapBuffers();
} }
// Reshape the window // Reshape the window

View File

@ -27,7 +27,7 @@ using namespace reactphysics3d;
// Constructor of the class Simulation // Constructor of the class Simulation
Simulation::Simulation() Simulation::Simulation()
:world(new PhysicsWorld(Vector3D(0.0, 0.0, 0.0))), engine(world, Time(0.01)), scene(this->world) { :world(new PhysicsWorld(Vector3D(0.0, -9.8, 0.0))), engine(world, Time(0.01)), scene(this->world) { // TODO : Change the timestep here after debugging
simRunning = false; simRunning = false;
mouseButtonPressed = false; mouseButtonPressed = false;
nbFrame = 0; nbFrame = 0;
@ -84,7 +84,7 @@ void Simulation::start() {
pEngine->updateDisplayTime(Time(time)); pEngine->updateDisplayTime(Time(time));
// Update the physics // Update the physics
pEngine->updateCollision(); pEngine->update();
// Display the actual scene // Display the actual scene
scene.display(context); scene.display(context);