aboutsummaryrefslogtreecommitdiff
path: root/tests/bullet/src/BulletMultiThreaded/TrbStateVec.h
blob: 46d1904aa64d84b99c8ca6b6c48e6ab568980dcc (plain)
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/*
   Copyright (C) 2009 Sony Computer Entertainment Inc.
   All rights reserved.

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.

*/

#ifndef BT_TRBSTATEVEC_H__
#define BT_TRBSTATEVEC_H__

#include <stdlib.h>
#ifdef PFX_USE_FREE_VECTORMATH
#include "vecmath/vmInclude.h"
#else
#include "vectormath/vmInclude.h"
#endif //PFX_USE_FREE_VECTORMATH


#include "PlatformDefinitions.h"


static inline vmVector3 read_Vector3(const float* p)
{
	vmVector3 v;
	loadXYZ(v, p);
	return v;
}

static inline vmQuat read_Quat(const float* p)
{
	vmQuat vq;
	loadXYZW(vq, p);
	return vq;
}

static inline void store_Vector3(const vmVector3 &src, float* p)
{
	vmVector3 v = src;
	storeXYZ(v, p);
}

static inline void store_Quat(const vmQuat &src, float* p)
{
	vmQuat vq = src;
	storeXYZW(vq, p);
}

// Motion Type
enum {
	PfxMotionTypeFixed = 0,
	PfxMotionTypeActive,
	PfxMotionTypeKeyframe,
	PfxMotionTypeOneWay,
	PfxMotionTypeTrigger,
	PfxMotionTypeCount
};

#define PFX_MOTION_MASK_DYNAMIC 0x0a // Active,OneWay
#define PFX_MOTION_MASK_STATIC  0x95 // Fixed,Keyframe,Trigger,Sleeping
#define PFX_MOTION_MASK_SLEEP   0x0e // Can sleep
#define PFX_MOTION_MASK_TYPE    0x7f

//
// Rigid Body state
//

#ifdef __CELLOS_LV2__
ATTRIBUTE_ALIGNED128(class) TrbState
#else
ATTRIBUTE_ALIGNED16(class) TrbState
#endif

