2009-02-04 14:43:33 +00:00
|
|
|
/****************************************************************************
|
|
|
|
* Copyright (C) 2009 Daniel Chappuis *
|
|
|
|
****************************************************************************
|
|
|
|
* This file is part of ReactPhysics3D. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is free software: you can redistribute it and/or modify *
|
2009-06-05 14:22:40 +00:00
|
|
|
* it under the terms of the GNU Lesser General Public License as published *
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or *
|
2009-02-04 14:43:33 +00:00
|
|
|
* (at your option) any later version. *
|
|
|
|
* *
|
|
|
|
* ReactPhysics3D is distributed in the hope that it will be useful, *
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
2009-06-05 14:22:40 +00:00
|
|
|
* GNU Lesser General Public License for more details. *
|
2009-02-04 14:43:33 +00:00
|
|
|
* *
|
2009-06-05 14:22:40 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License *
|
2009-02-04 14:43:33 +00:00
|
|
|
* along with ReactPhysics3D. If not, see <http://www.gnu.org/licenses/>. *
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
// Libraries
|
|
|
|
#include "Objects.h"
|
|
|
|
|
|
|
|
//#include <windows.h> // To avoid an error due to the #include <GL/glut.h>
|
2009-02-13 11:26:51 +00:00
|
|
|
#include <GL/freeglut.h>
|
|
|
|
#include <GL/gl.h>
|
|
|
|
#include <GL/glu.h>
|
|
|
|
#include <cmath>
|
|
|
|
#include <iostream>
|
2009-02-04 14:43:33 +00:00
|
|
|
|
|
|
|
// ----- Class Object ----- //
|
|
|
|
|
|
|
|
// Constructor of the class Object
|
2009-08-04 12:21:15 +00:00
|
|
|
Object::Object(const Vector3D& position, const Quaternion& orientation, const Kilogram& mass, const Matrix3x3& inertiaTensor, const OBB& obb)
|
|
|
|
:rigidBody(new RigidBody(position, orientation, mass, inertiaTensor, obb)) {
|
2009-02-13 11:26:51 +00:00
|
|
|
|
2009-02-04 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor of the class Object
|
|
|
|
Object::~Object() {
|
2009-02-13 11:26:51 +00:00
|
|
|
// Delete the rigid body object
|
|
|
|
delete rigidBody;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return the pointer to the rigid body
|
|
|
|
RigidBody* Object::getRigidBody() {
|
|
|
|
return rigidBody;
|
2009-02-04 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ----- Class Cube ----- //
|
2009-02-13 11:26:51 +00:00
|
|
|
|
|
|
|
// Static attributes
|
|
|
|
const Matrix3x3 Cube::inertiaTensor;
|
2009-02-04 14:43:33 +00:00
|
|
|
|
|
|
|
// Constructor of the class Cube
|
2009-08-04 12:21:15 +00:00
|
|
|
Cube::Cube(const Vector3D& position, const Quaternion& orientation, float size, const Kilogram& mass)
|
|
|
|
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*2*size*size, 0.0, 0.0,
|
2009-02-13 11:26:51 +00:00
|
|
|
0.0, 1.0/12.0*mass.getValue()*2*size*size, 0.0,
|
2009-07-21 20:28:27 +00:00
|
|
|
0.0, 0.0, 1.0/12.0*mass.getValue()*2*size*size), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
|
|
|
size/2, size/2, size/2)) {
|
2009-02-04 14:43:33 +00:00
|
|
|
this->size = size;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor of the classe Cube
|
|
|
|
Cube::~Cube() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method to draw the cube
|
2009-02-13 11:26:51 +00:00
|
|
|
void Cube::draw() const {
|
|
|
|
|
|
|
|
// 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
|
2009-04-10 15:36:44 +00:00
|
|
|
glRotatef(orientationAngle/PI*180.0, orientationAxis.getX(), orientationAxis.getY(), orientationAxis.getZ());
|
2009-02-13 11:26:51 +00:00
|
|
|
|
|
|
|
// Draw the cube
|
|
|
|
glutSolidCube(size);
|
2009-02-04 14:43:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// ----- Class Plane ----- //
|
|
|
|
|
|
|
|
// Constructor of the class Plane
|
2009-08-04 12:21:15 +00:00
|
|
|
Plane::Plane(const Vector3D& position, const Quaternion& orientation, float width, float height, const Vector3D& d1, const Vector3D& d2, const Kilogram& mass)
|
|
|
|
:Object(position, orientation, mass, Matrix3x3(1.0/12.0*mass.getValue()*height*height, 0.0, 0.0,
|
2009-02-13 11:26:51 +00:00
|
|
|
0.0, 1.0/12.0*mass.getValue()*(width*width+height*height), 0.0,
|
2009-07-21 20:28:27 +00:00
|
|
|
0.0, 0.0, 1.0/12.0*mass.getValue()*width*width), OBB(position, Vector3D(1.0, 0.0, 0.0), Vector3D(0.0, 1.0, 0.0), Vector3D(0.0, 0.0, 1.0),
|
|
|
|
width/2, 0.5, height/2)) { // TODO : Change the height of the OBB
|
2009-02-04 14:43:33 +00:00
|
|
|
this->width = width;
|
|
|
|
this->height = height;
|
|
|
|
this->d1 = d1;
|
2009-02-13 11:26:51 +00:00
|
|
|
this->d2 = d2;
|
|
|
|
|
|
|
|
// By default Planes in the demo cannot move
|
|
|
|
rigidBody->setIsMotionEnabled(false);
|
2009-02-04 14:43:33 +00:00
|
|
|
|
|
|
|
// Compute the unit normal vector of the plane by a cross product
|
|
|
|
normalVector = d1.crossProduct(d2).getUnit();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Destructor of the class Plane
|
|
|
|
Plane::~Plane() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Method used to draw the plane
|
2009-02-13 11:26:51 +00:00
|
|
|
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(x, y, z);
|
2009-02-04 14:43:33 +00:00
|
|
|
|
|
|
|
float halfWidth = width / 2.0;
|
|
|
|
float halfHeight = height / 2.0;
|
|
|
|
|
|
|
|
// Draw the plane
|
|
|
|
glBegin(GL_POLYGON);
|
|
|
|
glColor3f(1.0, 1.0, 1.0);
|
2009-02-13 11:26:51 +00:00
|
|
|
glVertex3f(x + d1.getX() * halfWidth + d2.getX() * halfHeight , y + d1.getY() * halfWidth + d2.getY() * halfHeight
|
|
|
|
, z + d1.getZ() * halfWidth + d2.getZ() * halfHeight);
|
2009-02-04 14:43:33 +00:00
|
|
|
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
2009-02-13 11:26:51 +00:00
|
|
|
glVertex3f(x + d1.getX() * halfWidth - d2.getX() * halfHeight , y + d1.getY() * halfWidth - d2.getY() * halfHeight
|
|
|
|
, z + d1.getZ() * halfWidth - d2.getZ() * halfHeight);
|
2009-02-04 14:43:33 +00:00
|
|
|
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
2009-02-13 11:26:51 +00:00
|
|
|
glVertex3f(x - d1.getX() * halfWidth - d2.getX() * halfHeight , y - d1.getY() * halfWidth - d2.getY() * halfHeight
|
|
|
|
, z - d1.getZ() * halfWidth - d2.getZ() * halfHeight);
|
2009-02-04 14:43:33 +00:00
|
|
|
glNormal3f(normalVector.getX(), normalVector.getY(), normalVector.getZ());
|
2009-02-13 11:26:51 +00:00
|
|
|
glVertex3f(x - d1.getX() * halfWidth + d2.getX() * halfHeight , y - d1.getY() * halfWidth + d2.getY() * halfHeight
|
|
|
|
, z - d1.getZ() * halfWidth + d2.getZ() * halfHeight);
|
2009-02-04 14:43:33 +00:00
|
|
|
glEnd();
|
2009-08-04 12:21:15 +00:00
|
|
|
}
|