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
|
/*
Bullet Continuous Collision Detection and Physics Library
Copyright (c) 2003-2007 Erwin Coumans http://continuousphysics.com/Bullet/
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
#include "btBulletDynamicsCommon.h"
#include <stdio.h>
/// This is a Hello World program for running a basic Bullet physics simulation
btDefaultCollisionConfiguration* zz_collisionConfiguration;
btCollisionDispatcher* zz_dispatcher;
btBroadphaseInterface* zz_overlappingPairCache;
btSequentialImpulseConstraintSolver* zz_solver;
btDiscreteDynamicsWorld* zz_dynamicsWorld;
void zz_prepare()
{
int i;
///collision configuration contains default setup for memory, collision setup. Advanced users can create their own configuration.
zz_collisionConfiguration = new btDefaultCollisionConfiguration();
///use the default collision zz_dispatcher. For parallel processing you can use a diffent zz_dispatcher (see Extras/BulletMultiThreaded)
zz_dispatcher = new btCollisionDispatcher(zz_collisionConfiguration);
///btDbvtBroadphase is a good general purpose broadphase. You can also try out btAxis3Sweep.
zz_overlappingPairCache = new btDbvtBroadphase();
///the default constraint zz_solver. For parallel processing you can use a different zz_solver (see Extras/BulletMultiThreaded)
zz_solver = new btSequentialImpulseConstraintSolver;
zz_dynamicsWorld = new btDiscreteDynamicsWorld(zz_dispatcher,zz_overlappingPairCache,zz_solver,zz_collisionConfiguration);
zz_dynamicsWorld->setGravity(btVector3(0,-10,0));
///create a few basic rigid bodies
btCollisionShape* groundShape = new btStaticPlaneShape(btVector3(btScalar(0.),btScalar(1.),btScalar(0.)), 0);
btTransform groundTransform;
groundTransform.setIdentity();
groundTransform.setOrigin(btVector3(0,-6,0));
{
btScalar mass(0.);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
groundShape->calculateLocalInertia(mass,localInertia);
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(groundTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,groundShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
//add the body to the dynamics world
zz_dynamicsWorld->addRigidBody(body);
}
for (int i = 0; i < 5; i++)
{
//create a dynamic rigidbody
btCollisionShape* colShape = new btBoxShape(btVector3(1,1,1));
//btCollisionShape* colShape = new btSphereShape(btScalar(1.));
/// Create Dynamic Objects
btTransform startTransform;
startTransform.setIdentity();
btScalar mass(1.f);
//rigidbody is dynamic if and only if mass is non zero, otherwise static
bool isDynamic = (mass != 0.f);
btVector3 localInertia(0,0,0);
if (isDynamic)
colShape->calculateLocalInertia(mass,localInertia);
startTransform.setOrigin(btVector3(0,4+i*2.1,0));
//using motionstate is recommended, it provides interpolation capabilities, and only synchronizes 'active' objects
btDefaultMotionState* myMotionState = new btDefaultMotionState(startTransform);
btRigidBody::btRigidBodyConstructionInfo rbInfo(mass,myMotionState,colShape,localInertia);
btRigidBody* body = new btRigidBody(rbInfo);
zz_dynamicsWorld->addRigidBody(body);
}
}
/// Do some simulation
void zz_simulate(float diff) {
zz_dynamicsWorld->stepSimulation(diff);
}
void zz_read(int j, btVector3& location, btQuaternion& rotation)
{
{
btCollisionObject* obj = zz_dynamicsWorld->getCollisionObjectArray()[j];
btRigidBody* body = btRigidBody::upcast(obj);
if (body && body->getMotionState())
{
btTransform trans;
body->getMotionState()->getWorldTransform(trans);
location = trans.getOrigin();
rotation = trans.getRotation();
//printf("world pos = %.5f,%.5f,%.5f\n",float(location.getX()),float(location.getY()),float(location.getZ()));
}
}
}
int main(int argc, char** argv)
{
zz_prepare();
zz_simulate(1/60.);//new btVector3);
btVector3 location;
btQuaternion rotation;
zz_read(1, location, rotation);
printf("world pos = %.5f,%.5f,%.5f rot=%.5f,%.5f,%.5f,%.5f\n",float(location.getX()),float(location.getY()),float(location.getZ()),
float(rotation.getX()), float(rotation.getY()), float(rotation.getZ()), float(rotation.getW())
);
}
|