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 *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "Context.h"
#include "../reactphysics3d/reactphysics3d.h"
#include <iostream>
#include <vector>
// Libraries
#include "Context.h"
#include "../reactphysics3d/reactphysics3d.h"
#include <iostream>
#include <vector>
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor of the class 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));
using namespace reactphysics3d;
// Constructor of the class Context
Context::Context() {
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));
//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* 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));
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(cube2);
//addObject(cube3);
addObject(plane1);
}
// Destructor of the class Context
Context::~Context() {
// Delete all the objects in vectObjects
for(unsigned int i=0; i<vectObjects.size(); ++i) {
delete vectObjects[i];
}
}
// Method to get an object from the context
Object& Context::getObject(int objectIndex) const {
// TODO : WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Return the object from the context
return (*vectObjects.at(objectIndex)); // TODO : THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
}
// Method for adding an object into the context
void Context::addObject(Object* object) {
if (object != 0) {
// Add the object into the context
vectObjects.push_back(object);
}
}
// Method to remove an object from the context
void Context::removeObject(int objectIndex) {
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Restore the memory of the element
delete vectObjects[objectIndex];
// Erase the element in the vector
vectObjects.erase(vectObjects.begin()+objectIndex);
}
addObject(cube1);
//addObject(cube2);
//addObject(cube3);
addObject(plane1);
}
// Destructor of the class Context
Context::~Context() {
// Delete all the objects in vectObjects
for(unsigned int i=0; i<vectObjects.size(); ++i) {
delete vectObjects[i];
}
}
// Method to get an object from the context
Object& Context::getObject(int objectIndex) const {
// TODO : WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Return the object from the context
return (*vectObjects.at(objectIndex)); // TODO : THROWN AN EXCEPTION IF INDEX IS OUT OF THE BOUNDS
}
// Method for adding an object into the context
void Context::addObject(Object* object) {
if (object != 0) {
// Add the object into the context
vectObjects.push_back(object);
}
}
// Method to remove an object from the context
void Context::removeObject(int objectIndex) {
// WE HAVE TO ADD HERE AN EXCEPTION IMPLEMENTATION
// Restore the memory of the element
delete vectObjects[objectIndex];
// Erase the element in the vector
vectObjects.erase(vectObjects.begin()+objectIndex);
}

View File

