2 This file is part of the simulation module of 3dengfx.
4 Copyright (c) 2005 John Tsiombikas <nuclear@siggraph.org>
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 RigidBody::RigidBody(dWorldID world) {
25 body = dBodyCreate(world);
28 RigidBody::~RigidBody() {
32 void RigidBody::enable() {
36 void RigidBody::disable() {
40 void RigidBody::set_position(const Vector3 &pos, unsigned long time) {
41 dBodySetPosition(body, pos.x, pos.y, pos.z);
42 Object::set_position(pos, time);
45 #define MAT3_TO_ODE(m, o)\
46 for(int i=0; i<3; i++) {\
47 for(int j=0; j<3; j++) {\
48 dACCESS33(o, i, j) = m[i][j];\
53 void RigidBody::set_rotation(const Quaternion &rot, unsigned long time) {
54 Matrix3x3 mat = rot.get_rotation_matrix();
56 MAT3_TO_ODE(mat, ode_mat);
57 dBodySetRotation(body, ode_mat);
58 Object::set_rotation(rot, time);
61 void RigidBody::set_rotation(const Vector3 &euler, unsigned long time) {
64 mat.set_rotation(euler);
65 MAT3_TO_ODE(mat, ode_mat);
66 dBodySetRotation(body, ode_mat);
67 Object::set_rotation(euler, time);
70 void RigidBody::translate(const Vector3 &trans, unsigned long time) {
73 void RigidBody::rotate(const Quaternion &rot, unsigned long time) {
76 void RigidBody::rotate(const Vector3 &euler, unsigned long time) {
79 void RigidBody::rotate(const Matrix3x3 &rmat, unsigned long time) {
82 RigidSim::RigidSim() {
83 world = dWorldCreate();
85 set_gravity(Vector3(0, -9.81, 0));
88 RigidSim::~RigidSim() {
92 void RigidSim::run(unsigned long msec) {
93 dWorldStep(world, timeslice);
96 void RigidSim::set_gravity(const Vector3 &gvec) {
97 dWorldSetGravity(world, gvec.x, gvec.y, gvec.z);
100 void RigidSim::add_object(Object *obj) {
102 obj_list.push_back(obj);
103 // TODO: cont. here...