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

This commit is contained in:
chappuis.daniel 2009-02-13 11:26:51 +00:00
parent f4e061911c
commit 7afb8ba7f0
9 changed files with 189 additions and 102 deletions

View File

@ -19,19 +19,21 @@
// 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() {
// We add some objects in the context at the beginning ---> THESE THINGS WILL BE STORE IN A TEXT FILE
Cube* cube1 = new Cube(Object::Position(-2.0, 1.0, -6.0), 2.0);
Cube* cube2 = new Cube(Object::Position(0.0, 1.5, 6.0), 3.0);
Cube* cube3 = new Cube(Object::Position(4.0, 4.0, -2.0), 2.0);
Plane* plane1 = new Plane(Object::Position(0.0, 0.0, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0));
Cube* cube1 = new Cube(Vector3D(-2.0, 3, -6.0), 2.0, 10.0);
Cube* cube2 = new Cube(Vector3D(0.0, 3, 6.0), 3.0, 5.0);
Cube* cube3 = new Cube(Vector3D(4.0, 3.0, -2.0), 2.0, 0.01);
Plane* plane1 = new Plane(Vector3D(0.0, 0.0, 0.0), 20.0, 30.0, Vector3D(-1.0, 0.0, 0.0), Vector3D(0.0, 0.0, 1.0), 10.0);
addObject(cube1);
addObject(cube2);
@ -43,7 +45,7 @@ Context::Context() {
// Destructor of the class Context
Context::~Context() {
// Delete all the objects in vectObjects
for(int i=0; i<vectObjects.size(); ++i) {
for(unsigned int i=0; i<vectObjects.size(); ++i) {
delete vectObjects[0];
}
}

View File

@ -22,7 +22,6 @@
// Libraries
#include "Objects.h"
#include <vector>
// Class Context

View File

@ -22,41 +22,40 @@
//#include <windows.h> // To avoid an error due to the #include <GL/glut.h>
#include <GL/freeglut.h>
#include <math.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <cmath>
#include <iostream>
// ----- Class Object ----- //
// Constructor of the class Object
Object::Object(const Position& position) {
this->position = position;
Object::Object(const Vector3D& position, const Kilogram& mass, const Matrix3x3& inertiaTensor)
:rigidBody(new RigidBody(position, mass, inertiaTensor)) {
}
// Destructor of the class Object
Object::~Object() {
// Delete the rigid body object
delete rigidBody;
}
// ----- Structure Position ----- //
// Constructor without arguments of the structure Position
Object::Position::Position() {
x = 0.0;
y = 0.0;
z = 0.0;
// Return the pointer to the rigid body
RigidBody* Object::getRigidBody() {
return rigidBody;
}
// Constructor of the structure Position
Object::Position::Position(double x, double y, double z) {
this->x = x;
this->y = y;
this->z = z;
};
// ----- Class Cube ----- //
// Static attributes
const Matrix3x3 Cube::inertiaTensor;
// Constructor of the class Cube
Cube::Cube(const Position& position, float size)
:Object(position) {
Cube::Cube(const Vector3D& position, float size, const Kilogram& mass)
:Object(position, 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, 0.0, 1.0/12.0*mass.getValue()*2*size*size)) {
this->size = size;
}
@ -67,24 +66,46 @@ Cube::~Cube() {
// Method to draw the cube
void Cube::draw() const {
// Translation of the cube to its position
glTranslatef(position.x, position.y, position.z);
// Draw the cube
glutSolidCube(size);
// Get the interpolated state of the rigid body
BodyState state = rigidBody->getInterpolatedState();
// Position of the cube
double x = state.getPosition().getX();
double y = state.getPosition().getY();
double z = state.getPosition().getZ();
// Orientation of the cube
Vector3D orientationAxis;
double orientationAngle;
state.getOrientation().getRotationAngleAxis(orientationAngle, orientationAxis);
// Translation of the cube to its position
glTranslatef(x, y, z);
// Rotation of the cube according to its orientation
glRotatef(orientationAngle/pi*180.0, orientationAxis.getX(), orientationAxis.getY(), orientationAxis.getZ());
// Draw the cube
glutSolidCube(size);
}
// ----- Class Plane ----- //
// Constructor of the class Plane
Plane::Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2)
:Object(position) {
Plane::Plane(const Vector3D& position, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass)
:Object(position, 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, 0.0, 1.0/12.0*mass.getValue()*width*width)) {
this->width = width;
this->height = height;
this->d1 = d1;
this->d2 = d2;
// By default Planes in the demo cannot move
rigidBody->setIsMotionEnabled(false);
// Compute the unit normal vector of the plane by a cross product
normalVector = d1.crossProduct(d2).getUnit();
}
@ -96,8 +117,17 @@ Plane::~Plane() {
// Method used to draw the plane
void Plane::draw() const {
// Get the interpolated state of the rigid body
BodyState state = rigidBody->getInterpolatedState();
// Get the position of the rigid body
double x = state.getPosition().getX();
double y = state.getPosition().getY();
double z = state.getPosition().getZ();
// Translation of the cube to its position
glTranslatef(position.x, position.y, position.z);
glTranslatef(x, y, z);
float halfWidth = width / 2.0;
float halfHeight = height / 2.0;
@ -105,17 +135,17 @@ void Plane::draw() const {
// Draw the plane
glBegin(GL_POLYGON);
glColor3f(1.0, 1.0, 1.0);
glVertex3f(position.x + d1.getX() * halfWidth + d2.getX() * halfHeight , position.y + d1.getY() * halfWidth + d2.getY() * halfHeight
, position.z + d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glVertex3f(x + d1.getX() * halfWidth + d2.getX() * halfHeight , y + d1.getY() * halfWidth + d2.getY() * halfHeight
, z + d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(position.x + d1.getX() * halfWidth - d2.getX() * halfHeight , position.y + d1.getY() * halfWidth - d2.getY() * halfHeight
, position.z + d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glVertex3f(x + d1.getX() * halfWidth - d2.getX() * halfHeight , y + d1.getY() * halfWidth - d2.getY() * halfHeight
, z + d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(position.x - d1.getX() * halfWidth - d2.getX() * halfHeight , position.y - d1.getY() * halfWidth - d2.getY() * halfHeight
, position.z - d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glVertex3f(x - d1.getX() * halfWidth - d2.getX() * halfHeight , y - d1.getY() * halfWidth - d2.getY() * halfHeight
, z - d1.getZ() * halfWidth - d2.getZ() * halfHeight);
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
glVertex3f(position.x - d1.getX() * halfWidth + d2.getX() * halfHeight , position.y - d1.getY() * halfWidth + d2.getY() * halfHeight
, position.z - d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glVertex3f(x - d1.getX() * halfWidth + d2.getX() * halfHeight , y - d1.getY() * halfWidth + d2.getY() * halfHeight
, z - d1.getZ() * halfWidth + d2.getZ() * halfHeight);
glEnd();
}

View File

@ -33,47 +33,44 @@ using namespace reactphysics3d;
// ----- Class Object (abstract) ----- //
// Represent an object of the simulation
class Object {
public :
// Structure Object::Position
struct Position {
double x; // x coordinate
double y; // y coordinate
double z; // z coordinate
protected :
RigidBody* rigidBody; // Rigid Body that represents the object
// Methods
Position(); // Constructor without arguments of the structure Position
Position(double x, double y, double z); // Constructor of the structure Position
} position; // Position of the object
Object(const Position& position); // Constructor of the class Object
virtual ~Object(); // Destructor of the class Object
virtual void draw() const =0; // pure virtual method to draw the object
public :
Object(const Vector3D& position, const Kilogram& mass, const Matrix3x3& inertiaTensor); // Constructor of the class Object
virtual ~Object(); // Destructor of the class Object
virtual void draw() const =0; // pure virtual method to draw the object
RigidBody* getRigidBody(); // Return the pointer to the rigid body
};
// ----- Class Cube ----- //
// Represente a Cube in the simulation
class Cube : public Object {
private :
float size; // Size of a side in the cube
float size; // Size of a side in the cube
static const Matrix3x3 inertiaTensor; // Inertia tensor of a cube
public :
Cube(const Position& position, float size); // Constructor of the class cube
virtual ~Cube(); // Destructor of the class cube
virtual void draw() const; // Method to draw the cube
Cube(const Vector3D& position, float size, const Kilogram& mass); // Constructor of the class cube
virtual ~Cube(); // Destructor of the class cube
virtual void draw() const; // Method to draw the cube
};
// ----- Class Plane ---- //
// Represent a plane in the simulation
class Plane : public Object {
private :
float width; // Width of the plane
float height; // Height of the plane
Vector3D d1; // Unit vector in the plane
Vector3D d2; // Unit vector in the plane
Vector3D normalVector; // Unit normal vector of the plane
public :
float width; // Width of the plane
float height; // Height of the plane
Vector3D d1; // Unit vector in the plane
Vector3D d2; // Unit vector in the plane
Vector3D normalVector; // Unit normal vector of the plane
Plane(const Position& position, float width, float height, const Vector3D& d1, const Vector3D& d2); // Constructor of the class Plane
virtual ~Plane(); // Destructor of the class Plane
virtual void draw() const; // Method to draw the plane
Plane(const Vector3D& position, 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 void draw() const; // Method to draw the plane
};
#endif

View File

@ -31,7 +31,7 @@ OutSideCamera::OutSideCamera() {
heightFromFloor = 20.0;
horizontalAngleRotation = 0;
verticalAngleRotation = 45;
distanceFromOrigin = 10.0;
distanceFromOrigin = 40.0;
lookAtPoint.setAllValues(0.0, 0.0, 0.0);
// Update the position of the camera
@ -61,7 +61,7 @@ void OutSideCamera::updatePosition() {
void OutSideCamera::modifyHorizontalAngleRotation(int screenDistance, float fps) {
// Update the horizontal rotation angle of the camera
horizontalAngleRotation = (horizontalAngleRotation + int(screenDistance * 60.0 / fps)) % 360;
horizontalAngleRotation = (horizontalAngleRotation + int(screenDistance * 700.0 / fps)) % 360;
// Update the position and the view vector of the camera
updatePosition();
@ -72,7 +72,7 @@ void OutSideCamera::modifyHorizontalAngleRotation(int screenDistance, float fps)
void OutSideCamera::modifyVerticalAngleRotation(int screenDistance, float fps) {
// Update the vertical rotation angle of the camera
verticalAngleRotation = verticalAngleRotation + (screenDistance * 60.0 / fps);
verticalAngleRotation = verticalAngleRotation + (screenDistance * 700.0 / fps);
// Vertical angle limits
if (verticalAngleRotation > 89) {

View File

@ -23,7 +23,6 @@
// Libraries
#include "Context.h"
#include "OutSideCamera.h"
#include <SDL/SDL.h>
#include <GL/freeglut.h> // Used only to draw cubes
#include <GL/gl.h>

View File

@ -22,16 +22,23 @@
#include "ReactDemo.h"
#include <iostream>
// We want to use the ReactPhysics3D namespace
using namespace reactphysics3d;
// Constructor of the class Simulation
Simulation::Simulation() {
Simulation::Simulation()
:world(new DynamicWorld(Vector3D(0.0, -9.81, 0.0))), engine(world, Time(0.01)){
simRunning = false;
mouseButtonPressed = false;
nbFrame = 0;
lastFrameTime = 0.0;
fps = 0.0;
}
// Destructor of the class Simulation
Simulation::~Simulation() {
// Delete the physics world object
delete world;
}
// Method to start the simulation
@ -42,24 +49,69 @@ void Simulation::start() {
// 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
simRunning = true;
// Get the current time
currentFrameTime = SDL_GetTicks();
lastFrameTime = SDL_GetTicks();
// Initialize the display time
engine.initializeDisplayTime(Time(SDL_GetTicks()/1000.0));
// Start the physics simulation
engine.start();
// 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;
std::cout << "Time : " << time << std::endl;
// Update the display time
engine.updateDisplayTime(Time(time));
// Update the physics
engine.update();
// Display the actual scene
scene.display(context);
// Compute the fps (framerate)
computeFps();
std::cout << "FPS : " << fps << std::endl;
//std::cout << fps << std::endl;
BodyState state = context.getObject(0).getRigidBody()->getInterpolatedState();
Vector3D velocity = context.getObject(0).getRigidBody()->getInterpolatedState().getLinearVelocity();
//std::cout << "Velocity 0 : " << velocity.getX() << ", " << velocity.getY() << ", " << velocity.getZ() << ")" << std::endl;
double x = state.getPosition().getX();
double y = state.getPosition().getY();
double z = state.getPosition().getZ();
std::cout << "Position Cube 0 : (" << x << ", " << y << ", " << z << ")" << std::endl;
std::cout << "linear velocity 0 : " << velocity.length() << std::endl;;
BodyState state1 = context.getObject(1).getRigidBody()->getInterpolatedState();
Vector3D velocity1 = context.getObject(1).getRigidBody()->getInterpolatedState().getLinearVelocity();
//std::cout << "Velocity 1 : " << velocity1.getX() << ", " << velocity1.getY() << ", " << velocity1.getZ() << ")" << std::endl;
double x1 = state1.getPosition().getX();
double y1 = state1.getPosition().getY();
double z1 = state1.getPosition().getZ();
std::cout << "Position Cube 1 : (" << x1 << ", " << y1 << ", " << z1 << ")" << std::endl;
std::cout << "linear velocity 1 : " << velocity1.length() << std::endl;
BodyState state2 = context.getObject(2).getRigidBody()->getInterpolatedState();
Vector3D velocity2 = context.getObject(2).getRigidBody()->getInterpolatedState().getLinearVelocity();
//std::cout << "Velocity 2 : " << velocity2.getX() << ", " << velocity2.getY() << ", " << velocity2.getZ() << ")" << std::endl;
double x2 = state2.getPosition().getX();
double y2 = state2.getPosition().getY();
double z2 = state2.getPosition().getZ();
std::cout << "Position Cube 2: (" << x2 << ", " << y2 << ", " << z2 << ")" << std::endl;
std::cout << "linear velocity 2 : " << velocity2.length() << std::endl;;
}
}
@ -96,10 +148,8 @@ void Simulation::checkEvents() {
// If the mouse moved
case SDL_MOUSEMOTION: if (SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) {
// Rotation of the outSideCamera
// TODO : Problem here when we try to implement fps indepence (if we try to
// replace 60 by the variable fps
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, 30);
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, 30);
scene.getOutSideCamera().modifyHorizontalAngleRotation(event.motion.xrel, fps);
scene.getOutSideCamera().modifyVerticalAngleRotation(event.motion.yrel, fps);
}
}
}
@ -107,11 +157,17 @@ void Simulation::checkEvents() {
// Compute the framerate (fps) of the application
void Simulation::computeFps() {
double lastFrameTime = currentFrameTime;
// Get the current time
currentFrameTime = SDL_GetTicks();
// Increment the number of frame in the last second
nbFrame++;
// Compute the new framerate
fps = 1000 / double(currentFrameTime - lastFrameTime);
// Get the current time
double currentTime = SDL_GetTicks();
// Compute the framerate
if (currentTime - lastFrameTime > 1000.0) {
fps = nbFrame * 1000.0/(currentTime-lastFrameTime);
lastFrameTime = currentTime;
nbFrame = 0;
}
}

View File

@ -23,15 +23,19 @@
// Librairies
#include "Context.h"
#include "Scene.h"
#include "../reactphysics3d/reactphysics3d.h"
// Class Simulation
class Simulation {
private :
Scene scene; // Scene object for displaying the simulation
Context context; // Context of the simulation
rp3d::DynamicWorld* world; // Pointer to the dynamic world that contains bodies of the simulation
rp3d::DynamicEngine engine; // Dynamic engine for the physics of the simulation
bool simRunning; // True if the simulation is running and false otherwise
bool mouseButtonPressed; // True if the left mouse button is pressed
double currentFrameTime; // Current frame time
double lastFrameTime; // Last frame time
int nbFrame; // Number of frame (used to compute the framerate)
double fps; // Framerate of the application
void computeFps(); // Compute the framerate of the application

View File

@ -87,7 +87,7 @@ int initSDL() {
}
// Define the window title and the window icon
SDL_WM_SetCaption("React Demo 0.01", NULL);
SDL_WM_SetCaption("React Demo 0.02", NULL);
// Get the state of the Double Buffer parameter
int nValue;