@ -16,106 +16,106 @@
* You should have received a copy of the GNU Lesser General Public License *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Librairies
#include "Scene.h"
// Librairies
#include "Scene.h"
#include "Objects.h"
#include <GL/gl.h>
#include <GL/glu.h>
// Constructor of the class Scene
#include <GL/glu.h>
// Constructor of the class Scene
Scene::Scene(rp3d::PhysicsWorld* world) {
this->world = world;
// Initialise the material specular color
mat_specular[0] = 1.0;
mat_specular[1] = 1.0;
mat_specular[2] = 1.0;
mat_specular[3] = 1.0;
// Initialize the material shininess
mat_shininess[0] = 50.0;
// Initialise the light source position
light_position[0] = 20.0;
light_position[1] = 9.0;
light_position[2] = 15.0;
light_position[3] = 0.0;
// Initialise the ambient color of the light
ambient_color[0] = 1.0;
ambient_color[1] = 1.0;
ambient_color[2] = 1.0;
ambient_color[3] = 0.7;
// Initialise the diffuse light color
white_light[0] = 1.0;
white_light[1] = 1.0;
white_light[2] = 1.0;
white_light[3] = 1.0;
}
// Destructor of the class Scene
Scene::~Scene() {
}
// Init method
void Scene::init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
// Lighting settings
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color 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_AMBIENT, ambient_color); // Ambient 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
glEnable(GL_LIGHTING); // Activate the lighting
glEnable(GL_LIGHT0); // Activate a light source
// Initialise the material specular color
mat_specular[0] = 1.0;
mat_specular[1] = 1.0;
mat_specular[2] = 1.0;
mat_specular[3] = 1.0;
// Initialize the material shininess
mat_shininess[0] = 50.0;
// Initialise the light source position
light_position[0] = 20.0;
light_position[1] = 9.0;
light_position[2] = 15.0;
light_position[3] = 0.0;
// Initialise the ambient color of the light
ambient_color[0] = 1.0;
ambient_color[1] = 1.0;
ambient_color[2] = 1.0;
ambient_color[3] = 0.7;
// Initialise the diffuse light color
white_light[0] = 1.0;
white_light[1] = 1.0;
white_light[2] = 1.0;
white_light[3] = 1.0;
}
// Destructor of the class Scene
Scene::~Scene() {
}
// Init method
void Scene::init() {
glClearColor(0.0, 0.0, 0.0, 0.0); // Select the color for the background
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
// Lighting settings
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular); // Specular color 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_AMBIENT, ambient_color); // Ambient 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
glEnable(GL_LIGHTING); // Activate the lighting
glEnable(GL_LIGHT0); // Activate a light source
glEnable(GL_DEPTH_TEST); // Activate the Depth buffer
//glEnable(GL_CULL_FACE);
}
// Display method
void Scene::display(const Context& context) const {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Define the position and the direction of the camera
//glEnable(GL_CULL_FACE);
}
// Display method
void Scene::display(const Context& context) const {
glClearColor(0,0,0,0);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Define the position and the direction of the camera
//gluLookAt(30,10,0,0,0,0,0,1,0);
double x = outsideCamera.getPosition().getX();
double y = outsideCamera.getPosition().getY();
double z = outsideCamera.getPosition().getZ();
gluLookAt(x,y,z,0,0,0,0,1,0);
// Draw all objects in the context
for(int i=0; i<context.getNbObjects(); ++i)
{
// Copy the active matrix on the matrix stack
glPushMatrix();
// Draw the object
double z = outsideCamera.getPosition().getZ();
gluLookAt(x,y,z,0,0,0,0,1,0);
// Draw all objects in the context
for(int i=0; i<context.getNbObjects(); ++i)
{
// Copy the active matrix on the matrix stack
glPushMatrix();
// Draw the object
context.getObject(i).draw();
glPopMatrix();
glPushMatrix();
// Draw the bounding volume
context.getObject(i).getRigidBody()->getOBB().draw();
// Remove the matrix on the top of the matrix stack
glPopMatrix();
context.getObject(i).getRigidBody()->getOBB()->draw();
// Remove the matrix on the top of the matrix stack
glPopMatrix();
}
// 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* rigidBody2 = dynamic_cast<RigidBody*>((*it)->getBody2());
rigidBody1->setIsMotionEnabled(false);
@ -125,24 +125,22 @@ void Scene::display(const Context& context) const {
assert(contact != 0);
// Draw the contact points
for (unsigned int i=0; i<contact->getPoints().size(); ++i) {
glPushMatrix();
glTranslatef(contact->getPoints()[i].getX(), contact->getPoints()[i].getY(), contact->getPoints()[i].getZ());
contact->draw();
glPopMatrix();
}
}
// Change the buffers
SDL_GL_SwapBuffers();
}
// Reshape the window
void Scene::reshape(int width, int height) {
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified
glLoadIdentity(); // Load the identity matrix before the transformations
gluPerspective(45.0, (float) width/height, 0.1f, 150.0f);
glMatrixMode(GL_MODELVIEW); // Specify the matrix that will be modified
glLoadIdentity(); // Load the identity matrix before the transformations
}
glPushMatrix();
glTranslatef(contact->getPoint().getX(), contact->getPoint().getY(), contact->getPoint().getZ());
contact->draw();
glPopMatrix();
}
// Change the buffers
SDL_GL_SwapBuffers();
}
// Reshape the window
void Scene::reshape(int width, int height) {
glViewport(0,0,width,height);
glMatrixMode(GL_PROJECTION); // Specify the matrix that will be modified
glLoadIdentity(); // Load the identity matrix before the transformations
gluPerspective(45.0, (float) width/height, 0.1f, 150.0f);
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 *
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
***************************************************************************/
// Libraries
#include "Simulation.h"
#include "ReactDemo.h"
// Libraries
#include "Simulation.h"
#include "ReactDemo.h"
#include <iostream>
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor of the class Simulation
using namespace reactphysics3d;
// Constructor of the class Simulation
Simulation::Simulation()
:world(new PhysicsWorld(Vector3D(0.0, -0.6, 0.0))), engine(world, Time(0.01)), scene(this->world) {
simRunning = false;
:world(new PhysicsWorld(Vector3D(0.0, -0.6, 0.0))), engine(world, Time(0.01)), scene(this->world) {
simRunning = false;
mouseButtonPressed = false;
nbFrame = 0;
lastFrameTime = 0.0;
fps = 0.0;
}
// Destructor of the class Simulation
Simulation::~Simulation() {
fps = 0.0;
}
// Destructor of the class Simulation
Simulation::~Simulation() {
// Delete the physics world object
delete world;
}
// Method to start the simulation
void Simulation::start() {
// Initialisation of the OpenGL settings for the scene
scene.init();
// Reshape the windows for the first time
delete world;
}
// Method to start the simulation
void Simulation::start() {
// Initialisation of the OpenGL settings for the scene
scene.init();
// Reshape the windows for the first time
scene.reshape(WINWIDTH, WINHEIGHT);
// Add every rigid body to the dynamic world
for (int i=0; i<context.getNbObjects(); ++i) {
world->addBody(context.getObject(i).getRigidBody());
}
// Activation of the simulation
// Activation of the simulation
simRunning = true;
// Get the current time
@ -68,11 +68,11 @@ void Simulation::start() {
// Start the physics simulation
pEngine->start();
//double time = 1.0;
// Main loop of the simulation
while(simRunning) {
// Check if an SDL event occured and make the apropriate actions
//double time = 1.0;
// Main loop of the simulation
while(simRunning) {
// Check if an SDL event occured and make the apropriate actions
checkEvents();
double time = SDL_GetTicks()/1000.0;
@ -84,9 +84,9 @@ void Simulation::start() {
pEngine->updateDisplayTime(Time(time));
// Update the physics
pEngine->update();
// Display the actual scene
pEngine->updateCollision();
// Display the actual scene
scene.display(context);
// Compute the fps (framerate)
@ -119,7 +119,7 @@ void Simulation::start() {
double y2 = state2.getPosition().getY();
double z2 = state2.getPosition().getZ();
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
void Simulation::checkEvents() {
}
}
// This method checks if an events occur and call the apropriate method
void Simulation::checkEvents() {
SDL_Event event; // An SDL event
// Zoom of the outside camera
@ -142,34 +142,34 @@ void Simulation::checkEvents() {
}
else if(SDL_GetKeyState(NULL)[SDLK_DOWN]) {
scene.getOutSideCamera().increaseDistance(fps);
}
// Check in the stack of events
while(SDL_PollEvent(&event)) {
// Check an event
switch(event.type) {
// An QUIT event occur
case SDL_QUIT: simRunning = false;
break;
// A keyboard key has been pushed
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
if (event.key.keysym.sym == SDLK_ESCAPE)
simRunning = false;
break;
// The size of the windows changed then we reshape the windows
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
break;
// If the mouse moved
}
// Check in the stack of events
while(SDL_PollEvent(&event)) {
// Check an event
switch(event.type) {
// An QUIT event occur
case SDL_QUIT: simRunning = false;
break;
// A keyboard key has been pushed
case SDL_KEYDOWN: // The Esc key has been pushed then we end the simulation
if (event.key.keysym.sym == SDLK_ESCAPE)
simRunning = false;
break;
// The size of the windows changed then we reshape the windows
case SDL_VIDEORESIZE: scene.reshape(event.resize.w, event.resize.h);
break;
// If the mouse moved
case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
// Rotation of the outSideCamera
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, fps);
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, fps);
}
}
}
}
}
}
}
// Compute the framerate (fps) of the application
@ -187,4 +187,4 @@ void Simulation::computeFps() {
lastFrameTime = currentTime;
nbFrame = 0;
}
}
}