-
Notifications
You must be signed in to change notification settings - Fork 2
/
btosg.cpp
245 lines (214 loc) · 7.17 KB
/
btosg.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/*
btosg.cpp
Miguel Leitao, 2016
*/
#include "btosg.h"
#include <osg/Material>
#include <osg/Texture2D>
#define _DEBUG_ (0)
void btosgObject::print() {
/// Print out objects position.
std::cout << "Object: " << name << "\n";
btosgVec3 pos = getPosition();
std::cout << " Position: " << pos.x() << ", " << pos.y() << ", " << pos.z() << "\n";
if ( isnan(pos.x() )) exit(2);
}
btVector3 quat2Euler(const btQuaternion& q)
{
// Implementation tested.
// Using HPR order rotation.
// Angles are interpreted when Y points forward, Z points up.
// https://www.euclideanspace.com/maths/geometry/rotations/conversions/quaternionToEuler/Quaternions.pdf
// roll (y-axis rotation)
double sinr = +2.0 * (q.w() * q.y() + q.x() * q.z());
double cosr = +1.0 - 2.0 * (q.y() * q.y() + q.x() * q.x());
double roll = atan2(sinr, cosr);
// pitch (x-axis rotation)
double sinp = +2.0 * (q.w() * q.x() - q.z() * q.y());
double pitch;
if (fabs(sinp) >= 1)
pitch = copysign(M_PI / 2., sinp); // use 90 degrees if out of range
else
pitch = asin(sinp);
// yaw (z-axis rotation)
double siny = +2.0 * (q.w() * q.z() + q.y() * q.x());
double cosy = +1.0 - 2.0 * (q.x() * q.x() + q.z() * q.z());
double yaw = atan2(siny, cosy);
return btVector3(yaw,pitch,roll);
}
btosgVec3 btosgQuat::toEuler()
{
return quat2Euler(*this);
}
int btosgWorld::deleteAllObjects() {
/// Deletes all abjects from the btosgWorld
int count = 0;
while ( !objects.empty() )
{
btosgObject *obj = objects.front();
removeObject(obj);
delete(obj);
//std::cout << ' ' << mylist.front();
count++;
}
return count;
}
btosgWorld::~btosgWorld() {
/*
for ( auto it = objects.begin() ; it != objects.end(); ++it ) {
btosgObject *obj = *it;
removeObject(obj);
//delete obj;
}
*/
deleteAllObjects();
/*
std::forward_list<btosgObject*>::iterator it = objects.begin();
while (it != objects.end())
{
std::forward_list<btosgObject*>::iterator next = it;
next++;
btosgObject *obj = *it;
removeObject(obj); // alternatively, i = items.erase(i);
delete(obj);
it = next;
}
*/
delete dynamic; // ????
delete solver;
delete dispatcher;
delete collisionConfiguration;
delete broadphase;
/*
while (!objects.empty())
{
objects.pop_front();
}
*/
//scene->unref();
}
void btosgWorld::listObjects() {
/// Outputs a list of all objects in the btosgWorld
int n = 0;
printf("## Object List\n");
for ( auto it = objects.begin() ; it != objects.end(); ++it ) {
btosgObject *obj = *it;
obj->print();
n++;
}
printf("%d objects listed.\n",n);
}
void btosgWorld::addObject(btosgObject *obj) {
/// Registers the object obj into a simulation world.
objects.push_front(obj);
if ( obj->body ) dynamic->addRigidBody(obj->body);
else if ( _DEBUG_ ) fprintf(stderr, "Adding object without rigid body\n");
//printf("adding object to World\n");
if ( obj->model ) scene->addChild(obj->model);
else if ( _DEBUG_ ) fprintf(stderr, "Adding object without visual model\n");
}
void btosgWorld::removeObject(btosgObject *obj) {
/// Unregisters the object obj from the simulation world.
if ( obj->body ) dynamic->removeRigidBody(obj->body);
else if ( _DEBUG_ ) fprintf(stderr, "Removing object without rigid body\n");
//printf("removing object from World\n");
if ( obj->model ) scene->removeChild(obj->model);
else if ( _DEBUG_ ) fprintf(stderr, "Removing object without visual model\n");
// Remove from the list.
// This also calls de the destructor for obj.
objects.remove(obj);
}
void btosgWorld::stepSimulation(btScalar timeStep, int maxSubSteps) {
/// Performs a simulation step.
if ( steps==0L ) {
for ( auto it = objects.begin(); it != objects.end(); ++it ) {
btosgObject *obj = *it;
obj->setInitState();
}
}
// listObjects();
dynamic->stepSimulation(timeStep,maxSubSteps);
for ( auto it = objects.begin(); it != objects.end(); ++it ) {
btosgObject *obj = *it;
obj->update();
}
steps += 1;
}
void btosgWorld::reset() {
/// Reset all registered objects.
for ( auto it = objects.begin(); it != objects.end(); ++it ) {
btosgObject *obj = *it;
obj->reset();
}
}
void btosgObject::loadObjectModel(char const *fname) {
/// Loads an object model from a Wavefront OBJ file.
/// Loadded model is used to define both the collision and graphical shapes.
if ( ! fname || ! *fname ) {
fprintf(stderr, "Invalid object model filename\n");
return;
}
osg::Node* loadedModel = osgDB::readNodeFile(fname);
if ( ! loadedModel ) {
fprintf(stderr, "Error reading Object model from file '%s'\n", fname);
}
if ( name ) {
fprintf(stderr, "Warning: Object '%s' overloaded with object '%s'\n", name, fname);
}
setName(fname);
if ( ! model ) model = new osg::PositionAttitudeTransform;
osg::PositionAttitudeTransform* obj_rot = new osg::PositionAttitudeTransform;
obj_rot->setAttitude(osg::Quat(-osg::PI/2.,osg::Vec3(1.,0.,0.)));
obj_rot->addChild(loadedModel);
model->addChild(obj_rot);
}
void btosgObject::setTexture(char const *fname)
{
/// Sets the object texture from a loaded image file.
osg::StateSet* stateset = new osg::StateSet();
osg::ref_ptr<osg::Image> image = osgDB::readRefImageFile( fname );
if (image) {
osg::Texture2D* texture = new osg::Texture2D;
texture->setImage(image);
texture->setFilter(osg::Texture::MIN_FILTER, osg::Texture::LINEAR);
stateset->setTextureAttributeAndModes(0, texture, osg::StateAttribute::ON);
}
stateset->setMode(GL_LIGHTING, osg::StateAttribute::ON);
model->setStateSet( stateset );
}
void btosgObject::reset() {
/// Reposition object to its inital state.
if ( body ) {
body->setWorldTransform(init_state);
body->getMotionState()->setWorldTransform(init_state);
body->clearForces();
if ( mass>0. ) {
body->setLinearVelocity(btVector3(0., 0., 0.));
body->setAngularVelocity(btVector3(0., 0., 0.));
body->activate(); // Required if the object was asleep
}
}
else { // Not required for dynamic objects.
if ( model ) {
model->setAttitude(btosgQuat(init_state.getRotation()));
model->setPosition(btosgVec3(init_state.getOrigin()));
}
}
}
void btosgObject::createRigidBody() {
/// Creates a new rigid body as a btRigidBody object.
if ( ! shape ) {
fprintf(stderr,"Cannot create RigidBody without shape\n");
return;
}
btDefaultMotionState* mState = new
btDefaultMotionState(btTransform(btQuaternion(0, 0, 0, 1), btVector3(0., 0., 0.)));
btVector3 inertia(0, 0, 0);
shape->calculateLocalInertia(mass,inertia);
btRigidBody::btRigidBodyConstructionInfo cInfo(mass, mState, shape, inertia);
//printf("mass: %f\n",mass);
cInfo.m_restitution = 0.9f;
cInfo.m_friction = 10.f;
body = new btRigidBody(cInfo);
if ( !body ) fprintf(stderr,"Error creating btBody\n");
}