-
Notifications
You must be signed in to change notification settings - Fork 267
/
Copy pathcore.h
1243 lines (1110 loc) · 38.8 KB
/
core.h
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Interface file for core components and functions.
*
* Part of the Cyclone physics system.
*
* Copyright (c) Icosagon 2003. All Rights Reserved.
*
* This software is distributed under licence. Use of this software
* implies agreement with all terms and conditions of the accompanying
* software licence.
*/
/**
* @mainpage Cyclone Reference
*
* Cyclone is a general purpose and robust system for real-time
* simulation of rigid bodies. The library was designed to be used in
* computer games, but may be applicable to other areas of simulation
* or research.
*
* @section docs About This Document
*
* This documentation contains detailed reference to every aspect of
* the Cyclone library.
*
* @subsection contents Contents
*
* Use the navigation system on the left side of the page to view the
* documentation. The navigation tool uses JavaScript, and requires a
* version 4 browser or above.
*
* All the publically accessible functions and classes of Cyclone
* are provided in a set of header files. These, and their contents,
* can be browsed from the File List section.
*
* Cyclone is contained in a single namespace, cyclone. Its
* contents can be viewed in the Compound List section. The Class
* Hierarchy section provides an alternative way to navigate these
* classes. The Graphical Class Hierarchy provides an overview of
* class inheritance.
*
* The Compound List section gives an alphabetic list of all symbols
* in the library, including method names and functions.
*
* @subsection graphs Graphs
*
* Most of the documentation contains detailed graphical
* representations of the file and class dependencies. These diagrams
* are clickable, and provide the fastest mechanism for browsing the
* documentation. Each diagram is followed by a link to a help file
* giving a legend.
*
* @section use Using Cyclone
*
* To set up:
*
* @li Create a set of instances of RigidBody.
*
* @li Set their mass, inertia tensor, and damping.
*
* @li Set their initial location, orientation, velocity and rotation.
*
* @li Apply any permanent forces (such as gravity).
*
* Then each frame:
*
* @li Apply any transient forces (such as springs or thrusts).
*
* @li Call eulerIntegrate on each body in turn.
*
* @li Fill an array of Contact instances with all contacts on all
* bodies.
*
* @li Call ContactResolver::resolveContacts to resolve the
* contacts.
*
* @li Call calculateInternals to update the bodies' internal
* properties (such as the transform matrix).
*
* @li Render the bodies.
*
* @section legal Legal
*
* This documentation is distributed under license. Use of this
* documentation implies agreement with all terms and conditions of
* the accompanying software and documentation license.
*/
#include <math.h>
/**
* @file
*
* The core contains utility functions, helpers and a basic set of
* mathematical types.
*/
#ifndef CYCLONE_CORE_H
#define CYCLONE_CORE_H
#include "precision.h"
/**
* The cyclone namespace includes all cyclone functions and
* classes. It is defined as a namespace to allow function and class
* names to be simple without causing conflicts.
*/
namespace cyclone {
/**
* Holds the value for energy under which a body will be put to
* sleep. This is a global value for the whole solution. By
* default it is 0.1, which is fine for simulation when gravity is
* about 20 units per second squared, masses are about one, and
* other forces are around that of gravity. It may need tweaking
* if your simulation is drastically different to this.
*/
extern real sleepEpsilon;
/**
* Sets the current sleep epsilon value: the kinetic energy under
* which a body may be put to sleep. Bodies are put to sleep if
* they appear to have a stable kinetic energy less than this
* value. For simulations that often have low values (such as slow
* moving, or light objects), this may need reducing.
*
* The value is global; all bodies will use it.
*
* @see sleepEpsilon
*
* @see getSleepEpsilon
*
* @param value The sleep epsilon value to use from this point
* on.
*/
void setSleepEpsilon(real value);
/**
* Gets the current value of the sleep epsilon parameter.
*
* @see sleepEpsilon
*
* @see setSleepEpsilon
*
* @return The current value of the parameter.
*/
real getSleepEpsilon();
/**
* Holds a vector in 3 dimensions. Four data members are allocated
* to ensure alignment in an array.
*
* @note This class contains a lot of inline methods for basic
* mathematics. The implementations are included in the header
* file.
*/
class Vector3
{
public:
/** Holds the value along the x axis. */
real x;
/** Holds the value along the y axis. */
real y;
/** Holds the value along the z axis. */
real z;
private:
/** Padding to ensure 4 word alignment. */
real pad;
public:
/** The default constructor creates a zero vector. */
Vector3() : x(0), y(0), z(0) {}
/**
* The explicit constructor creates a vector with the given
* components.
*/
Vector3(const real x, const real y, const real z)
: x(x), y(y), z(z) {}
const static Vector3 GRAVITY;
const static Vector3 HIGH_GRAVITY;
const static Vector3 UP;
const static Vector3 RIGHT;
const static Vector3 OUT_OF_SCREEN;
const static Vector3 X;
const static Vector3 Y;
const static Vector3 Z;
// ... Other Vector3 code as before ...
real operator[](unsigned i) const
{
if (i == 0) return x;
if (i == 1) return y;
return z;
}
real& operator[](unsigned i)
{
if (i == 0) return x;
if (i == 1) return y;
return z;
}
/** Adds the given vector to this. */
void operator+=(const Vector3& v)
{
x += v.x;
y += v.y;
z += v.z;
}
/**
* Returns the value of the given vector added to this.
*/
Vector3 operator+(const Vector3& v) const
{
return Vector3(x+v.x, y+v.y, z+v.z);
}
/** Subtracts the given vector from this. */
void operator-=(const Vector3& v)
{
x -= v.x;
y -= v.y;
z -= v.z;
}
/**
* Returns the value of the given vector subtracted from this.
*/
Vector3 operator-(const Vector3& v) const
{
return Vector3(x-v.x, y-v.y, z-v.z);
}
/** Multiplies this vector by the given scalar. */
void operator*=(const real value)
{
x *= value;
y *= value;
z *= value;
}
/** Returns a copy of this vector scaled the given value. */
Vector3 operator*(const real value) const
{
return Vector3(x*value, y*value, z*value);
}
/**
* Calculates and returns a component-wise product of this
* vector with the given vector.
*/
Vector3 componentProduct(const Vector3 &vector) const
{
return Vector3(x * vector.x, y * vector.y, z * vector.z);
}
/**
* Performs a component-wise product with the given vector and
* sets this vector to its result.
*/
void componentProductUpdate(const Vector3 &vector)
{
x *= vector.x;
y *= vector.y;
z *= vector.z;
}
/**
* Calculates and returns the vector product of this vector
* with the given vector.
*/
Vector3 vectorProduct(const Vector3 &vector) const
{
return Vector3(y*vector.z-z*vector.y,
z*vector.x-x*vector.z,
x*vector.y-y*vector.x);
}
/**
* Updates this vector to be the vector product of its current
* value and the given vector.
*/
void operator %=(const Vector3 &vector)
{
*this = vectorProduct(vector);
}
/**
* Calculates and returns the vector product of this vector
* with the given vector.
*/
Vector3 operator%(const Vector3 &vector) const
{
return Vector3(y*vector.z-z*vector.y,
z*vector.x-x*vector.z,
x*vector.y-y*vector.x);
}
/**
* Calculates and returns the scalar product of this vector
* with the given vector.
*/
real scalarProduct(const Vector3 &vector) const
{
return x*vector.x + y*vector.y + z*vector.z;
}
/**
* Calculates and returns the scalar product of this vector
* with the given vector.
*/
real operator *(const Vector3 &vector) const
{
return x*vector.x + y*vector.y + z*vector.z;
}
/**
* Adds the given vector to this, scaled by the given amount.
*/
void addScaledVector(const Vector3& vector, real scale)
{
x += vector.x * scale;
y += vector.y * scale;
z += vector.z * scale;
}
/** Gets the magnitude of this vector. */
real magnitude() const
{
return real_sqrt(x*x+y*y+z*z);
}
/** Gets the squared magnitude of this vector. */
real squareMagnitude() const
{
return x*x+y*y+z*z;
}
/** Limits the size of the vector to the given maximum. */
void trim(real size)
{
if (squareMagnitude() > size*size)
{
normalise();
x *= size;
y *= size;
z *= size;
}
}
/** Turns a non-zero vector into a vector of unit length. */
void normalise()
{
real l = magnitude();
if (l > 0)
{
(*this) *= ((real)1)/l;
}
}
/** Returns the normalised version of a vector. */
Vector3 unit() const
{
Vector3 result = *this;
result.normalise();
return result;
}
/** Checks if the two vectors have identical components. */
bool operator==(const Vector3& other) const
{
return x == other.x &&
y == other.y &&
z == other.z;
}
/** Checks if the two vectors have non-identical components. */
bool operator!=(const Vector3& other) const
{
return !(*this == other);
}
/**
* Checks if this vector is component-by-component less than
* the other.
*
* @note This does not behave like a single-value comparison:
* !(a < b) does not imply (b >= a).
*/
bool operator<(const Vector3& other) const
{
return x < other.x && y < other.y && z < other.z;
}
/**
* Checks if this vector is component-by-component less than
* the other.
*
* @note This does not behave like a single-value comparison:
* !(a < b) does not imply (b >= a).
*/
bool operator>(const Vector3& other) const
{
return x > other.x && y > other.y && z > other.z;
}
/**
* Checks if this vector is component-by-component less than
* the other.
*
* @note This does not behave like a single-value comparison:
* !(a <= b) does not imply (b > a).
*/
bool operator<=(const Vector3& other) const
{
return x <= other.x && y <= other.y && z <= other.z;
}
/**
* Checks if this vector is component-by-component less than
* the other.
*
* @note This does not behave like a single-value comparison:
* !(a <= b) does not imply (b > a).
*/
bool operator>=(const Vector3& other) const
{
return x >= other.x && y >= other.y && z >= other.z;
}
/** Zero all the components of the vector. */
void clear()
{
x = y = z = 0;
}
/** Flips all the components of the vector. */
void invert()
{
x = -x;
y = -y;
z = -z;
}
};
/**
* Holds a three degree of freedom orientation.
*
* Quaternions have
* several mathematical properties that make them useful for
* representing orientations, but require four items of data to
* hold the three degrees of freedom. These four items of data can
* be viewed as the coefficients of a complex number with three
* imaginary parts. The mathematics of the quaternion is then
* defined and is roughly correspondent to the math of 3D
* rotations. A quaternion is only a valid rotation if it is
* normalised: i.e. it has a length of 1.
*
* @note Angular velocity and acceleration can be correctly
* represented as vectors. Quaternions are only needed for
* orientation.
*/
class Quaternion
{
public:
union {
struct {
/**
* Holds the real component of the quaternion.
*/
real r;
/**
* Holds the first complex component of the
* quaternion.
*/
real i;
/**
* Holds the second complex component of the
* quaternion.
*/
real j;
/**
* Holds the third complex component of the
* quaternion.
*/
real k;
};
/**
* Holds the quaternion data in array form.
*/
real data[4];
};
// ... other Quaternion code as before ...
/**
* The default constructor creates a quaternion representing
* a zero rotation.
*/
Quaternion() : r(1), i(0), j(0), k(0) {}
/**
* The explicit constructor creates a quaternion with the given
* components.
*
* @param r The real component of the rigid body's orientation
* quaternion.
*
* @param i The first complex component of the rigid body's
* orientation quaternion.
*
* @param j The second complex component of the rigid body's
* orientation quaternion.
*
* @param k The third complex component of the rigid body's
* orientation quaternion.
*
* @note The given orientation does not need to be normalised,
* and can be zero. This function will not alter the given
* values, or normalise the quaternion. To normalise the
* quaternion (and make a zero quaternion a legal rotation),
* use the normalise function.
*
* @see normalise
*/
Quaternion(const real r, const real i, const real j, const real k)
: r(r), i(i), j(j), k(k)
{
}
/**
* Normalises the quaternion to unit length, making it a valid
* orientation quaternion.
*/
void normalise()
{
real d = r*r+i*i+j*j+k*k;
// Check for zero length quaternion, and use the no-rotation
// quaternion in that case.
if (d < real_epsilon) {
r = 1;
return;
}
d = ((real)1.0)/real_sqrt(d);
r *= d;
i *= d;
j *= d;
k *= d;
}
/**
* Multiplies the quaternion by the given quaternion.
*
* @param multiplier The quaternion by which to multiply.
*/
void operator *=(const Quaternion &multiplier)
{
Quaternion q = *this;
r = q.r*multiplier.r - q.i*multiplier.i -
q.j*multiplier.j - q.k*multiplier.k;
i = q.r*multiplier.i + q.i*multiplier.r +
q.j*multiplier.k - q.k*multiplier.j;
j = q.r*multiplier.j + q.j*multiplier.r +
q.k*multiplier.i - q.i*multiplier.k;
k = q.r*multiplier.k + q.k*multiplier.r +
q.i*multiplier.j - q.j*multiplier.i;
}
/**
* Adds the given vector to this, scaled by the given amount.
* This is used to update the orientation quaternion by a rotation
* and time.
*
* @param vector The vector to add.
*
* @param scale The amount of the vector to add.
*/
void addScaledVector(const Vector3& vector, real scale)
{
Quaternion q(0,
vector.x * scale,
vector.y * scale,
vector.z * scale);
q *= *this;
r += q.r * ((real)0.5);
i += q.i * ((real)0.5);
j += q.j * ((real)0.5);
k += q.k * ((real)0.5);
}
void rotateByVector(const Vector3& vector)
{
Quaternion q(0, vector.x, vector.y, vector.z);
(*this) *= q;
}
};
/**
* Holds a transform matrix, consisting of a rotation matrix and
* a position. The matrix has 12 elements, it is assumed that the
* remaining four are (0,0,0,1); producing a homogenous matrix.
*/
class Matrix4
{
public:
/**
* Holds the transform matrix data in array form.
*/
real data[12];
// ... Other Matrix4 code as before ...
/**
* Creates an identity matrix.
*/
Matrix4()
{
data[1] = data[2] = data[3] = data[4] = data[6] =
data[7] = data[8] = data[9] = data[11] = 0;
data[0] = data[5] = data[10] = 1;
}
/**
* Sets the matrix to be a diagonal matrix with the given coefficients.
*/
void setDiagonal(real a, real b, real c)
{
data[0] = a;
data[5] = b;
data[10] = c;
}
/**
* Returns a matrix which is this matrix multiplied by the given
* other matrix.
*/
Matrix4 operator*(const Matrix4 &o) const
{
Matrix4 result;
result.data[0] = (o.data[0]*data[0]) + (o.data[4]*data[1]) + (o.data[8]*data[2]);
result.data[4] = (o.data[0]*data[4]) + (o.data[4]*data[5]) + (o.data[8]*data[6]);
result.data[8] = (o.data[0]*data[8]) + (o.data[4]*data[9]) + (o.data[8]*data[10]);
result.data[1] = (o.data[1]*data[0]) + (o.data[5]*data[1]) + (o.data[9]*data[2]);
result.data[5] = (o.data[1]*data[4]) + (o.data[5]*data[5]) + (o.data[9]*data[6]);
result.data[9] = (o.data[1]*data[8]) + (o.data[5]*data[9]) + (o.data[9]*data[10]);
result.data[2] = (o.data[2]*data[0]) + (o.data[6]*data[1]) + (o.data[10]*data[2]);
result.data[6] = (o.data[2]*data[4]) + (o.data[6]*data[5]) + (o.data[10]*data[6]);
result.data[10] = (o.data[2]*data[8]) + (o.data[6]*data[9]) + (o.data[10]*data[10]);
result.data[3] = (o.data[3]*data[0]) + (o.data[7]*data[1]) + (o.data[11]*data[2]) + data[3];
result.data[7] = (o.data[3]*data[4]) + (o.data[7]*data[5]) + (o.data[11]*data[6]) + data[7];
result.data[11] = (o.data[3]*data[8]) + (o.data[7]*data[9]) + (o.data[11]*data[10]) + data[11];
return result;
}
/**
* Transform the given vector by this matrix.
*
* @param vector The vector to transform.
*/
Vector3 operator*(const Vector3 &vector) const
{
return Vector3(
vector.x * data[0] +
vector.y * data[1] +
vector.z * data[2] + data[3],
vector.x * data[4] +
vector.y * data[5] +
vector.z * data[6] + data[7],
vector.x * data[8] +
vector.y * data[9] +
vector.z * data[10] + data[11]
);
}
/**
* Transform the given vector by this matrix.
*
* @param vector The vector to transform.
*/
Vector3 transform(const Vector3 &vector) const
{
return (*this) * vector;
}
/**
* Returns the determinant of the matrix.
*/
real getDeterminant() const;
/**
* Sets the matrix to be the inverse of the given matrix.
*
* @param m The matrix to invert and use to set this.
*/
void setInverse(const Matrix4 &m);
/** Returns a new matrix containing the inverse of this matrix. */
Matrix4 inverse() const
{
Matrix4 result;
result.setInverse(*this);
return result;
}
/**
* Inverts the matrix.
*/
void invert()
{
setInverse(*this);
}
/**
* Transform the given direction vector by this matrix.
*
* @note When a direction is converted between frames of
* reference, there is no translation required.
*
* @param vector The vector to transform.
*/
Vector3 transformDirection(const Vector3 &vector) const
{
return Vector3(
vector.x * data[0] +
vector.y * data[1] +
vector.z * data[2],
vector.x * data[4] +
vector.y * data[5] +
vector.z * data[6],
vector.x * data[8] +
vector.y * data[9] +
vector.z * data[10]
);
}
/**
* Transform the given direction vector by the
* transformational inverse of this matrix.
*
* @note This function relies on the fact that the inverse of
* a pure rotation matrix is its transpose. It separates the
* translational and rotation components, transposes the
* rotation, and multiplies out. If the matrix is not a
* scale and shear free transform matrix, then this function
* will not give correct results.
*
* @note When a direction is converted between frames of
* reference, there is no translation required.
*
* @param vector The vector to transform.
*/
Vector3 transformInverseDirection(const Vector3 &vector) const
{
return Vector3(
vector.x * data[0] +
vector.y * data[4] +
vector.z * data[8],
vector.x * data[1] +
vector.y * data[5] +
vector.z * data[9],
vector.x * data[2] +
vector.y * data[6] +
vector.z * data[10]
);
}
/**
* Transform the given vector by the transformational inverse
* of this matrix.
*
* @note This function relies on the fact that the inverse of
* a pure rotation matrix is its transpose. It separates the
* translational and rotation components, transposes the
* rotation, and multiplies out. If the matrix is not a
* scale and shear free transform matrix, then this function
* will not give correct results.
*
* @param vector The vector to transform.
*/
Vector3 transformInverse(const Vector3 &vector) const
{
Vector3 tmp = vector;
tmp.x -= data[3];
tmp.y -= data[7];
tmp.z -= data[11];
return Vector3(
tmp.x * data[0] +
tmp.y * data[4] +
tmp.z * data[8],
tmp.x * data[1] +
tmp.y * data[5] +
tmp.z * data[9],
tmp.x * data[2] +
tmp.y * data[6] +
tmp.z * data[10]
);
}
/**
* Gets a vector representing one axis (i.e. one column) in the matrix.
*
* @param i The row to return. Row 3 corresponds to the position
* of the transform matrix.
*
* @return The vector.
*/
Vector3 getAxisVector(int i) const
{
return Vector3(data[i], data[i+4], data[i+8]);
}
/**
* Sets this matrix to be the rotation matrix corresponding to
* the given quaternion.
*/
void setOrientationAndPos(const Quaternion &q, const Vector3 &pos)
{
data[0] = 1 - (2*q.j*q.j + 2*q.k*q.k);
data[1] = 2*q.i*q.j + 2*q.k*q.r;
data[2] = 2*q.i*q.k - 2*q.j*q.r;
data[3] = pos.x;
data[4] = 2*q.i*q.j - 2*q.k*q.r;
data[5] = 1 - (2*q.i*q.i + 2*q.k*q.k);
data[6] = 2*q.j*q.k + 2*q.i*q.r;
data[7] = pos.y;
data[8] = 2*q.i*q.k + 2*q.j*q.r;
data[9] = 2*q.j*q.k - 2*q.i*q.r;
data[10] = 1 - (2*q.i*q.i + 2*q.j*q.j);
data[11] = pos.z;
}
/**
* Fills the given array with this transform matrix, so it is
* usable as an open-gl transform matrix. OpenGL uses a column
* major format, so that the values are transposed as they are
* written.
*/
void fillGLArray(float array[16]) const
{
array[0] = (float)data[0];
array[1] = (float)data[4];
array[2] = (float)data[8];
array[3] = (float)0;
array[4] = (float)data[1];
array[5] = (float)data[5];
array[6] = (float)data[9];
array[7] = (float)0;
array[8] = (float)data[2];
array[9] = (float)data[6];
array[10] = (float)data[10];
array[11] = (float)0;
array[12] = (float)data[3];
array[13] = (float)data[7];
array[14] = (float)data[11];
array[15] = (float)1;
}
};
/**
* Holds an inertia tensor, consisting of a 3x3 row-major matrix.
* This matrix is not padding to produce an aligned structure, since
* it is most commonly used with a mass (single real) and two
* damping coefficients to make the 12-element characteristics array
* of a rigid body.
*/
class Matrix3
{
public:
/**
* Holds the tensor matrix data in array form.
*/
real data[9];
// ... Other Matrix3 code as before ...
/**
* Creates a new matrix.
*/
Matrix3()
{
data[0] = data[1] = data[2] = data[3] = data[4] = data[5] =
data[6] = data[7] = data[8] = 0;
}
/**
* Creates a new matrix with the given three vectors making
* up its columns.
*/
Matrix3(const Vector3 &compOne, const Vector3 &compTwo,
const Vector3 &compThree)
{
setComponents(compOne, compTwo, compThree);
}
/**
* Creates a new matrix with explicit coefficients.
*/
Matrix3(real c0, real c1, real c2, real c3, real c4, real c5,
real c6, real c7, real c8)
{
data[0] = c0; data[1] = c1; data[2] = c2;
data[3] = c3; data[4] = c4; data[5] = c5;
data[6] = c6; data[7] = c7; data[8] = c8;
}
/**
* Sets the matrix to be a diagonal matrix with the given
* values along the leading diagonal.
*/
void setDiagonal(real a, real b, real c)
{
setInertiaTensorCoeffs(a, b, c);
}
/**
* Sets the value of the matrix from inertia tensor values.
*/
void setInertiaTensorCoeffs(real ix, real iy, real iz,
real ixy=0, real ixz=0, real iyz=0)
{
data[0] = ix;
data[1] = data[3] = -ixy;
data[2] = data[6] = -ixz;
data[4] = iy;
data[5] = data[7] = -iyz;
data[8] = iz;
}
/**
* Sets the value of the matrix as an inertia tensor of
* a rectangular block aligned with the body's coordinate
* system with the given axis half-sizes and mass.
*/
void setBlockInertiaTensor(const Vector3 &halfSizes, real mass)
{
Vector3 squares = halfSizes.componentProduct(halfSizes);
setInertiaTensorCoeffs(0.3f*mass*(squares.y + squares.z),
0.3f*mass*(squares.x + squares.z),
0.3f*mass*(squares.x + squares.y));
}
/**
* Sets the matrix to be a skew symmetric matrix based on
* the given vector. The skew symmetric matrix is the equivalent
* of the vector product. So if a,b are vectors. a x b = A_s b
* where A_s is the skew symmetric form of a.
*/
void setSkewSymmetric(const Vector3 vector)
{
data[0] = data[4] = data[8] = 0;
data[1] = -vector.z;
data[2] = vector.y;
data[3] = vector.z;
data[5] = -vector.x;
data[6] = -vector.y;
data[7] = vector.x;
}
/**
* Sets the matrix values from the given three vector components.
* These are arranged as the three columns of the vector.
*/
void setComponents(const Vector3 &compOne, const Vector3 &compTwo,
const Vector3 &compThree)
{
data[0] = compOne.x;
data[1] = compTwo.x;
data[2] = compThree.x;