{
public:
	TrbState()
	{
		setMotionType(PfxMotionTypeActive);
		contactFilterSelf=contactFilterTarget=0xffffffff;
		deleted = 0;
		mSleeping = 0;
		useSleep = 1;
		trbBodyIdx=0;
		mSleepCount=0;
		useCcd = 0;
		useContactCallback = 0;
		useSleepCallback = 0;
		linearDamping = 1.0f;
		angularDamping = 0.99f;
	}

	TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega );
	
	uint16_t	mSleepCount;
	uint8_t		mMotionType;
	uint8_t		deleted            : 1;
	uint8_t		mSleeping           : 1;
	uint8_t		useSleep           : 1;
	uint8_t		useCcd		       : 1;
	uint8_t		useContactCallback : 1;
	uint8_t		useSleepCallback   : 1;

	uint16_t	trbBodyIdx;
	uint32_t	contactFilterSelf;
	uint32_t	contactFilterTarget;

	float		center[3];		// AABB center(World)
	float		half[3];		// AABB half(World)

	float		linearDamping;
	float		angularDamping;
	
	float		deltaLinearVelocity[3];
	float		deltaAngularVelocity[3];

	float     fX[3];				// position
	float     fQ[4];				// orientation
	float     fV[3];				// velocity
	float     fOmega[3];			// angular velocity

	inline void setZero();      // Zeroes out the elements
	inline void setIdentity();  // Sets the rotation to identity and zeroes out the other elements

	bool		isDeleted() const {return deleted==1;}

	uint16_t	getRigidBodyId() const {return trbBodyIdx;}
	void		setRigidBodyId(uint16_t i) {trbBodyIdx = i;}


	uint32_t	getContactFilterSelf() const {return contactFilterSelf;}
	void		setContactFilterSelf(uint32_t filter) {contactFilterSelf = filter;}

	uint32_t	getContactFilterTarget() const {return contactFilterTarget;}
	void		setContactFilterTarget(uint32_t filter) {contactFilterTarget = filter;}

	float getLinearDamping() const {return linearDamping;}
	float getAngularDamping() const {return angularDamping;}

	void setLinearDamping(float damping) {linearDamping=damping;}
	void setAngularDamping(float damping) {angularDamping=damping;}


	uint8_t		getMotionType() const {return mMotionType;}
	void		setMotionType(uint8_t t) {mMotionType = t;mSleeping=0;mSleepCount=0;}

	uint8_t		getMotionMask() const {return (1<<mMotionType)|(mSleeping<<7);}

	bool		isAsleep() const {return mSleeping==1;}
	bool		isAwake() const {return mSleeping==0;}

	void		wakeup() {mSleeping=0;mSleepCount=0;}
	void		sleep() {if(useSleep) {mSleeping=1;mSleepCount=0;}}

	uint8_t		getUseSleep() const {return useSleep;}
	void		setUseSleep(uint8_t b) {useSleep=b;}

	uint8_t		getUseCcd() const {return useCcd;}
	void		setUseCcd(uint8_t b) {useCcd=b;}

	uint8_t		getUseContactCallback() const {return useContactCallback;}
	void		setUseContactCallback(uint8_t b) {useContactCallback=b;}

	uint8_t		getUseSleepCallback() const {return useSleepCallback;}
	void		setUseSleepCallback(uint8_t b) {useSleepCallback=b;}

	void	 	incrementSleepCount() {mSleepCount++;}
	void		resetSleepCount() {mSleepCount=0;}
	uint16_t	getSleepCount() const {return mSleepCount;}

	vmVector3 getPosition() const {return read_Vector3(fX);}
	vmQuat    getOrientation() const {return read_Quat(fQ);}
	vmVector3 getLinearVelocity() const {return read_Vector3(fV);}
	vmVector3 getAngularVelocity() const {return read_Vector3(fOmega);}
	vmVector3 getDeltaLinearVelocity() const {return read_Vector3(deltaLinearVelocity);}
	vmVector3 getDeltaAngularVelocity() const {return read_Vector3(deltaAngularVelocity);}

	void setPosition(const vmVector3 &pos) {store_Vector3(pos, fX);}
	void setLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, fV);}
	void setAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, fOmega);}
	void setDeltaLinearVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaLinearVelocity);}
	void setDeltaAngularVelocity(const vmVector3 &vel) {store_Vector3(vel, deltaAngularVelocity);}
	void setOrientation(const vmQuat &rot) {store_Quat(rot, fQ);}

	inline void setAuxils(const vmVector3 &centerLocal,const vmVector3 &halfLocal);
	inline void	setAuxilsCcd(const vmVector3 &centerLocal,const vmVector3 &halfLocal,float timeStep);
	inline	void reset();
};

inline
TrbState::TrbState(const uint8_t m, const vmVector3& x, const vmQuat& q, const vmVector3& v, const vmVector3& omega)
{
	setMotionType(m);
	fX[0] = x[0];
	fX[1] = x[1];
	fX[2] = x[2];
	fQ[0] = q[0];
	fQ[1] = q[1];
	fQ[2] = q[2];
	fQ[3] = q[3];
	fV[0] = v[0];
	fV[1] = v[1];
	fV[2] = v[2];
	fOmega[0] = omega[0];
	fOmega[1] = omega[1];
	fOmega[2] = omega[2];
	contactFilterSelf=contactFilterTarget=0xffff;
	trbBodyIdx=0;
	mSleeping = 0;
	deleted = 0;
	useSleep = 1;
	useCcd = 0;
	useContactCallback = 0;
	useSleepCallback = 0;
	mSleepCount=0;
	linearDamping = 1.0f;
	angularDamping = 0.99f;
}

inline void
TrbState::setIdentity()
{
	fX[0] = 0.0f;
	fX[1] = 0.0f;
	fX[2] = 0.0f;
	fQ[0] = 0.0f;
	fQ[1] = 0.0f;
	fQ[2] = 0.0f;
	fQ[3] = 1.0f;
	fV[0] = 0.0f;
	fV[1] = 0.0f;
	fV[2] = 0.0f;
	fOmega[0] = 0.0f;
	fOmega[1] = 0.0f;
	fOmega[2] = 0.0f;
}

inline void
TrbState::setZero()
{
	fX[0] = 0.0f;
	fX