Fixed some transforms, move to 3d and quaternion math in progress...
[ne.git] / src / phys / object.cpp
blobdb17cb8920f6dcbfc2b50de6c9b8211a660a860f
1 /************************************************************************
2 This file is part of NE.
4 NE is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 NE is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with NE. If not, see <http://www.gnu.org/licenses/>.
16 ************************************************************************/
18 #include "object.h"
20 #include "world.h"
21 #include <GL/gl.h>
22 #include <ode/ode.h>
23 #include <ode/odecpp.h>
24 #include <ode/odecpp_collision.h>
25 #include <string>
26 #include <sstream>
27 #include <vector>
29 Object::Object()
30 : m_gravity_mag(0), m_geom(0), m_static(false), m_colfunc(0)
32 m_body = new dBody(World::Instance()->getWorld()->id());
35 Object::~Object()
37 delete m_body;
40 int Object::collide(Object* o)
42 if (!m_colfunc)
44 return 1;
46 else
48 return m_colfunc(this,o);
52 void Object::apply_matrix()
54 const dReal *pos = m_body->getPosition();
55 const dReal *rot = m_body->getRotation();
57 static GLfloat matrix[16];
58 matrix[0]=rot[0];
59 matrix[1]=rot[4];
60 matrix[2]=rot[8];
61 matrix[3]=0;
62 matrix[4]=rot[1];
63 matrix[5]=rot[5];
64 matrix[6]=rot[9];
65 matrix[7]=0;
66 matrix[8]=rot[2];
67 matrix[9]=rot[6];
68 matrix[10]=rot[10];
69 matrix[11]=0;
70 matrix[12]=pos[0];
71 matrix[13]=pos[1];
72 matrix[14]=pos[2];
73 matrix[15]=1;
75 glMultMatrixf (matrix);
78 void Object::init_geom()
80 if (m_geom)
82 m_geom->setData(this);
83 m_geom->setBody(m_body->id());
87 void Object::setStatic(bool is_static)
89 m_static = is_static;
90 if (m_geom)
92 if (m_static)
93 m_geom->setBody(0);
94 else
95 m_geom->setBody(m_body->id());
99 bool Object::applyGravity(const Vec3f &grav)
101 m_body->addForce(grav.x(), grav.y(), grav.z());
103 float mag = grav.length();
105 if (m_gravity_mag < mag)
107 m_gravity_mag = mag;
108 m_gravity = grav;
110 return true;
113 return false;
116 std::string &Object::serialize()
118 std::stringstream stream;
120 const dReal *p = getPosition();
122 stream << "pos: ";
123 stream << p[0] << ' ' << p[1] << ' ' << p[2] << ' ';
125 p = getQuaternion();
127 stream << "quat: ";
128 stream << p[0] << ' ' << p[1] << ' ' << p[2] << ' ' << p[3] << ' ';
130 static std::string out = stream.str();
131 return out;
134 void Object::unserialize(std::string &s)
136 std::stringstream stream(s);
137 std::string str;
138 while (stream >> str)
140 if (str == "pos:")
142 dReal x, y, z;
143 stream >> x >> y >> z;
144 setPosition(x,y,z);
146 else if (str == "quat:")
148 dReal q[4];
149 stream >> q[0] >> q[1] >> q[2] >> q[3];
150 setQuaternion(q);
155 void Object::setPosition(dReal x, dReal y, dReal z)
157 if (m_static)
158 m_geom->setPosition(x,y,z);
159 else
160 m_body->setPosition(x,y,z);
162 const dReal *Object::getPosition()
164 if (m_static)
165 return m_geom->getPosition();
166 else
167 return m_body->getPosition();
170 void Object::setRotation(const dMatrix3 r)
172 if (m_static)
173 m_geom->setRotation(r);
174 else
175 m_body->setRotation(r);
177 const dReal *Object::getRotation()
179 if (m_static)
180 return m_geom->getRotation();
181 else
182 return m_body->getRotation();
185 void Object::setQuaternion(const dQuaternion q)
187 if (m_static)
188 m_geom->setQuaternion(q);
189 else
190 m_body->setQuaternion(q);
192 const dReal *Object::getQuaternion()
194 if (m_static)
196 static dReal q[4];
197 m_geom->getQuaternion(q);
198 return q;
200 else
201 return m_body->getQuaternion();