added 3dengfx into the repo, probably not the correct version for this
[summerhack] / src / 3dengfx / src / sim / rigid.cpp
1 /*
2 This file is part of the simulation module of 3dengfx.
3
4 Copyright (c) 2005 John Tsiombikas <nuclear@siggraph.org>
5
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.
10
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.
15
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
19 */
20
21 #include "rigid.hpp"
22 #include <ode/ode.h>
23
24 RigidBody::RigidBody(dWorldID world) {
25         body = dBodyCreate(world);
26 }
27
28 RigidBody::~RigidBody() {
29         dBodyDestroy(body);
30 }
31
32 void RigidBody::enable() {
33         dBodyEnable(body);
34 }
35
36 void RigidBody::disable() {
37         dBodyDisable(body);
38 }
39
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);
43 }
44
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];\
49                 }\
50         }
51
52
53 void RigidBody::set_rotation(const Quaternion &rot, unsigned long time) {
54         Matrix3x3 mat = rot.get_rotation_matrix();
55         dMatrix3 ode_mat;
56         MAT3_TO_ODE(mat, ode_mat);
57         dBodySetRotation(body, ode_mat);
58         Object::set_rotation(rot, time);
59 }
60
61 void RigidBody::set_rotation(const Vector3 &euler, unsigned long time) {
62         Matrix3x3 mat;
63         dMatrix3 ode_mat;
64         mat.set_rotation(euler);
65         MAT3_TO_ODE(mat, ode_mat);
66         dBodySetRotation(body, ode_mat);
67         Object::set_rotation(euler, time);
68 }
69
70 void RigidBody::translate(const Vector3 &trans, unsigned long time) {
71 }
72
73 void RigidBody::rotate(const Quaternion &rot, unsigned long time) {
74 }
75
76 void RigidBody::rotate(const Vector3 &euler, unsigned long time) {
77 }
78
79 void RigidBody::rotate(const Matrix3x3 &rmat, unsigned long time) {
80 }
81
82 RigidSim::RigidSim() {
83         world = dWorldCreate();
84
85         set_gravity(Vector3(0, -9.81, 0));
86 }
87
88 RigidSim::~RigidSim() {
89         dWorldDestroy(world);
90 }
91
92 void RigidSim::run(unsigned long msec) {
93         dWorldStep(world, timeslice);
94 }
95
96 void RigidSim::set_gravity(const Vector3 &gvec) {
97         dWorldSetGravity(world, gvec.x, gvec.y, gvec.z);
98 }
99
100 void RigidSim::add_object(Object *obj) {
101         if(obj) {
102                 obj_list.push_back(obj);
103                 // TODO: cont. here...
104         }
105 }