Tizen 2.1 base
[platform/upstream/libbullet.git] / Extras / PhysicsEffects / src / util / pfx_mass.cpp
1 /*\r
2 Physics Effects Copyright(C) 2010 Sony Computer Entertainment Inc.\r
3 All rights reserved.\r
4 \r
5 Physics Effects is open software; you can redistribute it and/or\r
6 modify it under the terms of the BSD License.\r
7 \r
8 Physics Effects is distributed in the hope that it will be useful,\r
9 but WITHOUT ANY WARRANTY; without even the implied warranty of\r
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\r
11 See the BSD License for more details.\r
12 \r
13 A copy of the BSD License is distributed with\r
14 Physics Effects under the filename: physics_effects_license.txt\r
15 */\r
16 \r
17 #include "../../include/physics_effects/util/pfx_mass.h"\r
18 \r
19 namespace sce {\r
20 namespace PhysicsEffects {\r
21 \r
22 ///////////////////////////////////////////////////////////////////////////////\r
23 // Box\r
24 \r
25 PfxFloat pfxCalcMassBox(PfxFloat density,const PfxVector3 &halfExtent)\r
26 {\r
27         return density * halfExtent[0] * halfExtent[1] * halfExtent[2] * 8;\r
28 }\r
29 \r
30 PfxMatrix3 pfxCalcInertiaBox(const PfxVector3 &halfExtent,PfxFloat mass)\r
31 {\r
32         PfxVector3 sqrSz = halfExtent * 2.0f;\r
33         sqrSz = mulPerElem(sqrSz,sqrSz);\r
34         PfxMatrix3 inertia = PfxMatrix3::identity();\r
35         inertia[0][0] = (mass*(sqrSz[1]+sqrSz[2]))/12.0f;\r
36         inertia[1][1] = (mass*(sqrSz[0]+sqrSz[2]))/12.0f;\r
37         inertia[2][2] = (mass*(sqrSz[0]+sqrSz[1]))/12.0f;\r
38         return inertia;\r
39 }\r
40 \r
41 ///////////////////////////////////////////////////////////////////////////////\r
42 // Sphere\r
43 \r
44 PfxFloat pfxCalcMassSphere(PfxFloat density,PfxFloat radius)\r
45 {\r
46         return (4.0f/3.0f) * SCE_PFX_PI * radius * radius * radius * density;\r
47 }\r
48 \r
49 PfxMatrix3 pfxCalcInertiaSphere(PfxFloat radius,PfxFloat mass)\r
50 {\r
51         PfxMatrix3 inertia = PfxMatrix3::identity();\r
52         inertia[0][0] = inertia[1][1] = inertia[2][2] = 0.4f * mass * radius * radius;\r
53         return inertia;\r
54 }\r
55 \r
56 ///////////////////////////////////////////////////////////////////////////////\r
57 // Cylinder\r
58 \r
59 PfxFloat pfxCalcMassCylinder(PfxFloat density,PfxFloat halfLength,PfxFloat radius)\r
60 {\r
61         return SCE_PFX_PI * radius * radius * 2.0f * halfLength * density;\r
62 }\r
63 \r
64 static inline\r
65 PfxMatrix3 pfxCalcInertiaCylinder(PfxFloat halfLength,PfxFloat radius,PfxFloat mass,int axis)\r
66 {\r
67         PfxMatrix3 inertia = PfxMatrix3::identity();\r
68         inertia[0][0] = inertia[1][1] = inertia[2][2] = 0.25f * mass * radius * radius + 0.33f * mass * halfLength * halfLength; \r
69         inertia[axis][axis] = 0.5f * mass * radius * radius;\r
70         return inertia;\r
71 }\r
72 \r
73 PfxMatrix3 pfxCalcInertiaCylinderX(PfxFloat halfLength,PfxFloat radius,PfxFloat mass)\r
74 {\r
75         return pfxCalcInertiaCylinder(radius,halfLength,mass,0);\r
76 }\r
77 \r
78 PfxMatrix3 pfxCalcInertiaCylinderY(PfxFloat halfLength,PfxFloat radius,PfxFloat mass)\r
79 {\r
80         return pfxCalcInertiaCylinder(radius,halfLength,mass,1);\r
81 }\r
82 \r
83 PfxMatrix3 pfxCalcInertiaCylinderZ(PfxFloat halfLength,PfxFloat radius,PfxFloat mass)\r
84 {\r
85         return pfxCalcInertiaCylinder(radius,halfLength,mass,2);\r
86 }\r
87 \r
88 ///////////////////////////////////////////////////////////////////////////////\r
89 \r
90 PfxMatrix3 pfxMassTranslate(PfxFloat mass,const PfxMatrix3 &inertia,const PfxVector3 &translation)\r
91 {\r
92         PfxMatrix3 m = crossMatrix(translation);\r
93         return inertia + mass * (-m*m);\r
94 }\r
95 \r
96 PfxMatrix3 pfxMassRotate(const PfxMatrix3 &inertia,const PfxMatrix3 &rotate)\r
97 {\r
98         return rotate * inertia * transpose(rotate);\r
99 }\r
100 \r
101 } //namespace PhysicsEffects
102 } //namespace sce\r