/
pobject.cpp
178 lines (150 loc) · 3.87 KB
/
pobject.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include <ode/ode.h>
#include <iostream>
#include "pobject.h"
#include "physics.h"
#include "quat.h"
#include "geominfo.h"
#include "world.h"
PObject::PObject(GeomInfo *info) : collType(REAL)
{
space = Physics::getInstance().getOdeSpace();
geom = info->createGeom(space);
dGeomSetData(geom, this);
}
PObject::~PObject()
{
dGeomDestroy(geom);
}
bool PObject::isPlaceable()
{
int c = dGeomGetClass(geom);
if (c == dPlaneClass)
return false;
return true;
}
void PObject::getQuat(Quatf_t quat)
{
dReal dquat[4];
if (isPlaceable())
{
dGeomGetQuaternion(geom, dquat);
DQuatToQuatf(dquat, quat);
}
else
{
quat[0] = 0;
quat[1] = 0;
quat[2] = 0;
quat[3] = 1;
}
}
Vec3f PObject::getPos()
{
if (isPlaceable())
{
const dReal *pos = dGeomGetPosition(geom);
return Vec3f(pos[0], pos[1], pos[2]);
}
else
return Vec3f(0,0,0);
}
PMoveable::PMoveable(Kinematic &kinematic, float mass, GeomInfo *info)
: PObject(info), kinematic(kinematic)
{
dQuaternion q;
body = dBodyCreate(Physics::getInstance().getOdeWorld());
info->createMass(&this->mass, mass);
dBodySetMass(body, &this->mass);
dGeomSetBody(geom, body);
kinematicToOde();
dQFromAxisAndAngle(q, 0, 1, 0, kinematic.orientation);
dBodySetQuaternion(body, q);
}
const Kinematic &PMoveable::getKinematic()
{
return kinematic;
}
void PMoveable::kinematicToOde()
{
Kinematic &k = kinematic;
dBodySetPosition(body, k.pos[0], k.pos[1], k.pos[2]);
dBodySetLinearVel(body, k.vel[0], k.vel[1], k.vel[2]);
}
Kinematic PMoveable::odeToKinematic()
{
Kinematic k;
dQuaternion q_result, q_result1, q_base;
float norm;
const dReal *b_info;
const dReal *q_current = dBodyGetQuaternion(body);
q_base[0] = 0; q_base[1] = 0; q_base[2] = 0; q_base[3] = 1;
dQMultiply0(q_result1, q_current, q_base);
dQMultiply2(q_result, q_result1, q_current);
k.orientation_v = Vec3f(q_result[1], q_result[2], q_result[3]);
norm = sqrt(q_result[1] * q_result[1] + q_result[3] * q_result[3]);
if (norm == 0)
k.orientation = 0;
else
k.orientation = atan2(q_result[1] / norm, q_result[3] / norm);
b_info = dBodyGetPosition(body);
k.pos = Vec3f(b_info[0], b_info[1], b_info[2]);
b_info = dBodyGetLinearVel(body);
k.vel = Vec3f(b_info[0], b_info[1], b_info[2]);
return k;
}
void PMoveable::prePhysics()
{
return;
}
void PMoveable::postPhysics()
{
kinematic = odeToKinematic();
}
PSteerable::PSteerable(SteerInfo &steerinfo, Kinematic &kinematic, float mass,
GeomInfo *info)
: PMoveable(kinematic, mass, info), steerInfo(steerinfo)
{
}
const SteerInfo &PSteerable::getSteering()
{
return steerInfo;
}
void PSteerable::setSteering(SteerInfo steerInfo)
{
this->steerInfo = steerInfo;
}
void PSteerable::steeringToOde()
{
if (steerInfo.acc.length() != 0 || steerInfo.rot)
dBodyEnable(body);
Vec3f f = steerInfo.acc * mass.mass;
const dReal *angVel = dBodyGetAngularVel(body);
dBodySetAngularVel(body, angVel[0], angVel[1] + steerInfo.rot,
angVel[2]);
dBodyAddForce(body, f[0], f[1], f[2]);
}
void PSteerable::resetOdeAngularVelocity()
{
const dReal *angVel = dBodyGetAngularVel(body);
dBodySetAngularVel(body, angVel[0], angVel[1] - steerInfo.rot, angVel[2]);
}
void PSteerable::prePhysics()
{
int n = World::size;
if (kinematic.pos.x > n)
kinematic.pos.x = -n;
else if (kinematic.pos.x < -n)
kinematic.pos.x = n;
if (kinematic.pos.z > n)
kinematic.pos.z = -n;
else if (kinematic.pos.z < -n)
kinematic.pos.z = n;
kinematicToOde();
steeringToOde();
PMoveable::prePhysics();
}
void PSteerable::postPhysics()
{
resetOdeAngularVelocity();
PMoveable::postPhysics();
}