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

This commit is contained in:
chappuis.daniel 2010-06-08 21:03:35 +00:00
parent 99de21bcaa
commit f2beab0fcf
3 changed files with 221 additions and 223 deletions

View File

@ -16,64 +16,64 @@
* 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 "Context.h" #include "Context.h"
#include "../reactphysics3d/reactphysics3d.h" #include "../reactphysics3d/reactphysics3d.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
// We want to use the ReactPhysics3D namespace // We want to use the ReactPhysics3D namespace
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor of the class Context // Constructor of the class Context
Context::Context() { Context::Context() {
Cube* cube1 = new Cube(Vector3D(11.0,14.0, 4.0), Quaternion(1.0, 1.0, 0.0, 0.0), 4.0, Kilogram(3.0)); Cube* cube1 = new Cube(Vector3D(5.0,14.0, 4.0), Quaternion(1.0, 1.0, 0.0, 0.0), 4.0, Kilogram(3.0));
cube1->getRigidBody()->setLinearVelocity(Vector3D(0.0, -5.0, 0.0)); cube1->getRigidBody()->setLinearVelocity(Vector3D(0.0, -5.0, 0.0));
//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* 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)); //Cube* cube3 = new Cube(Vector3D(4.0, 17, -2.0), Quaternion(0.0, 1.0, 0.0, 0.0), 2.0, Kilogram(11.0));
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)); 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));
addObject(cube1); addObject(cube1);
//addObject(cube2); //addObject(cube2);
//addObject(cube3); //addObject(cube3);
addObject(plane1); addObject(plane1);
} }
// Destructor of the class Context // Destructor of the class Context
Context::~Context() { Context::~Context() {
// Delete all the objects in vectObjects // Delete all the objects in vectObjects
for(unsigned int i=0; i<vectObjects.size(); ++i) { for(unsigned int i=0; i<vectObjects.size(); ++i) {
delete vectObjects[i]; delete vectObjects[i];
} }
} }
// Method to get an object from the context // Method to get an object from the context
Object& Context::getObject(int objectIndex) const { Object& Context::getObject(int objectIndex) const {
// TODO : WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION // TODO : WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Return the object from the context // Return the object from the context
return (*vectObjects.at(objectIndex)); // TODO : THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS return (*vectObjects.at(objectIndex)); // TODO : THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
} }
// Method for adding an object into the context // Method for adding an object into the context
void Context::addObject(Object* object) { void Context::addObject(Object* object) {
if (object != 0) { if (object != 0) {
// Add the object into the context // Add the object into the context
vectObjects.push_back(object); vectObjects.push_back(object);
} }
} }
// Method to remove an object from the context // Method to remove an object from the context
void Context::removeObject(int objectIndex) { void Context::removeObject(int objectIndex) {
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION // WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Restore the memory of the element // Restore the memory of the element
delete vectObjects[objectIndex]; delete vectObjects[objectIndex];
// Erase the element in the vector // Erase the element in the vector
vectObjects.erase(vectObjects.begin()+objectIndex); vectObjects.erase(vectObjects.begin()+objectIndex);
} }

View File

