Refactor testbed application

This commit is contained in:
Daniel Chappuis 2017-01-28 11:38:48 +01:00
parent a9da0d6d3c
commit 0b0975769b
21 changed files with 54 additions and 296 deletions

View File

@ -39,13 +39,8 @@ int Box::totalNbBoxes = 0;
// Constructor
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cube.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath)
: PhysicsObject(meshFolderPath + "cube.obj") {
// Initialize the size of the box
mSize[0] = size.x * 0.5f;
@ -93,7 +88,8 @@ Box::Box(const openglframework::Vector3& size, const openglframework::Vector3 &p
// Constructor
Box::Box(const openglframework::Vector3& size, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* world, const std::string& meshFolderPath) {
float mass, reactphysics3d::DynamicsWorld* world, const std::string& meshFolderPath)
: PhysicsObject(meshFolderPath + "cube.obj") {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cube.obj", *this);
@ -284,24 +280,6 @@ void Box::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Box::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Box::setScaling(const openglframework::Vector3& scaling) {

View File

@ -88,9 +88,6 @@ class Box : public PhysicsObject {
/// Render the cube at the correct position and with the correct orientation
void render(openglframework::Shader& shader, const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -37,13 +37,7 @@ int Capsule::totalNbCapsules = 0;
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "capsule.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -86,13 +80,7 @@ Capsule::Capsule(float radius, float height, const openglframework::Vector3& pos
Capsule::Capsule(float radius, float height, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "capsule.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "capsule.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -272,24 +260,6 @@ void Capsule::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Capsule::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Capsule::setScaling(const openglframework::Vector3& scaling) {

View File

@ -97,9 +97,6 @@ class Capsule : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -30,16 +30,10 @@
ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world,
const std::string& meshPath)
: mVBOVertices(GL_ARRAY_BUFFER),
: PhysicsObject(meshPath), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh
calculateNormals();
// Initialize the position where the sphere will be rendered
translateWorld(position);
@ -87,16 +81,10 @@ ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position,
ConcaveMesh::ConcaveMesh(const openglframework::Vector3 &position, float mass,
reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshPath)
: mVBOVertices(GL_ARRAY_BUFFER),
: PhysicsObject(meshPath), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh
calculateNormals();
// Initialize the position where the sphere will be rendered
translateWorld(position);
@ -285,24 +273,6 @@ void ConcaveMesh::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void ConcaveMesh::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void ConcaveMesh::setScaling(const openglframework::Vector3& scaling) {

View File

@ -90,9 +90,6 @@ class ConcaveMesh : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -37,13 +37,7 @@ int Cone::totalNbCones = 0;
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "cone.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -85,13 +79,7 @@ Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
Cone::Cone(float radius, float height, const openglframework::Vector3 &position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cone.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "cone.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -269,24 +257,6 @@ void Cone::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Cone::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Cone::setScaling(const openglframework::Vector3& scaling) {

View File

@ -96,9 +96,6 @@ class Cone : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -30,16 +30,10 @@
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world,
const std::string& meshPath)
: mVBOVertices(GL_ARRAY_BUFFER),
: PhysicsObject(meshPath), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh
calculateNormals();
// Initialize the position where the sphere will be rendered
translateWorld(position);
@ -81,16 +75,10 @@ ConvexMesh::ConvexMesh(const openglframework::Vector3 &position,
ConvexMesh::ConvexMesh(const openglframework::Vector3 &position, float mass,
reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshPath)
: mVBOVertices(GL_ARRAY_BUFFER),
: PhysicsObject(meshPath), mVBOVertices(GL_ARRAY_BUFFER),
mVBONormals(GL_ARRAY_BUFFER), mVBOTextureCoords(GL_ARRAY_BUFFER),
mVBOIndices(GL_ELEMENT_ARRAY_BUFFER) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh
calculateNormals();
// Initialize the position where the sphere will be rendered
translateWorld(position);
@ -266,24 +254,6 @@ void ConvexMesh::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void ConvexMesh::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void ConvexMesh::setScaling(const openglframework::Vector3& scaling) {

View File

@ -89,9 +89,6 @@ class ConvexMesh : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -37,13 +37,7 @@ int Cylinder::totalNbCylinders = 0;
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "cylinder.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -85,13 +79,7 @@ Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& p
Cylinder::Cylinder(float radius, float height, const openglframework::Vector3& position,
float mass, reactphysics3d::DynamicsWorld* dynamicsWorld,
const std::string& meshFolderPath)
: mRadius(radius), mHeight(height) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "cylinder.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "cylinder.obj"), mRadius(radius), mHeight(height) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -270,24 +258,6 @@ void Cylinder::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Cylinder::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Cylinder::setScaling(const openglframework::Vector3& scaling) {

View File

@ -96,9 +96,6 @@ class Cylinder : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -35,13 +35,8 @@ int Dumbbell::totalNbDumbbells = 0;
// Constructor
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
reactphysics3d::DynamicsWorld* dynamicsWorld, const std::string& meshFolderPath)
: PhysicsObject(meshFolderPath + "dumbbell.obj") {
// Identity scaling matrix
mScalingMatrix.setToIdentity();
@ -105,13 +100,8 @@ Dumbbell::Dumbbell(const openglframework::Vector3 &position,
// Constructor
Dumbbell::Dumbbell(const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "dumbbell.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
reactphysics3d::CollisionWorld* world, const std::string& meshFolderPath)
: PhysicsObject(meshFolderPath + "dumbbell.obj"){
// Identity scaling matrix
mScalingMatrix.setToIdentity();
@ -309,24 +299,6 @@ void Dumbbell::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Dumbbell::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != NULL) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Dumbbell::setScaling(const openglframework::Vector3& scaling) {

View File

@ -97,9 +97,6 @@ class Dumbbell : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -328,24 +328,6 @@ void HeightField::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void HeightField::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != NULL) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void HeightField::setScaling(const openglframework::Vector3& scaling) {

View File

@ -103,9 +103,6 @@ class HeightField : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -33,6 +33,16 @@ PhysicsObject::PhysicsObject() : openglframework::Mesh() {
mSleepingColor = openglframework::Color(1, 0, 0, 1);
}
/// Constructor
PhysicsObject::PhysicsObject(const std::string& meshPath) : PhysicsObject() {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshPath, *this);
// Calculate the normals of the mesh
calculateNormals();
}
// Compute the new transform matrix
openglframework::Matrix4 PhysicsObject::computeTransform(float interpolationFactor,
const openglframework::Matrix4& scalingMatrix) {
@ -57,3 +67,21 @@ openglframework::Matrix4 PhysicsObject::computeTransform(float interpolationFact
// Apply the scaling matrix to have the correct box dimensions
return newMatrix * scalingMatrix;
}
// Reset the transform
void PhysicsObject::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != nullptr) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}

View File

@ -56,6 +56,9 @@ class PhysicsObject : public openglframework::Mesh {
/// Constructor
PhysicsObject();
/// Constructor
PhysicsObject(const std::string& meshPath);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor)=0;
@ -65,6 +68,9 @@ class PhysicsObject : public openglframework::Mesh {
/// Set the sleeping color of the box
void setSleepingColor(const openglframework::Color& color);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Return a pointer to the collision body of the box
reactphysics3d::CollisionBody* getCollisionBody();

View File

@ -37,13 +37,7 @@ int Sphere::totalNbSpheres = 0;
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
reactphysics3d::CollisionWorld* world,
const std::string& meshFolderPath)
: mRadius(radius) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "sphere.obj"), mRadius(radius) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -86,13 +80,7 @@ Sphere::Sphere(float radius, const openglframework::Vector3 &position,
Sphere::Sphere(float radius, const openglframework::Vector3 &position,
float mass, reactphysics3d::DynamicsWorld* world,
const std::string& meshFolderPath)
: mRadius(radius) {
// Load the mesh from a file
openglframework::MeshReaderWriter::loadMeshFromFile(meshFolderPath + "sphere.obj", *this);
// Calculate the normals of the mesh
calculateNormals();
: PhysicsObject(meshFolderPath + "sphere.obj"), mRadius(radius) {
// Compute the scaling matrix
mScalingMatrix = openglframework::Matrix4(mRadius, 0, 0, 0,
@ -270,24 +258,6 @@ void Sphere::createVBOAndVAO() {
mVAO.unbind();
}
// Reset the transform
void Sphere::resetTransform(const rp3d::Transform& transform) {
// Reset the transform
mBody->setTransform(transform);
mBody->setIsSleeping(false);
// Reset the velocity of the rigid body
rp3d::RigidBody* rigidBody = dynamic_cast<rp3d::RigidBody*>(mBody);
if (rigidBody != NULL) {
rigidBody->setLinearVelocity(rp3d::Vector3(0, 0, 0));
rigidBody->setAngularVelocity(rp3d::Vector3(0, 0, 0));
}
updateTransform(1.0f);
}
// Set the scaling of the object
void Sphere::setScaling(const openglframework::Vector3& scaling) {

View File

@ -93,9 +93,6 @@ class Sphere : public PhysicsObject {
void render(openglframework::Shader& shader,
const openglframework::Matrix4& worldToCameraMatrix, bool wireframe);
/// Set the position of the box
void resetTransform(const rp3d::Transform& transform);
/// Update the transform matrix of the object
virtual void updateTransform(float interpolationFactor) override;

View File

@ -27,7 +27,6 @@
#define COLLISION_DETECTION_SCENE_H
// Libraries
#define _USE_MATH_DEFINES
#include <cmath>
#include "openglframework.h"
#include "reactphysics3d.h"