1 | // |
---|
2 | // Orbital |
---|
3 | // |
---|
4 | // Copyright: (c) 2009-2012 Cédric Lecacheur <jordx@free.fr> |
---|
5 | // (c) 2009-2012 Benjamin Huet <huet.benjamin@gmail.com> |
---|
6 | // (c) 2012 Sam Hocevar <sam@hocevar.net> |
---|
7 | // |
---|
8 | |
---|
9 | /* FIXME: this file is pure crap; it's only a test. */ |
---|
10 | |
---|
11 | #if !defined __PHYSICOBJECT_H__ |
---|
12 | #define __PHYSICOBJECT_H__ |
---|
13 | |
---|
14 | #include "core.h"
|
---|
15 | #include "easymesh/easymesh.h" |
---|
16 | #include "Physics/EasyPhysics.h" |
---|
17 | |
---|
18 | using namespace lol;
|
---|
19 | using namespace lol::phys;
|
---|
20 | |
---|
21 | class PhysicsObject : public WorldEntity |
---|
22 | { |
---|
23 | public: |
---|
24 | PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation) |
---|
25 | : m_ready(false), m_should_render(true) |
---|
26 | { |
---|
27 | m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]"); |
---|
28 | vec3 BoxSize = vec3(60.f, 1.f, 60.f); |
---|
29 | m_physics.SetCollisionChannel(0, 0xFF); |
---|
30 | m_physics.SetShapeToBox(BoxSize); |
---|
31 | m_physics.SetMass(.0f); |
---|
32 | m_physics.SetTransform(base_location, base_rotation); |
---|
33 | m_physics.InitBodyToRigid(true); |
---|
34 | m_physics.AddToSimulation(new_sim); |
---|
35 | } |
---|
36 | |
---|
37 | PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy) |
---|
38 | : m_ready(false), m_should_render(true) |
---|
39 | { |
---|
40 | m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]"); |
---|
41 | vec3 BoxSize = vec3(20.f, 1.f, 20.f); |
---|
42 | m_physics.SetCollisionChannel(0, 0xFF); |
---|
43 | m_physics.SetShapeToBox(BoxSize); |
---|
44 | m_physics.SetMass(.0f); |
---|
45 | m_physics.SetTransform(base_location, base_rotation); |
---|
46 | m_physics.InitBodyToRigid(true); |
---|
47 | m_physics.AddToSimulation(new_sim); |
---|
48 | } |
---|
49 | |
---|
50 | PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) |
---|
51 | : m_ready(false), m_should_render(true) |
---|
52 | { |
---|
53 | Array<char const *> MeshRand; |
---|
54 |
|
---|
55 | MeshRand << "[sc#add afcb2 2 2 -.1]";
|
---|
56 | MeshRand << "[sc#dad afcb2 2 2 -.1]";
|
---|
57 | MeshRand << "[sc#dda afcb2 2 2 -.1]";
|
---|
58 | MeshRand << "[sc#daa afcb2 2 2 -.1]";
|
---|
59 | MeshRand << "[sc#ada afcb2 2 2 -.1]";
|
---|
60 | MeshRand << "[sc#aad afcb2 2 2 -.1]"; |
---|
61 | |
---|
62 | int SphereLimit = MeshRand.Count(); |
---|
63 | |
---|
64 | MeshRand << "[sc#add asph15 2 2 2]";
|
---|
65 | MeshRand << "[sc#dad asph15 2 2 2]";
|
---|
66 | MeshRand << "[sc#dda asph15 2 2 2]";
|
---|
67 | MeshRand << "[sc#daa asph15 2 2 2]";
|
---|
68 | MeshRand << "[sc#ada asph15 2 2 2]";
|
---|
69 | MeshRand << "[sc#aad asph15 2 2 2]"; |
---|
70 | |
---|
71 | int ConeLimit = MeshRand.Count(); |
---|
72 | |
---|
73 | MeshRand << "[sc#add scb#add ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]";
|
---|
74 | MeshRand << "[sc#dad scb#dad ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]";
|
---|
75 | MeshRand << "[sc#dda scb#dda ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]";
|
---|
76 | MeshRand << "[sc#daa scb#daa ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]";
|
---|
77 | MeshRand << "[sc#ada scb#ada ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]";
|
---|
78 | MeshRand << "[sc#aad scb#aad ad48 2 0 rx180 ty-1 ac48 2 2 0 0 0]"; |
---|
79 | |
---|
80 | int CylLimit = MeshRand.Count(); |
---|
81 | |
---|
82 | MeshRand << "[sc#add scb#add ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]";
|
---|
83 | MeshRand << "[sc#dad scb#dad ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]";
|
---|
84 | MeshRand << "[sc#dda scb#dda ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]";
|
---|
85 | MeshRand << "[sc#daa scb#daa ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]";
|
---|
86 | MeshRand << "[sc#ada scb#ada ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]";
|
---|
87 | MeshRand << "[sc#aad scb#aad ad96 2 0 rx180 ty-1 my ac96 2 2 2 0 0]"; |
---|
88 | |
---|
89 | int CapsLimit = MeshRand.Count(); |
---|
90 | |
---|
91 | MeshRand << "[sc#add scb#add acap21 2 1]";
|
---|
92 | MeshRand << "[sc#dad scb#dad acap21 2 1]";
|
---|
93 | MeshRand << "[sc#dda scb#dda acap21 2 1]";
|
---|
94 | MeshRand << "[sc#daa scb#daa acap21 2 1]";
|
---|
95 | MeshRand << "[sc#ada scb#ada acap21 2 1]";
|
---|
96 | MeshRand << "[sc#aad scb#aad acap21 2 1]"; |
---|
97 | |
---|
98 | switch (RandValue) |
---|
99 | { |
---|
100 | case 0: |
---|
101 | { |
---|
102 | RandValue = (int)(lol::RandF() * (SphereLimit - 1)); |
---|
103 | break; |
---|
104 | } |
---|
105 | case 1: |
---|
106 | { |
---|
107 | RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1)); |
---|
108 | break; |
---|
109 | } |
---|
110 | case 2: |
---|
111 | { |
---|
112 | RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1)); |
---|
113 | break; |
---|
114 | } |
---|
115 | case 3: |
---|
116 | { |
---|
117 | RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1)); |
---|
118 | break; |
---|
119 | } |
---|
120 | case 4: |
---|
121 | { |
---|
122 | RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1)); |
---|
123 | break; |
---|
124 | } |
---|
125 | default: |
---|
126 | { |
---|
127 | RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1)); |
---|
128 | } |
---|
129 | } |
---|
130 | |
---|
131 | m_mesh.Compile(MeshRand[RandValue]); |
---|
132 | vec3 BoxSize = vec3(2.0f); |
---|
133 | int ColGroup = 1; |
---|
134 | if (RandValue < SphereLimit) |
---|
135 | { |
---|
136 | m_physics.SetShapeToBox(BoxSize); |
---|
137 | ColGroup += 0; |
---|
138 | } |
---|
139 | else if (RandValue < ConeLimit) |
---|
140 | { |
---|
141 | m_physics.SetShapeToSphere(BoxSize.x * 2.f); |
---|
142 | ColGroup += 1; |
---|
143 | } |
---|
144 | else if (RandValue < CylLimit) |
---|
145 | { |
---|
146 | m_physics.SetShapeToCone(BoxSize.x, BoxSize.y); |
---|
147 | ColGroup += 2; |
---|
148 | } |
---|
149 | else if (RandValue < CapsLimit) |
---|
150 | { |
---|
151 | m_physics.SetShapeToCylinder(BoxSize); |
---|
152 | ColGroup += 3; |
---|
153 | } |
---|
154 | else |
---|
155 | { |
---|
156 | m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y); |
---|
157 | ColGroup += 4; |
---|
158 | } |
---|
159 | |
---|
160 | m_physics.SetCollisionChannel(0, 0xFF); |
---|
161 | //m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1)); |
---|
162 | m_physics.SetMass(base_mass); |
---|
163 | m_physics.SetTransform(base_location); |
---|
164 | m_physics.InitBodyToRigid(); |
---|
165 | m_physics.AddToSimulation(new_sim); |
---|
166 | } |
---|
167 | |
---|
168 | void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) |
---|
169 | { |
---|
170 | m_physics.SetTransform(base_location, base_rotation); |
---|
171 | } |
---|
172 | |
---|
173 | lol::mat4 GetTransform() |
---|
174 | { |
---|
175 | return m_physics.GetTransform(); |
---|
176 | } |
---|
177 | |
---|
178 | void SetRender(bool should_render) |
---|
179 | { |
---|
180 | m_should_render = should_render; |
---|
181 | } |
---|
182 | |
---|
183 | EasyMesh *GetMesh() { return &m_mesh; } |
---|
184 | EasyPhysic *GetPhysic() { return &m_physics; } |
---|
185 | |
---|
186 | ~PhysicsObject() |
---|
187 | { |
---|
188 | } |
---|
189 | |
---|
190 | char const *GetName() { return "<PhysicsObject>"; } |
---|
191 | |
---|
192 | protected: |
---|
193 | virtual void TickGame(float seconds) |
---|
194 | { |
---|
195 | WorldEntity::TickGame(seconds); |
---|
196 | } |
---|
197 | |
---|
198 | virtual void TickDraw(float seconds) |
---|
199 | { |
---|
200 | WorldEntity::TickDraw(seconds); |
---|
201 | |
---|
202 | if (!m_ready) |
---|
203 | { |
---|
204 | m_mesh.MeshConvert(); |
---|
205 | m_ready = true; |
---|
206 | } |
---|
207 | |
---|
208 | if (m_should_render) |
---|
209 | m_mesh.Render(m_physics.GetTransform()); |
---|
210 | } |
---|
211 | |
---|
212 | private: |
---|
213 | //Base datas |
---|
214 | EasyMesh m_mesh; |
---|
215 | EasyPhysic m_physics; |
---|
216 | |
---|
217 | bool m_ready; |
---|
218 | bool m_should_render; |
---|
219 | }; |
---|
220 | |
---|
221 | #endif /* __PHYSICOBJECT_H__ */ |
---|
222 | |
---|