| 1 |
|
| 2 |
// psim.h [psim] |
| 3 |
|
| 4 |
// Copyright 2004-2006 Jasmine Langridge, [email protected] |
| 5 |
// License: GPL version 2 (see included gpl.txt) |
| 6 |
|
| 7 |
// |
| 8 |
// This file contains basic classes the physic engine uses |
| 9 |
// |
| 10 |
|
| 11 |
#include <pengine.h> |
| 12 |
|
| 13 |
|
| 14 |
class PSim; |
| 15 |
class PRigidBody; |
| 16 |
|
| 17 |
class PVehicleType; |
| 18 |
class PVehicleTypePart; |
| 19 |
class PVehicle; |
| 20 |
|
| 21 |
/// |
| 22 |
/// @brief Store a Reference oriented point (stores coordinates and orientation) |
| 23 |
/// @details It is the base class for more advanced classes such as PRigidBody |
| 24 |
/// |
| 25 |
class PReferenceFrame { |
| 26 |
public: |
| 27 |
// position |
| 28 |
vec3f pos; |
| 29 |
// orientation |
| 30 |
quatf ori; |
| 31 |
// orientation matrix, and its inverse |
| 32 |
// not directly modify them, but modify ori then run UpdateMatrices() |
| 33 |
// @todo maybe make them private/protected? |
| 34 |
mat44f ori_mat, ori_mat_inv; |
| 35 |
|
| 36 |
public: |
| 37 |
PReferenceFrame() : pos(vec3f::zero()), ori(quatf::identity()) { |
| 38 |
updateMatrices(); |
| 39 |
} |
| 40 |
|
| 41 |
// Update the orientation matrices from quaternion orientation (ori) |
| 42 |
void updateMatrices() { |
| 43 |
ori.normalize(); |
| 44 |
ori_mat = ori.getMatrix(); |
| 45 |
ori_mat_inv = ori_mat.transpose(); |
| 46 |
} |
| 47 |
|
| 48 |
// Position |
| 49 |
void setPosition(const vec3f &_pos) { pos = _pos; } |
| 50 |
vec3f getPosition() { return pos; } |
| 51 |
|
| 52 |
// Orientation |
| 53 |
void setOrientation(const quatf &_ori) { ori = _ori; } |
| 54 |
quatf getOrientation() { return ori; } |
| 55 |
mat44f getOrientationMatrix() { return ori_mat; } |
| 56 |
mat44f getInverseOrientationMatrix() { return ori_mat_inv; } |
| 57 |
|
| 58 |
// Change coordinate reference (From local to world system and viceversa) |
| 59 |
vec3f getLocToWorldVector(const vec3f &pt) { |
| 60 |
return ori_mat.transform1(pt); |
| 61 |
} |
| 62 |
vec3f getWorldToLocVector(const vec3f &pt) { |
| 63 |
return ori_mat.transform2(pt); |
| 64 |
} |
| 65 |
vec3f getLocToWorldPoint(const vec3f &pt) { |
| 66 |
return pos + ori_mat.transform1(pt); |
| 67 |
} |
| 68 |
vec3f getWorldToLocPoint(const vec3f &pt) { |
| 69 |
return ori_mat.transform2(pt - pos); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
/// |
| 74 |
/// @brief A Rigid body with mass, position, velocity, and angular mass, position, and velocity |
| 75 |
/// @todo: intelligent friction calculation |
| 76 |
/// |
| 77 |
class PRigidBody : public PReferenceFrame { |
| 78 |
private: |
| 79 |
|
| 80 |
// Class that stores simulation informations (such as gravity force intensity) |
| 81 |
PSim ∼ |
| 82 |
|
| 83 |
// mass and 1/mass |
| 84 |
float mass, mass_inv; |
| 85 |
// inertial tensor (angular mass) and 1/angmass (usually just approximately computed) |
| 86 |
vec3f angmass, angmass_inv; |
| 87 |
|
| 88 |
// linear and angular velocity |
| 89 |
vec3f linvel; |
| 90 |
vec3f angvel; |
| 91 |
|
| 92 |
// the force and the torque accumuled during a slice of time |
| 93 |
vec3f accum_force; |
| 94 |
vec3f accum_torque; |
| 95 |
|
| 96 |
public: |
| 97 |
PRigidBody(PSim &sim_parent); |
| 98 |
~PRigidBody(); |
| 99 |
|
| 100 |
public: |
| 101 |
void setMassCuboid(float _mass, const vec3f &dim); |
| 102 |
|
| 103 |
void setLinearVel(const vec3f &vel) { linvel = vel; } |
| 104 |
const vec3f &getLinearVel() { return linvel; } |
| 105 |
|
| 106 |
void setAngularVel(const vec3f &vel) { angvel = vel; } |
| 107 |
const vec3f &getAngularVel() { return angvel; } |
| 108 |
|
| 109 |
void addForce(const vec3f &frc); |
| 110 |
void addLocForce(const vec3f &frc); |
| 111 |
void addForceAtPoint(const vec3f &frc, const vec3f &pt); |
| 112 |
void addLocForceAtPoint(const vec3f &frc, const vec3f &pt); |
| 113 |
void addForceAtLocPoint(const vec3f &frc, const vec3f &pt); |
| 114 |
void addLocForceAtLocPoint(const vec3f &frc, const vec3f &pt); |
| 115 |
|
| 116 |
void addTorque(const vec3f &trq); |
| 117 |
void addLocTorque(const vec3f &trq); |
| 118 |
|
| 119 |
vec3f getLinearVelAtPoint(const vec3f &pt); |
| 120 |
vec3f getLinearVelAtLocPoint(const vec3f &pt); |
| 121 |
|
| 122 |
// Step the simulation delta seconds |
| 123 |
void tick(float delta); |
| 124 |
|
| 125 |
friend class PSim; |
| 126 |
}; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
#include "vehicle.h" |
| 132 |
|
| 133 |
/// |
| 134 |
/// @brief class that stores information about a physic simulation instance |
| 135 |
/// |
| 136 |
class PSim { |
| 137 |
private: |
| 138 |
|
| 139 |
// the terrain class |
| 140 |
PTerrain *terrain; |
| 141 |
|
| 142 |
// the various types of vehicles |
| 143 |
PResourceList<PVehicleType> vtypelist; |
| 144 |
|
| 145 |
// the various bodyes inside the simulation |
| 146 |
std::vector<PRigidBody *> body; |
| 147 |
|
| 148 |
// the various vehicles inside the simulation |
| 149 |
std::vector<PVehicle *> vehicle; |
| 150 |
|
| 151 |
// the gravity vector |
| 152 |
vec3f gravity; |
| 153 |
|
| 154 |
public: |
| 155 |
PSim(); |
| 156 |
~PSim(); |
| 157 |
|
| 158 |
inline void setTerrain(PTerrain *new_terrain) { terrain = new_terrain; } |
| 159 |
inline PTerrain *getTerrain() { return terrain; } |
| 160 |
|
| 161 |
inline void setGravity(const vec3f &new_gravity) { gravity = new_gravity; } |
| 162 |
|
| 163 |
PVehicleType *loadVehicleType(const std::string &filename, PSSModel &ssModel); |
| 164 |
|
| 165 |
PRigidBody *createRigidBody(); |
| 166 |
|
| 167 |
PVehicle *createVehicle(XMLElement *element, const std::string &filepath, PSSModel &ssModel); |
| 168 |
PVehicle *createVehicle(const std::string &type, const vec3f &pos, const quatf &ori, const std::string &filepath, PSSModel &ssModel); |
| 169 |
PVehicle *createVehicle(PVehicleType *type, const vec3f &pos, const quatf &ori /* , PSSModel &ssModel */); |
| 170 |
|
| 171 |
// Remove all bodies and vehicles |
| 172 |
void clear(); |
| 173 |
|
| 174 |
// Step the simulation delta seconds |
| 175 |
void tick(float delta); |
| 176 |
|
| 177 |
friend class PRigidBody; |
| 178 |
friend class PVehicle; |
| 179 |
}; |