2015-04-08 18:47:55 +00:00
|
|
|
/********************************************************************************
|
|
|
|
* ReactPhysics3D physics library, http://www.reactphysics3d.com *
|
2016-04-11 18:15:20 +00:00
|
|
|
* Copyright (c) 2010-2016 Daniel Chappuis *
|
2015-04-08 18:47:55 +00:00
|
|
|
*********************************************************************************
|
|
|
|
* *
|
|
|
|
* 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
|
2015-04-15 21:11:27 +00:00
|
|
|
#include "JointsScene.h"
|
2015-04-08 18:47:55 +00:00
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
// Namespaces
|
|
|
|
using namespace openglframework;
|
2015-04-15 21:11:27 +00:00
|
|
|
using namespace jointsscene;
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Constructor
|
2017-11-02 21:58:41 +00:00
|
|
|
JointsScene::JointsScene(const std::string& name, EngineSettings& settings)
|
2020-01-27 16:46:00 +00:00
|
|
|
: SceneDemo(name, settings, true, SCENE_RADIUS) {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Compute the radius and the center of the scene
|
|
|
|
openglframework::Vector3 center(0, 5, 0);
|
|
|
|
|
|
|
|
// Set the center of the scene
|
2015-07-29 16:15:20 +00:00
|
|
|
setScenePosition(center, SCENE_RADIUS);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Gravity vector in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
rp3d::Vector3 gravity(0, rp3d::decimal(-9.81), 0);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
rp3d::PhysicsWorld::WorldSettings worldSettings;
|
2018-03-15 22:11:26 +00:00
|
|
|
worldSettings.worldName = name;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the physics world for the physics simulation
|
|
|
|
rp3d::PhysicsWorld* physicsWorld = mPhysicsCommon.createPhysicsWorld(worldSettings);
|
|
|
|
physicsWorld->setEventListener(this);
|
|
|
|
mPhysicsWorld = physicsWorld;
|
2017-11-10 18:57:50 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Create the Ball-and-Socket joint
|
|
|
|
createBallAndSocketJoints();
|
|
|
|
|
|
|
|
// Create the Slider joint
|
|
|
|
createSliderJoint();
|
|
|
|
|
|
|
|
// Create the Hinge joint
|
|
|
|
createPropellerHingeJoint();
|
|
|
|
|
|
|
|
// Create the Fixed joint
|
|
|
|
createFixedJoints();
|
|
|
|
|
|
|
|
// Create the floor
|
|
|
|
createFloor();
|
2015-07-11 16:35:58 +00:00
|
|
|
|
|
|
|
// Get the physics engine parameters
|
2020-01-27 16:46:00 +00:00
|
|
|
mEngineSettings.isGravityEnabled = mPhysicsWorld->isGravityEnabled();
|
|
|
|
rp3d::Vector3 gravityVector = mPhysicsWorld->getGravity();
|
2015-07-11 16:35:58 +00:00
|
|
|
mEngineSettings.gravity = openglframework::Vector3(gravityVector.x, gravityVector.y, gravityVector.z);
|
2020-01-27 16:46:00 +00:00
|
|
|
mEngineSettings.isSleepingEnabled = mPhysicsWorld->isSleepingEnabled();
|
|
|
|
mEngineSettings.sleepLinearVelocity = mPhysicsWorld->getSleepLinearVelocity();
|
|
|
|
mEngineSettings.sleepAngularVelocity = mPhysicsWorld->getSleepAngularVelocity();
|
|
|
|
mEngineSettings.nbPositionSolverIterations = mPhysicsWorld->getNbIterationsPositionSolver();
|
|
|
|
mEngineSettings.nbVelocitySolverIterations = mPhysicsWorld->getNbIterationsVelocitySolver();
|
|
|
|
mEngineSettings.timeBeforeSleep = mPhysicsWorld->getTimeBeforeSleep();
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor
|
2015-04-15 21:11:27 +00:00
|
|
|
JointsScene::~JointsScene() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Destroy the joints
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->destroyJoint(mSliderJoint);
|
|
|
|
mPhysicsWorld->destroyJoint(mPropellerHingeJoint);
|
|
|
|
mPhysicsWorld->destroyJoint(mFixedJoint1);
|
|
|
|
mPhysicsWorld->destroyJoint(mFixedJoint2);
|
2015-04-08 18:47:55 +00:00
|
|
|
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES-1; i++) {
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->destroyJoint(mBallAndSocketJoints[i]);
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy all the rigid bodies of the scene
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->destroyRigidBody(mSliderJointBottomBox->getRigidBody());
|
|
|
|
mPhysicsWorld->destroyRigidBody(mSliderJointTopBox->getRigidBody());
|
|
|
|
mPhysicsWorld->destroyRigidBody(mPropellerBox->getRigidBody());
|
|
|
|
mPhysicsWorld->destroyRigidBody(mFixedJointBox1->getRigidBody());
|
|
|
|
mPhysicsWorld->destroyRigidBody(mFixedJointBox2->getRigidBody());
|
2015-04-08 18:47:55 +00:00
|
|
|
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->destroyRigidBody(mBallAndSocketJointChainBoxes[i]->getRigidBody());
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
delete mSliderJointBottomBox;
|
|
|
|
delete mSliderJointTopBox;
|
|
|
|
delete mPropellerBox;
|
|
|
|
delete mFixedJointBox1;
|
|
|
|
delete mFixedJointBox2;
|
|
|
|
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
|
|
|
|
delete mBallAndSocketJointChainBoxes[i];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destroy the floor
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->destroyRigidBody(mFloor->getRigidBody());
|
2015-04-08 18:47:55 +00:00
|
|
|
delete mFloor;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Destroy the physics world
|
|
|
|
mPhysicsCommon.destroyPhysicsWorld(mPhysicsWorld);
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-20 21:23:46 +00:00
|
|
|
// Update the physics world (take a simulation step)
|
|
|
|
void JointsScene::updatePhysics() {
|
2015-04-15 21:11:27 +00:00
|
|
|
|
|
|
|
// Update the motor speed of the Slider Joint (to move up and down)
|
2017-11-02 21:58:41 +00:00
|
|
|
double motorSpeed = 2.0 * std::cos(static_cast<double>(mEngineSettings.elapsedTime) * 1.5);
|
2015-04-15 21:11:27 +00:00
|
|
|
mSliderJoint->setMotorSpeed(rp3d::decimal(motorSpeed));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
SceneDemo::updatePhysics();
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
2015-04-15 21:11:27 +00:00
|
|
|
// Reset the scene
|
|
|
|
void JointsScene::reset() {
|
|
|
|
|
2019-06-25 21:26:06 +00:00
|
|
|
SceneDemo::reset();
|
|
|
|
|
2015-06-25 20:28:11 +00:00
|
|
|
openglframework::Vector3 positionBox(0, 15, 5);
|
|
|
|
openglframework::Vector3 boxDimension(1, 1, 1);
|
|
|
|
|
|
|
|
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
|
|
|
|
|
|
|
|
// Initial position and orientation of the rigid body
|
|
|
|
rp3d::Vector3 initPosition(positionBox.x, positionBox.y, positionBox.z);
|
|
|
|
rp3d::Quaternion initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transform(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mBallAndSocketJointChainBoxes[i]->setTransform(transform);
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
positionBox.y -= boxDimension.y + 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------- Slider Joint --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
|
|
|
openglframework::Vector3 positionBox1(0, 2.1f, 0);
|
|
|
|
rp3d::Vector3 initPosition(positionBox1.x, positionBox1.y, positionBox1.z);
|
|
|
|
rp3d::Quaternion initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transformBottomBox(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mSliderJointBottomBox->setTransform(transformBottomBox);
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
// Position of the box
|
|
|
|
openglframework::Vector3 positionBox2(0, 4.2f, 0);
|
|
|
|
initPosition = rp3d::Vector3(positionBox2.x, positionBox2.y, positionBox2.z);
|
|
|
|
initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transformTopBox(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mSliderJointTopBox->setTransform(transformTopBox);
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
// --------------- Propeller Hinge joint --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
|
|
|
positionBox1 = openglframework::Vector3(0, 7, 0);
|
|
|
|
initPosition = rp3d::Vector3(positionBox1.x, positionBox1.y, positionBox1.z);
|
|
|
|
initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transformHingeBox(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mPropellerBox->setTransform(transformHingeBox);
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
// --------------- Fixed joint --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
|
|
|
positionBox1 = openglframework::Vector3(5, 7, 0);
|
|
|
|
initPosition = rp3d::Vector3(positionBox1.x, positionBox1.y, positionBox1.z);
|
|
|
|
initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transformFixedBox1(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mFixedJointBox1->setTransform(transformFixedBox1);
|
2015-06-25 20:28:11 +00:00
|
|
|
|
|
|
|
// Position of the box
|
|
|
|
positionBox2 = openglframework::Vector3(-5, 7, 0);
|
|
|
|
initPosition = rp3d::Vector3(positionBox2.x, positionBox2.y, positionBox2.z);
|
|
|
|
initOrientation = rp3d::Quaternion::identity();
|
|
|
|
rp3d::Transform transformFixedBox2(initPosition, initOrientation);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2017-01-28 14:23:05 +00:00
|
|
|
mFixedJointBox2->setTransform(transformFixedBox2);
|
2015-04-15 21:11:27 +00:00
|
|
|
}
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Create the boxes and joints for the Ball-and-Socket joint example
|
2015-04-15 21:11:27 +00:00
|
|
|
void JointsScene::createBallAndSocketJoints() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the boxes --------------- //
|
|
|
|
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox(0, 15, 5);
|
2015-04-08 18:47:55 +00:00
|
|
|
openglframework::Vector3 boxDimension(1, 1, 1);
|
|
|
|
const float boxMass = 0.5f;
|
|
|
|
|
|
|
|
for (int i=0; i<NB_BALLSOCKETJOINT_BOXES; i++) {
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2020-03-01 15:39:16 +00:00
|
|
|
mBallAndSocketJointChainBoxes[i] = new Box(true, boxDimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mBallAndSocketJointChainBoxes[i]->setTransform(rp3d::Transform(positionBox, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mBallAndSocketJointChainBoxes[i]->setColor(mObjectColorDemo);
|
|
|
|
mBallAndSocketJointChainBoxes[i]->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// The fist box cannot move (static body)
|
|
|
|
if (i == 0) {
|
2016-07-08 05:25:37 +00:00
|
|
|
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setType(rp3d::BodyType::STATIC);
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Add some angular velocity damping
|
|
|
|
mBallAndSocketJointChainBoxes[i]->getRigidBody()->setAngularDamping(rp3d::decimal(0.2));
|
|
|
|
|
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material = mBallAndSocketJointChainBoxes[i]->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material.setBounciness(rp3d::decimal(0.4));
|
|
|
|
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mBallAndSocketJointChainBoxes[i]);
|
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
positionBox.y -= boxDimension.y + 0.5f;
|
|
|
|
}
|
|
|
|
|
|
|
|
// --------------- 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);
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the joint in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
mBallAndSocketJoints[i] = dynamic_cast<rp3d::BallAndSocketJoint*>(
|
2020-01-27 16:46:00 +00:00
|
|
|
mPhysicsWorld->createJoint(jointInfo));
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the boxes and joint for the Slider joint example
|
2015-04-15 21:11:27 +00:00
|
|
|
void JointsScene::createSliderJoint() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the first box --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox1(0, 2.1f, 0);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
openglframework::Vector3 box1Dimension(2, 4, 2);
|
2020-03-01 15:39:16 +00:00
|
|
|
mSliderJointBottomBox = new Box(true, box1Dimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mSliderJointBottomBox->setTransform(rp3d::Transform(positionBox1, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mSliderJointBottomBox->setColor(mObjectColorDemo);
|
|
|
|
mSliderJointBottomBox->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// The fist box cannot move
|
2016-07-08 05:25:37 +00:00
|
|
|
mSliderJointBottomBox->getRigidBody()->setType(rp3d::BodyType::STATIC);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material1 = mSliderJointBottomBox->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material1.setBounciness(0.4f);
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mSliderJointBottomBox);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the second box --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox2(0, 4.2f, 0);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
openglframework::Vector3 box2Dimension(1.5f, 4, 1.5f);
|
2020-03-01 15:39:16 +00:00
|
|
|
mSliderJointTopBox = new Box(true, box2Dimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mSliderJointTopBox->setTransform(rp3d::Transform(positionBox2, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mSliderJointTopBox->setColor(mObjectColorDemo);
|
|
|
|
mSliderJointTopBox->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material2 = mSliderJointTopBox->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material2.setBounciness(0.4f);
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mSliderJointTopBox);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the joint --------------- //
|
|
|
|
|
|
|
|
// Create the joint info object
|
|
|
|
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);
|
|
|
|
const rp3d::Vector3 sliderAxisWorldSpace = (body2Position - body1Position);
|
|
|
|
rp3d::SliderJointInfo jointInfo(body1, body2, anchorPointWorldSpace, sliderAxisWorldSpace,
|
|
|
|
rp3d::decimal(-1.7), rp3d::decimal(1.7));
|
|
|
|
jointInfo.isMotorEnabled = true;
|
|
|
|
jointInfo.motorSpeed = 0.0;
|
|
|
|
jointInfo.maxMotorForce = 10000.0;
|
|
|
|
jointInfo.isCollisionEnabled = false;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the joint in the physics world
|
|
|
|
mSliderJoint = dynamic_cast<rp3d::SliderJoint*>(mPhysicsWorld->createJoint(jointInfo));
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the boxes and joint for the Hinge joint example
|
2015-04-15 21:11:27 +00:00
|
|
|
void JointsScene::createPropellerHingeJoint() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the propeller box --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox1(0, 7, 0);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
openglframework::Vector3 boxDimension(10, 1, 1);
|
2020-03-01 15:39:16 +00:00
|
|
|
mPropellerBox = new Box(true, boxDimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mPropellerBox->setTransform(rp3d::Transform(positionBox1, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mPropellerBox->setColor(mObjectColorDemo);
|
|
|
|
mPropellerBox->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material = mPropellerBox->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material.setBounciness(rp3d::decimal(0.4));
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mPropellerBox);
|
2015-04-08 18:47:55 +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);
|
|
|
|
jointInfo.isCollisionEnabled = false;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the joint in the physics world
|
|
|
|
mPropellerHingeJoint = dynamic_cast<rp3d::HingeJoint*>(mPhysicsWorld->createJoint(jointInfo));
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Create the boxes and joints for the fixed joints
|
2015-04-15 21:11:27 +00:00
|
|
|
void JointsScene::createFixedJoints() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the first box --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox1(5, 7, 0);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2015-04-08 18:47:55 +00:00
|
|
|
openglframework::Vector3 boxDimension(1.5, 1.5, 1.5);
|
2020-03-01 15:39:16 +00:00
|
|
|
mFixedJointBox1 = new Box(true, boxDimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mFixedJointBox1->setTransform(rp3d::Transform(positionBox1, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mFixedJointBox1->setColor(mObjectColorDemo);
|
|
|
|
mFixedJointBox1->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material1 = mFixedJointBox1->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material1.setBounciness(rp3d::decimal(0.4));
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mFixedJointBox1);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the second box --------------- //
|
|
|
|
|
|
|
|
// Position of the box
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 positionBox2(-5, 7, 0);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create a box and a corresponding rigid in the physics world
|
2020-03-01 15:39:16 +00:00
|
|
|
mFixedJointBox2 = new Box(true, boxDimension, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2017-11-02 21:58:41 +00:00
|
|
|
mFixedJointBox2->setTransform(rp3d::Transform(positionBox2, rp3d::Quaternion::identity()));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
2015-08-08 09:40:37 +00:00
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mFixedJointBox2->setColor(mObjectColorDemo);
|
|
|
|
mFixedJointBox2->setSleepingColor(mSleepingColorDemo);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
2015-04-08 18:47:55 +00:00
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material2 = mFixedJointBox2->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material2.setBounciness(rp3d::decimal(0.4));
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mFixedJointBox2);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- Create the first fixed joint --------------- //
|
|
|
|
|
|
|
|
// Create the joint info object
|
|
|
|
rp3d::RigidBody* body1 = mFixedJointBox1->getRigidBody();
|
|
|
|
rp3d::RigidBody* propellerBody = mPropellerBox->getRigidBody();
|
|
|
|
const rp3d::Vector3 anchorPointWorldSpace1(5, 7, 0);
|
|
|
|
rp3d::FixedJointInfo jointInfo1(body1, propellerBody, anchorPointWorldSpace1);
|
|
|
|
jointInfo1.isCollisionEnabled = false;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the joint in the physics world
|
|
|
|
mFixedJoint1 = dynamic_cast<rp3d::FixedJoint*>(mPhysicsWorld->createJoint(jointInfo1));
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// --------------- 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);
|
|
|
|
jointInfo2.isCollisionEnabled = false;
|
|
|
|
|
2020-01-27 16:46:00 +00:00
|
|
|
// Create the joint in the physics world
|
|
|
|
mFixedJoint2 = dynamic_cast<rp3d::FixedJoint*>(mPhysicsWorld->createJoint(jointInfo2));
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the floor
|
2015-04-15 21:11:27 +00:00
|
|
|
void JointsScene::createFloor() {
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Create the floor
|
2017-11-02 21:58:41 +00:00
|
|
|
rp3d::Vector3 floorPosition(0, 0, 0);
|
2020-03-01 15:39:16 +00:00
|
|
|
mFloor = new Box(true, FLOOR_SIZE, mPhysicsCommon, mPhysicsWorld, mMeshFolderPath);
|
2015-08-08 09:40:37 +00:00
|
|
|
|
|
|
|
// Set the box color
|
2019-10-21 05:22:18 +00:00
|
|
|
mFloor->setColor(mFloorColorDemo);
|
|
|
|
mFloor->setSleepingColor(mFloorColorDemo);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// The floor must be a static rigid body
|
2016-07-08 05:25:37 +00:00
|
|
|
mFloor->getRigidBody()->setType(rp3d::BodyType::STATIC);
|
2015-04-08 18:47:55 +00:00
|
|
|
|
|
|
|
// Change the material properties of the rigid body
|
2020-02-01 12:41:30 +00:00
|
|
|
rp3d::Material& material = mFloor->getCollider()->getMaterial();
|
2015-04-08 18:47:55 +00:00
|
|
|
material.setBounciness(rp3d::decimal(0.3));
|
2017-10-19 15:42:21 +00:00
|
|
|
mPhysicsObjects.push_back(mFloor);
|
2015-04-08 18:47:55 +00:00
|
|
|
}
|