@ -16,106 +16,106 @@
* 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/>. *
***************************************************************************/ ***************************************************************************/
// Librairies // Librairies
#include "Scene.h" #include "Scene.h"
#include "Objects.h" #include "Objects.h"
#include <GL/gl.h> #include <GL/gl.h>
#include <GL/glu.h> #include <GL/glu.h>
// Constructor of the class Scene // Constructor of the class Scene
Scene::Scene(rp3d::PhysicsWorld* world) { Scene::Scene(rp3d::PhysicsWorld* world) {
this->world = world; this->world = world;
// Initialise the material specular color // Initialise the material specular color
mat_specular[0] = 1.0; mat_specular[0] = 1.0;
mat_specular[1] = 1.0; mat_specular[1] = 1.0;
mat_specular[2] = 1.0; mat_specular[2] = 1.0;
mat_specular[3] = 1.0; mat_specular[3] = 1.0;
// Initialize the material shininess // Initialize the material shininess
mat_shininess[0] = 50.0; mat_shininess[0] = 50.0;
// Initialise the light source position // Initialise the light source position
light_position[0] = 20.0; light_position[0] = 20.0;
light_position[1] = 9.0; light_position[1] = 9.0;
light_position[2] = 15.0; light_position[2] = 15.0;
light_position[3] = 0.0; light_position[3] = 0.0;
// Initialise the ambient color of the light // Initialise the ambient color of the light
ambient_color[0] = 1.0; ambient_color[0] = 1.0;
ambient_color[1] = 1.0; ambient_color[1] = 1.0;
ambient_color[2] = 1.0; ambient_color[2] = 1.0;
ambient_color[3] = 0.7; ambient_color[3] = 0.7;
// Initialise the diffuse light color // Initialise the diffuse light color
white_light[0] = 1.0; white_light[0] = 1.0;
white_light[1] = 1.0; white_light[1] = 1.0;
white_light[2] = 1.0; white_light[2] = 1.0;
white_light[3] = 1.0; white_light[3] = 1.0;
} }
// Destructor of the class Scene // Destructor of the class Scene
Scene::~Scene() { Scene::~Scene() {
} }
// Init method // Init method
void Scene::init() { void Scene::init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background
glShadeModel(GL_SMOOTH); glShadeModel(GL_SMOOTH);
glClearDepth(1.0); glClearDepth(1.0);
// Lighting settings // Lighting settings
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color of the material glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color of the material
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // Shininess of the material glMaterialfv(GL_FRONT, GL_SHININESS, mat_shininess); // Shininess of the material
glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Position of the light source glLightfv(GL_LIGHT0, GL_POSITION, light_position); // Position of the light source
glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); // Ambient color of the light glLightfv(GL_LIGHT0, GL_AMBIENT, ambient_color); // Ambient color of the light
glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); // Diffuse color of the light glLightfv(GL_LIGHT0, GL_DIFFUSE, white_light); // Diffuse color of the light
glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); // Specular color of the light glLightfv(GL_LIGHT0, GL_SPECULAR, white_light); // Specular color of the light
glEnable(GL_LIGHTING); // Activate the lighting glEnable(GL_LIGHTING); // Activate the lighting
glEnable(GL_LIGHT0); // Activate a light source glEnable(GL_LIGHT0); // Activate a light source
glEnable(GL_DEPTH_TEST); // Activate the Depth buffer glEnable(GL_DEPTH_TEST); // Activate the Depth buffer
//glEnable(GL_CULL_FACE); //glEnable(GL_CULL_FACE);
} }
// Display method // Display method
void Scene::display(const Context& context) const { void Scene::display(const Context& context) const {
glClearColor(0,0,0,0); glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW); glMatrixMode(GL_MODELVIEW);
glLoadIdentity(); glLoadIdentity();
// Define the position and the direction of the camera // Define the position and the direction of the camera
//gluLookAt(30,10,0,0,0,0,0,1,0); //gluLookAt(30,10,0,0,0,0,0,1,0);
double x = outsideCamera.getPosition().getX(); double x = outsideCamera.getPosition().getX();
double y = outsideCamera.getPosition().getY(); double y = outsideCamera.getPosition().getY();
double z = outsideCamera.getPosition().getZ(); double z = outsideCamera.getPosition().getZ();
gluLookAt(x,y,z,0,0,0,0,1,0); gluLookAt(x,y,z,0,0,0,0,1,0);
// 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();
// Draw the object // Draw the object
context.getObject(i).draw(); context.getObject(i).draw();
glPopMatrix(); glPopMatrix();
glPushMatrix(); glPushMatrix();
// Draw the bounding volume // Draw the bounding volume
context.getObject(i).getRigidBody()->getOBB().draw(); context.getObject(i).getRigidBody()->getOBB()->draw();
// 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*>::const_iterator it = world->getConstraintListStartIterator(); it != world->getConstraintListEndIterator(); ++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);
@ -125,24 +125,22 @@ void Scene::display(const Context& context) const {
assert(contact != 0); assert(contact != 0);
// Draw the contact points // Draw the contact points
for (unsigned int i=0; i<contact->getPoints().size(); ++i) { glPushMatrix();
glPushMatrix(); glTranslatef(contact->getPoint().getX(), contact->getPoint().getY(), contact->getPoint().getZ());
glTranslatef(contact->getPoints()[i].getX(), contact->getPoints()[i].getY(), contact->getPoints()[i].getZ()); contact->draw();
contact->draw(); glPopMatrix();
glPopMatrix(); }
}
} // Change the buffers
SDL_GL_SwapBuffers();
// Change the buffers }
SDL_GL_SwapBuffers();
} // Reshape the window
void Scene::reshape(int width, int height) {
// Reshape the window glViewport(0,0,width,height);
void Scene::reshape(int width, int height) { glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified
glViewport(0,0,width,height); glLoadIdentity(); // Load the identity matrix before the transformations
glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified gluPerspective(45.0, (float) width/height, 0.1f, 150.0f);
glLoadIdentity(); // Load the identity matrix before the transformations glMatrixMode(GL_MODELVIEW); // Specify the matrix that will be modified
gluPerspective(45.0, (float) width/height, 0.1f, 150.0f); glLoadIdentity(); // Load the identity matrix before the transformations
glMatrixMode(GL_MODELVIEW); // Specify the matrix that will be modified }
glLoadIdentity(); // Load the identity matrix before the transformations
}

View File

@ -16,45 +16,45 @@
* 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 "Simulation.h" #include "Simulation.h"
#include "ReactDemo.h" #include "ReactDemo.h"
#include <iostream> #include <iostream>
// We want to use the ReactPhysics3D namespace // We want to use the ReactPhysics3D namespace
using namespace reactphysics3d; using namespace reactphysics3d;
// Constructor of the class Simulation // Constructor of the class Simulation
Simulation::Simulation() Simulation::Simulation()
:world(new PhysicsWorld(Vector3D(0.0, -0.6, 0.0))), engine(world, Time(0.01)), scene(this->world) { :world(new PhysicsWorld(Vector3D(0.0, -0.6, 0.0))), engine(world, Time(0.01)), scene(this->world) {
simRunning = false; simRunning = false;
mouseButtonPressed = false; mouseButtonPressed = false;
nbFrame = 0; nbFrame = 0;
lastFrameTime = 0.0; lastFrameTime = 0.0;
fps = 0.0; fps = 0.0;
} }
// Destructor of the class Simulation // Destructor of the class Simulation
Simulation::~Simulation() { Simulation::~Simulation() {
// Delete the physics world object // Delete the physics world object
delete world; delete world;
} }
// Method to start the simulation // Method to start the simulation
void Simulation::start() { void Simulation::start() {
// Initialisation of the OpenGL settings for the scene // Initialisation of the OpenGL settings for the scene
scene.init(); scene.init();
// Reshape the windows for the first time // Reshape the windows for the first time
scene.reshape(WINWIDTH, WINHEIGHT); scene.reshape(WINWIDTH, WINHEIGHT);
// Add every rigid body to the dynamic world // Add every rigid body to the dynamic world
for (int i=0; i<context.getNbObjects(); ++i) { for (int i=0; i<context.getNbObjects(); ++i) {
world->addBody(context.getObject(i).getRigidBody()); world->addBody(context.getObject(i).getRigidBody());
} }
// Activation of the simulation // Activation of the simulation
simRunning = true; simRunning = true;
// Get the current time // Get the current time
@ -68,11 +68,11 @@ void Simulation::start() {
// Start the physics simulation // Start the physics simulation
pEngine->start(); pEngine->start();
//double time = 1.0; //double time = 1.0;
// Main loop of the simulation // Main loop of the simulation
while(simRunning) { while(simRunning) {
// Check if an SDL event occured and make the apropriate actions // Check if an SDL event occured and make the apropriate actions
checkEvents(); checkEvents();
double time = SDL_GetTicks()/1000.0; double time = SDL_GetTicks()/1000.0;
@ -84,9 +84,9 @@ void Simulation::start() {
pEngine->updateDisplayTime(Time(time)); pEngine->updateDisplayTime(Time(time));
// Update the physics // Update the physics
pEngine->update(); pEngine->updateCollision();
// Display the actual scene // Display the actual scene
scene.display(context); scene.display(context);
// Compute the fps (framerate) // Compute the fps (framerate)
@ -119,7 +119,7 @@ void Simulation::start() {
double y2 = state2.getPosition().getY(); double y2 = state2.getPosition().getY();
double z2 = state2.getPosition().getZ(); double z2 = state2.getPosition().getZ();
std::cout << "Position Cube 2: (" << x2 << ", " << y2 << ", " << z2 << ")" << std::endl; std::cout << "Position Cube 2: (" << x2 << ", " << y2 << ", " << z2 << ")" << std::endl;
std::cout << "quaternion orientation 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ", " << velocity2.getW() << ")" << std::endl;; std::cout << "quaternion orientation 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ", " << velocity2.getW() << ")" << std::endl;;
*/ */
/* /*
@ -129,11 +129,11 @@ void Simulation::start() {
} }
*/ */
} }
} }
// This method checks if an events occur and call the apropriate method // This method checks if an events occur and call the apropriate method
void Simulation::checkEvents() { void Simulation::checkEvents() {
SDL_Event event; // An SDL event SDL_Event event; // An SDL event
// Zoom of the outside camera // Zoom of the outside camera
@ -142,34 +142,34 @@ void Simulation::checkEvents() {
} }
else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) { else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) {
scene.getOutSideCamera().increaseDistance(fps); scene.getOutSideCamera().increaseDistance(fps);
} }
// Check in the stack of events // Check in the stack of events
while(SDL_PollEvent(&event)) { while(SDL_PollEvent(&event)) {
// Check an event // Check an event
switch(event.type) { switch(event.type) {
// An QUIT event occur // An QUIT event occur
case SDL_QUIT: simRunning = false; case SDL_QUIT: simRunning = false;
break; break;
// A keyboard key has been pushed // A keyboard key has been pushed
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
if (event.key.keysym.sym == SDLK_ESCAPE) if (event.key.keysym.sym == SDLK_ESCAPE)
simRunning = false; simRunning = false;
break; break;
// The size of the windows changed then we reshape the windows // The size of the windows changed then we reshape the windows
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h); case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
break; break;
// If the mouse moved // If the mouse moved
case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) { case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
// Rotation of the outSideCamera // Rotation of the outSideCamera
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, fps); scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, fps);
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, fps); scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, fps);
} }
} }
} }
} }
// Compute the framerate (fps) of the application // Compute the framerate (fps) of the application
@ -187,4 +187,4 @@ void Simulation::computeFps() {
lastFrameTime = currentTime; lastFrameTime = currentTime;
nbFrame = 0; nbFrame = 0;
} }
} }