Bullet Collision Detection & Physics Library
TrbStateVec.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  All rights reserved.
4 
5 This software is provided 'as-is', without any express or implied warranty.
6 In no event will the authors be held liable for any damages arising from the use of this software.
7 Permission is granted to anyone to use this software for any purpose,
8 including commercial applications, and to alter it and redistribute it freely,
9 subject to the following restrictions:
10 
11 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.
12 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
13 3. This notice may not be removed or altered from any source distribution.
14 
15 */
16 
17 #ifndef BT_TRBSTATEVEC_H__
18 #define BT_TRBSTATEVEC_H__
19 
20 #include <stdlib.h>
21 #ifdef PFX_USE_FREE_VECTORMATH
22 #include "vecmath/vmInclude.h"
23 #else
24 #include "vectormath/vmInclude.h"
25 #endif //PFX_USE_FREE_VECTORMATH
26 
27 
28 #include "PlatformDefinitions.h"
29 
30 
31 static inline vmVector3 read_Vector3(const float* p)
32 {
33  vmVector3 v;
34  loadXYZ(v, p);
35  return v;
36 }
37 
38 static inline vmQuat read_Quat(const float* p)
39 {
40  vmQuat vq;
41  loadXYZW(vq, p);
42  return vq;
43 }
44 
45 static inline void store_Vector3(const vmVector3 &src, float* p)
46 {
47  vmVector3 v = src;
48  storeXYZ(v, p);
49 }
50 
51 static inline void store_Quat(const vmQuat &src, float* p)
52 {
53  vmQuat vq = src;
54  storeXYZW(vq, p);
55 }
56 
57 // Motion Type
58 enum {
65 };
66 
67 #define PFX_MOTION_MASK_DYNAMIC 0x0a // Active,OneWay
68 #define PFX_MOTION_MASK_STATIC 0x95 // Fixed,Keyframe,Trigger,Sleeping
69 #define PFX_MOTION_MASK_SLEEP 0x0e // Can sleep
70 #define PFX_MOTION_MASK_TYPE 0x7f
71 
72 //
73 // Rigid Body state
74 //
75 
76 #ifdef __CELLOS_LV2__
78 #else
80 #endif
81 
82 {
83 public:
85  {
86  setMotionType(PfxMotionTypeActive);
87  contactFilterSelf=contactFilterTarget=0xffffffff;
88  deleted = 0;
89  mSleeping = 0;
90  useSleep = 1;
91  trbBodyIdx=0;
92  mSleepCount=0;
93  useCcd = 0;
94  useContactCallback = 0;
95  useSleepCallback = 0;
96  linearDamping = 1.0f;
97  angularDamping = 0.99f;
98  }
99 
100  TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega );
101 
104  uint8_t deleted : 1;
105  uint8_t mSleeping : 1;
106  uint8_t useSleep : 1;
107  uint8_t useCcd : 1;
108  uint8_t useContactCallback : 1;
109  uint8_t useSleepCallback : 1;
110 
114 
115  float center[3]; // AABB center(World)
116  float half[3]; // AABB half(World)
117 
120 
121  float deltaLinearVelocity[3];
122  float deltaAngularVelocity[3];
123 
124  float fX[3]; // position
125  float fQ[4]; // orientation
126  float fV[3]; // velocity
127  float fOmega[3]; // angular velocity
128 
129  inline void setZero(); // Zeroes out the elements
130  inline void setIdentity(); // Sets the rotation to identity and zeroes out the other elements
131 
132  bool isDeleted() const {return deleted==1;}
133 
134  uint16_t getRigidBodyId() const {return trbBodyIdx;}
135  void setRigidBodyId(uint16_t i) {trbBodyIdx = i;}
136 
137 
138  uint32_t getContactFilterSelf() const {return contactFilterSelf;}
139  void setContactFilterSelf(uint32_t filter) {contactFilterSelf = filter;}
140 
141  uint32_t getContactFilterTarget() const {return contactFilterTarget;}
142  void setContactFilterTarget(uint32_t filter) {contactFilterTarget = filter;}
143 
144  float getLinearDamping() const {return linearDamping;}
145  float getAngularDamping() const {return angularDamping;}
146 
147  void setLinearDamping(float damping) {linearDamping=damping;}
148  void setAngularDamping(float damping) {angularDamping=damping;}
149 
150 
151  uint8_t getMotionType() const {return mMotionType;}
152  void setMotionType(uint8_t t) {mMotionType = t;mSleeping=0;mSleepCount=0;}
153 
154  uint8_t getMotionMask() const {return (1<<mMotionType)|(mSleeping<<7);}
155 
156  bool isAsleep() const {return mSleeping==1;}
157  bool isAwake() const {return mSleeping==0;}
158 
159  void wakeup() {mSleeping=0;mSleepCount=0;}
160  void sleep() {if(useSleep) {mSleeping=1;mSleepCount=0;}}
161 
162  uint8_t getUseSleep() const {return useSleep;}
163  void setUseSleep(uint8_t b) {useSleep=b;}
164 
165  uint8_t getUseCcd() const {return useCcd;}
166  void setUseCcd(uint8_t b) {useCcd=b;}
167 
168  uint8_t getUseContactCallback() const {return useContactCallback;}
169  void setUseContactCallback(uint8_t b) {useContactCallback=b;}
170 
171  uint8_t getUseSleepCallback() const {return useSleepCallback;}
172  void setUseSleepCallback(uint8_t b) {useSleepCallback=b;}
173 
174  void incrementSleepCount() {mSleepCount++;}
175  void resetSleepCount() {mSleepCount=0;}
176  uint16_t getSleepCount() const {return mSleepCount;}
177 
178  vmVector3 getPosition() const {return read_Vector3(fX);}
179  vmQuat getOrientation() const {return read_Quat(fQ);}
181  vmVector3 getAngularVelocity() const {return read_Vector3(fOmega);}
182  vmVector3 getDeltaLinearVelocity() const {return read_Vector3(deltaLinearVelocity);}
183  vmVector3 getDeltaAngularVelocity() const {return read_Vector3(deltaAngularVelocity);}
184 
185  void setPosition(const vmVector3 &pos) {store_Vector3(pos, fX);}
186  void setLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, fV);}
187  void setAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, fOmega);}
188  void setDeltaLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaLinearVelocity);}
189  void setDeltaAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaAngularVelocity);}
190  void setOrientation(const vmQuat &rot) {store_Quat(rot, fQ);}
191 
192  inline void setAuxils(const vmVector3 &centerLocal,const vmVector3 &halfLocal);
193  inline void setAuxilsCcd(const vmVector3 &centerLocal,const vmVector3 &halfLocal,float timeStep);
194  inline void reset();
195 };
196 
197 inline
198 TrbState::TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega)
199 {
200  setMotionType(m);
201  fX[0] = x[0];
202  fX[1] = x[1];
203  fX[2] = x[2];
204  fQ[0] = q[0];
205  fQ[1] = q[1];
206  fQ[2] = q[2];
207  fQ[3] = q[3];
208  fV[0] = v[0];
209  fV[1] = v[1];
210  fV[2] = v[2];
211  fOmega[0] = omega[0];
212  fOmega[1] = omega[1];
213  fOmega[2] = omega[2];
215  trbBodyIdx=0;
216  mSleeping = 0;
217  deleted = 0;
218  useSleep = 1;
219  useCcd = 0;
220  useContactCallback = 0;
221  useSleepCallback = 0;
222  mSleepCount=0;
223  linearDamping = 1.0f;
224  angularDamping = 0.99f;
225 }
226 
227 inline void
229 {
230  fX[0] = 0.0f;
231  fX[1] = 0.0f;
232  fX[2] = 0.0f;
233  fQ[0] = 0.0f;
234  fQ[1] = 0.0f;
235  fQ[2] = 0.0f;
236  fQ[3] = 1.0f;
237  fV[0] = 0.0f;
238  fV[1] = 0.0f;
239  fV[2] = 0.0f;
240  fOmega[0] = 0.0f;
241  fOmega[1] = 0.0f;
242  fOmega[2] = 0.0f;
243 }
244 
245 inline void
247 {
248  fX[0] = 0.0f;
249  fX[1] = 0.0f;
250  fX[2] = 0.0f;
251  fQ[0] = 0.0f;
252  fQ[1] = 0.0f;
253  fQ[2] = 0.0f;
254  fQ[3] = 0.0f;
255  fV[0] = 0.0f;
256  fV[1] = 0.0f;
257  fV[2] = 0.0f;
258  fOmega[0] = 0.0f;
259  fOmega[1] = 0.0f;
260  fOmega[2] = 0.0f;
261 }
262 
263 inline void
264 TrbState::setAuxils(const vmVector3 &centerLocal,const vmVector3 &halfLocal)
265 {
266  vmVector3 centerW = getPosition() + rotate(getOrientation(),centerLocal);
267  vmVector3 halfW = absPerElem(vmMatrix3(getOrientation())) * halfLocal;
268  center[0] = centerW[0];
269  center[1] = centerW[1];
270  center[2] = centerW[2];
271  half[0] = halfW[0];
272  half[1] = halfW[1];
273  half[2] = halfW[2];
274 }
275 
276 inline void
277 TrbState::setAuxilsCcd(const vmVector3 &centerLocal,const vmVector3 &halfLocal,float timeStep)
278 {
279  vmVector3 centerW = getPosition() + rotate(getOrientation(),centerLocal);
280  vmVector3 halfW = absPerElem(vmMatrix3(getOrientation())) * halfLocal;
281 
282  vmVector3 diffvec = getLinearVelocity()*timeStep;
283 
284  vmVector3 newCenter = centerW + diffvec;
285  vmVector3 aabbMin = minPerElem(newCenter - halfW,centerW - halfW);
286  vmVector3 aabbMax = maxPerElem(newCenter + halfW,centerW + halfW);
287 
288  centerW = 0.5f * (aabbMin + aabbMax);
289  halfW =0.5f * (aabbMax - aabbMin);
290 
291  center[0] = centerW[0];
292  center[1] = centerW[1];
293  center[2] = centerW[2];
294 
295  half[0] = halfW[0];
296  half[1] = halfW[1];
297  half[2] = halfW[2];
298 }
299 
300 inline
302 {
303 #if 0
304  mSleepCount = 0;
306  mDeleted = 0;
307  mSleeping = 0;
308  mUseSleep = 1;
309  mUseCcd = 0;
310  mUseContactCallback = 0;
311  mUseSleepCallback = 0;
312  mRigidBodyId = 0;
313  mContactFilterSelf = 0xffffffff;
314  mContactFilterTarget = 0xffffffff;
315  mLinearDamping = 1.0f;
316  mAngularDamping = 0.99f;
317  mPosition = vmVector3(0.0f);
318  mOrientation = vmQuat::identity();
319  mLinearVelocity = vmVector3(0.0f);
320  mAngularVelocity = vmVector3(0.0f);
321 #endif
322 
325  deleted = 0;
326  mSleeping = 0;
327  useSleep = 1;
328  trbBodyIdx=0;
329  mSleepCount=0;
330  useCcd = 0;
331  useContactCallback = 0;
332  useSleepCallback = 0;
333  linearDamping = 1.0f;
334  angularDamping = 0.99f;
335 }
336 
337 #endif //BT_TRBSTATEVEC_H__
338 
339 
float angularDamping
Definition: TrbStateVec.h:119
void setAngularDamping(float damping)
Definition: TrbStateVec.h:148
void setContactFilterTarget(uint32_t filter)
Definition: TrbStateVec.h:142
void setOrientation(const vmQuat &rot)
Definition: TrbStateVec.h:190
uint16_t mSleepCount
Definition: TrbStateVec.h:102
uint8_t mMotionType
Definition: TrbStateVec.h:103
void setAngularVelocity(const vmVector3 &vel)
Definition: TrbStateVec.h:187
uint8_t deleted
Definition: TrbStateVec.h:104
const Vector3 minPerElem(const Vector3 &vec0, const Vector3 &vec1)
Definition: neon/vec_aos.h:413
uint32_t contactFilterSelf
Definition: TrbStateVec.h:112
#define ATTRIBUTE_ALIGNED128(a)
Definition: btScalar.h:61
uint8_t useContactCallback
Definition: TrbStateVec.h:108
vmVector3 getPosition() const
Definition: TrbStateVec.h:178
uint8_t useCcd
Definition: TrbStateVec.h:107
float fOmega[3]
Definition: TrbStateVec.h:127
uint16_t trbBodyIdx
Definition: TrbStateVec.h:111
uint8_t useSleep
Definition: TrbStateVec.h:106
float fQ[4]
Definition: TrbStateVec.h:125
static void store_Quat(const vmQuat &src, float *p)
Definition: TrbStateVec.h:51
void setMotionType(uint8_t t)
Definition: TrbStateVec.h:152
uint8_t getMotionMask() const
Definition: TrbStateVec.h:154
vmQuat getOrientation() const
Definition: TrbStateVec.h:179
uint32_t getContactFilterTarget() const
Definition: TrbStateVec.h:141
unsigned short uint16_t
const Vector3 rotate(const Quat &quat, const Vector3 &vec)
static const Quat identity()
Definition: neon/quat_aos.h:68
uint8_t getUseCcd() const
Definition: TrbStateVec.h:165
uint8_t getUseSleepCallback() const
Definition: TrbStateVec.h:171
float fX[3]
Definition: TrbStateVec.h:124
Vectormath::Aos::Matrix3 vmMatrix3
Definition: vmInclude.h:25
uint16_t getSleepCount() const
Definition: TrbStateVec.h:176
static vmVector3 read_Vector3(const float *p)
Definition: TrbStateVec.h:31
void setPosition(const vmVector3 &pos)
Definition: TrbStateVec.h:185
static void store_Vector3(const vmVector3 &src, float *p)
Definition: TrbStateVec.h:45
bool isDeleted() const
Definition: TrbStateVec.h:132
float getLinearDamping() const
Definition: TrbStateVec.h:144
float center[3]
Definition: TrbStateVec.h:115
const Vector3 maxPerElem(const Vector3 &vec0, const Vector3 &vec1)
Definition: neon/vec_aos.h:396
unsigned char uint8_t
void setUseCcd(uint8_t b)
Definition: TrbStateVec.h:166
void setLinearDamping(float damping)
Definition: TrbStateVec.h:147
void setLinearVelocity(const vmVector3 &vel)
Definition: TrbStateVec.h:186
vmVector3 getDeltaLinearVelocity() const
Definition: TrbStateVec.h:182
void resetSleepCount()
Definition: TrbStateVec.h:175
uint8_t mSleeping
Definition: TrbStateVec.h:105
void setUseSleep(uint8_t b)
Definition: TrbStateVec.h:163
void sleep()
Definition: TrbStateVec.h:160
void setContactFilterSelf(uint32_t filter)
Definition: TrbStateVec.h:139
float getAngularDamping() const
Definition: TrbStateVec.h:145
unsigned int uint32_t
void setUseContactCallback(uint8_t b)
Definition: TrbStateVec.h:169
float half[3]
Definition: TrbStateVec.h:116
void reset()
Definition: TrbStateVec.h:301
bool isAsleep() const
Definition: TrbStateVec.h:156
bool isAwake() const
Definition: TrbStateVec.h:157
uint16_t getRigidBodyId() const
Definition: TrbStateVec.h:134
vmVector3 getAngularVelocity() const
Definition: TrbStateVec.h:181
#define ATTRIBUTE_ALIGNED16(a)
Definition: btScalar.h:59
uint32_t getContactFilterSelf() const
Definition: TrbStateVec.h:138
vmVector3 getDeltaAngularVelocity() const
Definition: TrbStateVec.h:183
uint32_t contactFilterTarget
Definition: TrbStateVec.h:113
static vmQuat read_Quat(const float *p)
Definition: TrbStateVec.h:38
uint8_t getUseSleep() const
Definition: TrbStateVec.h:162
void loadXYZW(Quat &quat, const float *fptr)
void setAuxils(const vmVector3 &centerLocal, const vmVector3 &halfLocal)
Definition: TrbStateVec.h:264
void setDeltaLinearVelocity(const vmVector3 &vel)
Definition: TrbStateVec.h:188
void setAuxilsCcd(const vmVector3 &centerLocal, const vmVector3 &halfLocal, float timeStep)
Definition: TrbStateVec.h:277
vmVector3 getLinearVelocity() const
Definition: TrbStateVec.h:180
void storeXYZW(const Quat &quat, float *fptr)
void wakeup()
Definition: TrbStateVec.h:159
void setZero()
Definition: TrbStateVec.h:246
uint8_t getMotionType() const
Definition: TrbStateVec.h:151
Vectormath::Aos::Vector3 vmVector3
Definition: vmInclude.h:23
void setDeltaAngularVelocity(const vmVector3 &vel)
Definition: TrbStateVec.h:189
void storeXYZ(const Vector3 &vec, float *fptr)
Definition: neon/vec_aos.h:105
uint8_t getUseContactCallback() const
Definition: TrbStateVec.h:168
float linearDamping
Definition: TrbStateVec.h:118
void incrementSleepCount()
Definition: TrbStateVec.h:174
const Matrix3 absPerElem(const Matrix3 &mat)
Definition: neon/mat_aos.h:233
void setRigidBodyId(uint16_t i)
Definition: TrbStateVec.h:135
float fV[3]
Definition: TrbStateVec.h:126
void setUseSleepCallback(uint8_t b)
Definition: TrbStateVec.h:172
void setIdentity()
Definition: TrbStateVec.h:228
void loadXYZ(Vector3 &vec, const float *fptr)
Definition: neon/vec_aos.h:100
uint8_t useSleepCallback
Definition: TrbStateVec.h:109