reactphysics3d/examples/joints/Scene.cpp

405 lines
16 KiB
C++
Raw Normal View History

2013-04-25 22:46:09 +00:00
/********************************************************************************
* 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 "Scene.h"
2013-06-26 20:29:23 +00:00
#include <cmath>
2013-04-25 22:46:09 +00:00
// Namespaces
using namespace openglframework;
// Constructor
Scene::Scene(GlutViewer* viewer) : mViewer(viewer), mLight0(0),
mPhongShader("shaders/phong.vert",
"shaders/phong.frag"), mIsRunning(false) {
// Move the light 0
mLight0.translateWorld(Vector3(7, 15, 15));
// Compute the radius and the center of the scene
float radiusScene = 10.0f;
openglframework::Vector3 center(0, 5, 0);
// Set the center of the scene
mViewer->setScenePosition(center, radiusScene);
// Gravity vector in the dynamics world
rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0);
2013-04-25 22:46:09 +00:00
// Time step for the physics simulation
rp3d::decimal timeStep = 1.0f / 60.0f;
// Create the dynamics world for the physics simulation
mDynamicsWorld = new rp3d::DynamicsWorld(gravity, timeStep);
// Set the number of iterations of the constraint solver
2013-05-08 21:33:04 +00:00
mDynamicsWorld->setNbIterationsVelocitySolver(15);
2013-04-25 22:46:09 +00:00
2013-05-02 20:41:57 +00:00
// Create the Ball-and-Socket joint
createBallAndSocketJoints();
2013-04-25 22:46:09 +00:00
// Create the Slider joint
createSliderJoint();
2013-06-26 20:29:23 +00:00
// Create the Hinge joint
createPropellerHingeJoint();
// Create the Fixed joint
createFixedJoints();
2013-04-25 22:46:09 +00:00
// Create the floor
2013-05-02 20:41:57 +00:00
createFloor();
2013-04-25 22:46:09 +00:00
// Start the simulation
startSimulation();
}
// Destructor
Scene::~Scene() {
// Stop the physics simulation
stopSimulation();
// Destroy the shader
mPhongShader.destroy();
2013-05-02 20:41:57 +00:00
// Destroy the joints
mDynamicsWorld->destroyJoint(mSliderJoint);
2013-06-26 20:29:23 +00:00
mDynamicsWorld->destroyJoint(mPropellerHingeJoint);
mDynamicsWorld->destroyJoint(mFixedJoint1);
mDynamicsWorld->destroyJoint(mFixedJoint2);
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES-1; i++) {
mDynamicsWorld->destroyJoint(mBallAndSocketJoints[i]);
}
// Destroy all the rigid bodies of the scene
mDynamicsWorld->destroyRigidBody(mSliderJointBottomBox->getRigidBody());
mDynamicsWorld->destroyRigidBody(mSliderJointTopBox->getRigidBody());
mDynamicsWorld->destroyRigidBody(mPropellerBox->getRigidBody());
mDynamicsWorld->destroyRigidBody(mFixedJointBox1->getRigidBody());
mDynamicsWorld->destroyRigidBody(mFixedJointBox2->getRigidBody());
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
mDynamicsWorld->destroyRigidBody(mBallAndSocketJointChainBoxes[i]->getRigidBody());
}
2013-04-25 22:46:09 +00:00
2013-06-26 20:29:23 +00:00
delete mSliderJointBottomBox;
delete mSliderJointTopBox;
delete mPropellerBox;
delete mFixedJointBox1;
delete mFixedJointBox2;
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
delete mBallAndSocketJointChainBoxes[i];
}
2013-04-25 22:46:09 +00:00
// Destroy the floor
2013-05-02 20:41:57 +00:00
mDynamicsWorld->destroyRigidBody(mFloor->getRigidBody());
2013-04-25 22:46:09 +00:00
delete mFloor;
// Destroy the dynamics world
delete mDynamicsWorld;
}
// Take a step for the simulation
void Scene::simulate() {
// If the physics simulation is running
if (mIsRunning) {
2013-06-26 20:29:23 +00:00
// Update the motor speed of the Slider Joint (to move up and down)
long double motorSpeed = 3 * cos(mDynamicsWorld->getPhysicsTime() * 1.5);
mSliderJoint->setMotorSpeed(rp3d::decimal(motorSpeed));
2013-06-26 20:29:23 +00:00
2013-04-25 22:46:09 +00:00
// Take a simulation step
mDynamicsWorld->update();
// Update the position and orientation of the boxes
2013-06-26 20:29:23 +00:00
mSliderJointBottomBox->updateTransform();
mSliderJointTopBox->updateTransform();
mPropellerBox->updateTransform();
mFixedJointBox1->updateTransform();
mFixedJointBox2->updateTransform();
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
mBallAndSocketJointChainBoxes[i]->updateTransform();
}
2013-04-25 22:46:09 +00:00
2013-05-02 20:41:57 +00:00
// Update the position and orientation of the floor
2013-04-25 22:46:09 +00:00
mFloor->updateTransform();
}
}
// Render the scene
void Scene::render() {
glEnable(GL_DEPTH_TEST);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glEnable(GL_CULL_FACE);
2013-07-02 21:14:08 +00:00
// Get the world-space to camera-space matrix
const Camera& camera = mViewer->getCamera();
const openglframework::Matrix4 worldToCameraMatrix = camera.getTransformMatrix().getInverse();
2013-04-25 22:46:09 +00:00
// Bind the shader
mPhongShader.bind();
// Set the variables of the shader
2013-07-03 20:42:58 +00:00
mPhongShader.setVector3Uniform("light0PosCameraSpace",worldToCameraMatrix * mLight0.getOrigin());
2013-04-25 22:46:09 +00:00
mPhongShader.setMatrix4x4Uniform("projectionMatrix", camera.getProjectionMatrix());
mPhongShader.setVector3Uniform("lightAmbientColor", Vector3(0.3f, 0.3f, 0.3f));
Color& diffCol = mLight0.getDiffuseColor();
Color& specCol = mLight0.getSpecularColor();
2013-07-03 20:42:58 +00:00
mPhongShader.setVector3Uniform("light0DiffuseColor", Vector3(diffCol.r, diffCol.g, diffCol.b));
mPhongShader.setVector3Uniform("light0SpecularColor", Vector3(specCol.r, specCol.g, specCol.b));
2013-04-25 22:46:09 +00:00
mPhongShader.setFloatUniform("shininess", 60.0f);
2013-05-02 20:41:57 +00:00
// Render all the boxes
2013-07-02 21:14:08 +00:00
mSliderJointBottomBox->render(mPhongShader, worldToCameraMatrix);
mSliderJointTopBox->render(mPhongShader, worldToCameraMatrix);
mPropellerBox->render(mPhongShader, worldToCameraMatrix);
mFixedJointBox1->render(mPhongShader, worldToCameraMatrix);
mFixedJointBox2->render(mPhongShader, worldToCameraMatrix);
2013-06-26 20:29:23 +00:00
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
2013-07-02 21:14:08 +00:00
mBallAndSocketJointChainBoxes[i]->render(mPhongShader, worldToCameraMatrix);
2013-06-26 20:29:23 +00:00
}
2013-04-25 22:46:09 +00:00
// Render the floor
2013-07-02 21:14:08 +00:00
mFloor->render(mPhongShader, worldToCameraMatrix);
2013-04-25 22:46:09 +00:00
// Unbind the shader
mPhongShader.unbind();
}
2013-05-02 20:41:57 +00:00
// Create the boxes and joints for the Ball-and-Socket joint example
void Scene::createBallAndSocketJoints() {
2013-06-26 20:29:23 +00:00
// --------------- Create the boxes --------------- //
openglframework::Vector3 positionBox(0, 15, 5);
openglframework::Vector3 boxDimension(1, 1, 1);
const float boxMass = 0.5f;
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
// Create a box and a corresponding rigid in the dynamics world
mBallAndSocketJointChainBoxes[i] = new Box(boxDimension, positionBox , boxMass,
mDynamicsWorld);
// The fist box cannot move
if (i == 0) mBallAndSocketJointChainBoxes[i]->getRigidBody()->setIsMotionEnabled(false);
else mBallAndSocketJointChainBoxes[i]->getRigidBody()->setIsMotionEnabled(true);
// Add some angular velocity damping
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setAngularDamping(rp3d::decimal(0.2));
// Change the material properties of the rigid body
rp3d::Material& material = mBallAndSocketJointChainBoxes[i]->getRigidBody()->getMaterial();
material.setBounciness(rp3d::decimal(0.4));
2013-06-26 20:29:23 +00:00
positionBox.y -= boxDimension.y + 0.5f;
2013-06-26 20:29:23 +00:00
}
// --------------- Create the joints --------------- //
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES-1; i++) {
// Create the joint info object
rp3d::RigidBody* body1 = mBallAndSocketJointChainBoxes[i]->getRigidBody();
rp3d::RigidBody* body2 = mBallAndSocketJointChainBoxes[i+1]->getRigidBody();
rp3d::Vector3 body1Position = body1->getTransform().getPosition();
rp3d::Vector3 body2Position = body2->getTransform().getPosition();
const rp3d::Vector3 anchorPointWorldSpace = 0.5 * (body1Position + body2Position);
rp3d::BallAndSocketJointInfo jointInfo(body1, body2, anchorPointWorldSpace);
// Create the joint in the dynamics world
mBallAndSocketJoints[i] = dynamic_cast<rp3d::BallAndSocketJoint*>(
mDynamicsWorld->createJoint(jointInfo));
}
}
/// Create the boxes and joint for the Slider joint example
void Scene::createSliderJoint() {
2013-05-02 20:41:57 +00:00
// --------------- Create the first box --------------- //
// Position of the box
openglframework::Vector3 positionBox1(0, 2.1f, 0);
2013-05-02 20:41:57 +00:00
// Create a box and a corresponding rigid in the dynamics world
2013-06-26 20:29:23 +00:00
openglframework::Vector3 box1Dimension(2, 4, 2);
2013-09-10 19:53:34 +00:00
mSliderJointBottomBox = new Box(box1Dimension, positionBox1 , BOX_MASS, mDynamicsWorld);
2013-05-02 20:41:57 +00:00
// The fist box cannot move
2013-06-26 20:29:23 +00:00
mSliderJointBottomBox->getRigidBody()->setIsMotionEnabled(false);
2013-05-02 20:41:57 +00:00
// Change the material properties of the rigid body
rp3d::Material& material1 = mSliderJointBottomBox->getRigidBody()->getMaterial();
material1.setBounciness(0.4f);
2013-05-02 20:41:57 +00:00
// --------------- Create the second box --------------- //
// Position of the box
openglframework::Vector3 positionBox2(0, 4.2f, 0);
2013-05-02 20:41:57 +00:00
// Create a box and a corresponding rigid in the dynamics world
openglframework::Vector3 box2Dimension(1.5f, 4, 1.5f);
2013-09-10 19:53:34 +00:00
mSliderJointTopBox = new Box(box2Dimension, positionBox2 , BOX_MASS, mDynamicsWorld);
2013-05-02 20:41:57 +00:00
// The second box is allowed to move
2013-06-26 20:29:23 +00:00
mSliderJointTopBox->getRigidBody()->setIsMotionEnabled(true);
2013-05-02 20:41:57 +00:00
// Change the material properties of the rigid body
rp3d::Material& material2 = mSliderJointTopBox->getRigidBody()->getMaterial();
material2.setBounciness(0.4f);
2013-05-02 20:41:57 +00:00
// --------------- Create the joint --------------- //
// Create the joint info object
2013-06-26 20:29:23 +00:00
rp3d::RigidBody* body1 = mSliderJointBottomBox->getRigidBody();
rp3d::RigidBody* body2 = mSliderJointTopBox->getRigidBody();
const rp3d::Vector3& body1Position = body1->getTransform().getPosition();
const rp3d::Vector3& body2Position = body2->getTransform().getPosition();
const rp3d::Vector3 anchorPointWorldSpace = rp3d::decimal(0.5) * (body2Position + body1Position);
2013-06-26 20:29:23 +00:00
const rp3d::Vector3 sliderAxisWorldSpace = (body2Position - body1Position);
rp3d::SliderJointInfo jointInfo(body1, body2, anchorPointWorldSpace, sliderAxisWorldSpace,
rp3d::decimal(-1.7), rp3d::decimal(1.7));
2013-06-26 20:29:23 +00:00
jointInfo.isMotorEnabled = true;
jointInfo.motorSpeed = 0.0;
jointInfo.maxMotorForce = 10000.0;
jointInfo.isCollisionEnabled = false;
2013-05-02 20:41:57 +00:00
// Create the joint in the dynamics world
2013-06-26 20:29:23 +00:00
mSliderJoint = dynamic_cast<rp3d::SliderJoint*>(mDynamicsWorld->createJoint(jointInfo));
2013-05-02 20:41:57 +00:00
}
2013-06-26 20:29:23 +00:00
/// Create the boxes and joint for the Hinge joint example
void Scene::createPropellerHingeJoint() {
// --------------- Create the propeller box --------------- //
// Position of the box
openglframework::Vector3 positionBox1(0, 7, 0);
// Create a box and a corresponding rigid in the dynamics world
openglframework::Vector3 boxDimension(10, 1, 1);
2013-09-10 19:53:34 +00:00
mPropellerBox = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
2013-06-26 20:29:23 +00:00
// The fist box cannot move
mPropellerBox->getRigidBody()->setIsMotionEnabled(true);
// Change the material properties of the rigid body
rp3d::Material& material = mPropellerBox->getRigidBody()->getMaterial();
material.setBounciness(rp3d::decimal(0.4));
2013-06-26 20:29:23 +00:00
// --------------- Create the Hinge joint --------------- //
// Create the joint info object
rp3d::RigidBody* body1 = mPropellerBox->getRigidBody();
rp3d::RigidBody* body2 = mSliderJointTopBox->getRigidBody();
const rp3d::Vector3& body1Position = body1->getTransform().getPosition();
const rp3d::Vector3& body2Position = body2->getTransform().getPosition();
const rp3d::Vector3 anchorPointWorldSpace = 0.5 * (body2Position + body1Position);
const rp3d::Vector3 hingeAxisWorldSpace(0, 1, 0);
rp3d::HingeJointInfo jointInfo(body1, body2, anchorPointWorldSpace, hingeAxisWorldSpace);
jointInfo.isMotorEnabled = true;
jointInfo.motorSpeed = - rp3d::decimal(0.5) * PI;
jointInfo.maxMotorTorque = rp3d::decimal(60.0);
2013-06-26 20:29:23 +00:00
jointInfo.isCollisionEnabled = false;
// Create the joint in the dynamics world
mPropellerHingeJoint = dynamic_cast<rp3d::HingeJoint*>(mDynamicsWorld->createJoint(jointInfo));
}
/// Create the boxes and joints for the fixed joints
void Scene::createFixedJoints() {
// --------------- Create the first box --------------- //
// Position of the box
2013-06-26 20:29:23 +00:00
openglframework::Vector3 positionBox1(5, 7, 0);
// Create a box and a corresponding rigid in the dynamics world
2013-06-26 20:29:23 +00:00
openglframework::Vector3 boxDimension(1.5, 1.5, 1.5);
2013-09-10 19:53:34 +00:00
mFixedJointBox1 = new Box(boxDimension, positionBox1 , BOX_MASS, mDynamicsWorld);
// The fist box cannot move
2013-06-26 20:29:23 +00:00
mFixedJointBox1->getRigidBody()->setIsMotionEnabled(true);
// Change the material properties of the rigid body
rp3d::Material& material1 = mFixedJointBox1->getRigidBody()->getMaterial();
material1.setBounciness(rp3d::decimal(0.4));
// --------------- Create the second box --------------- //
// Position of the box
2013-06-26 20:29:23 +00:00
openglframework::Vector3 positionBox2(-5, 7, 0);
// Create a box and a corresponding rigid in the dynamics world
2013-09-10 19:53:34 +00:00
mFixedJointBox2 = new Box(boxDimension, positionBox2 , BOX_MASS, mDynamicsWorld);
// The second box is allowed to move
2013-06-26 20:29:23 +00:00
mFixedJointBox2->getRigidBody()->setIsMotionEnabled(true);
// Change the material properties of the rigid body
rp3d::Material& material2 = mFixedJointBox2->getRigidBody()->getMaterial();
material2.setBounciness(rp3d::decimal(0.4));
2013-06-26 20:29:23 +00:00
// --------------- Create the first fixed joint --------------- //
// Create the joint info object
2013-06-26 20:29:23 +00:00
rp3d::RigidBody* body1 = mFixedJointBox1->getRigidBody();
rp3d::RigidBody* propellerBody = mPropellerBox->getRigidBody();
const rp3d::Vector3 anchorPointWorldSpace1(5, 7, 0);
rp3d::FixedJointInfo jointInfo1(body1, propellerBody, anchorPointWorldSpace1);
2013-07-03 20:42:58 +00:00
jointInfo1.isCollisionEnabled = false;
// Create the joint in the dynamics world
2013-06-26 20:29:23 +00:00
mFixedJoint1 = dynamic_cast<rp3d::FixedJoint*>(mDynamicsWorld->createJoint(jointInfo1));
// --------------- Create the second fixed joint --------------- //
// Create the joint info object
rp3d::RigidBody* body2 = mFixedJointBox2->getRigidBody();
const rp3d::Vector3 anchorPointWorldSpace2(-5, 7, 0);
rp3d::FixedJointInfo jointInfo2(body2, propellerBody, anchorPointWorldSpace2);
2013-07-03 20:42:58 +00:00
jointInfo2.isCollisionEnabled = false;
2013-06-26 20:29:23 +00:00
// Create the joint in the dynamics world
mFixedJoint2 = dynamic_cast<rp3d::FixedJoint*>(mDynamicsWorld->createJoint(jointInfo2));
}
2013-06-26 20:29:23 +00:00
2013-05-02 20:41:57 +00:00
// Create the floor
void Scene::createFloor() {
// Create the floor
openglframework::Vector3 floorPosition(0, 0, 0);
mFloor = new Box(FLOOR_SIZE, floorPosition, FLOOR_MASS, mDynamicsWorld);
// The floor must be a non-moving rigid body
mFloor->getRigidBody()->setIsMotionEnabled(false);
// Change the material properties of the rigid body
rp3d::Material& material = mFloor->getRigidBody()->getMaterial();
material.setBounciness(rp3d::decimal(0.3));
2013-05-02 20:41:57 +00:00
}