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), m_is_character(false) |
---|
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), m_is_character(false) |
---|
39 | { |
---|
40 | if (dummy == 1) //for Rope purpose |
---|
41 | { |
---|
42 | m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]"); |
---|
43 | //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]"); |
---|
44 | |
---|
45 | vec3 BoxSize = vec3(20.f, 1.f, 20.f); |
---|
46 | m_physics.SetCollisionChannel(0, 0xFF); |
---|
47 | m_physics.SetShapeToBox(BoxSize); |
---|
48 | m_physics.SetMass(.0f); |
---|
49 | m_physics.SetTransform(base_location, base_rotation); |
---|
50 | m_physics.InitBodyToRigid(true); |
---|
51 | m_physics.AddToSimulation(new_sim); |
---|
52 | } |
---|
53 | else if (dummy == 2) //for character purpose |
---|
54 | { |
---|
55 | m_is_character = true; |
---|
56 | m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]"); |
---|
57 | //m_mesh.Compile("[sc#fff scb#fff ac1 2 2 2 0 0]");
|
---|
58 | vec3 BoxSize = vec3(2.f, 2.f, 2.f); |
---|
59 | m_character.SetCollisionChannel(0, 0xFF); |
---|
60 | m_character.SetShapeToCapsule(BoxSize.x, BoxSize.y); |
---|
61 | m_character.SetMass(.0f); |
---|
62 | m_character.SetTransform(base_location, base_rotation); |
---|
63 | m_character.InitBodyToGhost(); |
---|
64 | m_character.AddToSimulation(new_sim); |
---|
65 | } |
---|
66 | } |
---|
67 | |
---|
68 | PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1) |
---|
69 | : m_ready(false), m_should_render(true), m_is_character(false) |
---|
70 | { |
---|
71 | Array<char const *> MeshRand; |
---|
72 |
|
---|
73 | MeshRand << "[sc#add afcb2 2 2 -.1]";
|
---|
74 | MeshRand << "[sc#dad afcb2 2 2 -.1]";
|
---|
75 | MeshRand << "[sc#dda afcb2 2 2 -.1]";
|
---|
76 | MeshRand << "[sc#daa afcb2 2 2 -.1]";
|
---|
77 | MeshRand << "[sc#ada afcb2 2 2 -.1]";
|
---|
78 | MeshRand << "[sc#aad afcb2 2 2 -.1]"; |
---|
79 | |
---|
80 | int SphereLimit = MeshRand.Count(); |
---|
81 | |
---|
82 | MeshRand << "[sc#add asph1 2 2 2]";
|
---|
83 | MeshRand << "[sc#dad asph1 2 2 2]";
|
---|
84 | MeshRand << "[sc#dda asph1 2 2 2]";
|
---|
85 | MeshRand << "[sc#daa asph1 2 2 2]";
|
---|
86 | MeshRand << "[sc#ada asph1 2 2 2]";
|
---|
87 | MeshRand << "[sc#aad asph1 2 2 2]"; |
---|
88 | |
---|
89 | int ConeLimit = MeshRand.Count(); |
---|
90 | |
---|
91 | MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
|
---|
92 | MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
|
---|
93 | MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
|
---|
94 | MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
|
---|
95 | MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]";
|
---|
96 | MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 ac1 2 2 0 0 0]"; |
---|
97 | |
---|
98 | int CylLimit = MeshRand.Count(); |
---|
99 | |
---|
100 | MeshRand << "[sc#add scb#add ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
|
---|
101 | MeshRand << "[sc#dad scb#dad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
|
---|
102 | MeshRand << "[sc#dda scb#dda ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
|
---|
103 | MeshRand << "[sc#daa scb#daa ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
|
---|
104 | MeshRand << "[sc#ada scb#ada ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]";
|
---|
105 | MeshRand << "[sc#aad scb#aad ad1 2 0 rx180 ty-1 my ac1 2 2 2 0 0]"; |
---|
106 | |
---|
107 | int CapsLimit = MeshRand.Count(); |
---|
108 | |
---|
109 | MeshRand << "[sc#add scb#add acap1 2 1]";
|
---|
110 | MeshRand << "[sc#dad scb#dad acap1 2 1]";
|
---|
111 | MeshRand << "[sc#dda scb#dda acap1 2 1]";
|
---|
112 | MeshRand << "[sc#daa scb#daa acap1 2 1]";
|
---|
113 | MeshRand << "[sc#ada scb#ada acap1 2 1]";
|
---|
114 | MeshRand << "[sc#aad scb#aad acap1 2 1]"; |
---|
115 | |
---|
116 | switch (RandValue) |
---|
117 | { |
---|
118 | case 0: |
---|
119 | { |
---|
120 | RandValue = (int)(lol::RandF() * (SphereLimit - 1)); |
---|
121 | break; |
---|
122 | } |
---|
123 | case 1: |
---|
124 | { |
---|
125 | RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1)); |
---|
126 | break; |
---|
127 | } |
---|
128 | case 2: |
---|
129 | { |
---|
130 | RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1)); |
---|
131 | break; |
---|
132 | } |
---|
133 | case 3: |
---|
134 | { |
---|
135 | RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1)); |
---|
136 | break; |
---|
137 | } |
---|
138 | case 4: |
---|
139 | { |
---|
140 | RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1)); |
---|
141 | break; |
---|
142 | } |
---|
143 | default: |
---|
144 | { |
---|
145 | RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1)); |
---|
146 | } |
---|
147 | } |
---|
148 | |
---|
149 | m_mesh.Compile(MeshRand[RandValue]); |
---|
150 | vec3 BoxSize = vec3(2.0f); |
---|
151 | int ColGroup = 1; |
---|
152 | if (RandValue < SphereLimit) |
---|
153 | { |
---|
154 | m_physics.SetShapeToBox(BoxSize); |
---|
155 | ColGroup += 0; |
---|
156 | } |
---|
157 | else if (RandValue < ConeLimit) |
---|
158 | { |
---|
159 | m_physics.SetShapeToSphere(BoxSize.x * 2.f); |
---|
160 | ColGroup += 1; |
---|
161 | } |
---|
162 | else if (RandValue < CylLimit) |
---|
163 | { |
---|
164 | m_physics.SetShapeToCone(BoxSize.x, BoxSize.y); |
---|
165 | ColGroup += 2; |
---|
166 | } |
---|
167 | else if (RandValue < CapsLimit) |
---|
168 | { |
---|
169 | m_physics.SetShapeToCylinder(BoxSize); |
---|
170 | ColGroup += 3; |
---|
171 | } |
---|
172 | else |
---|
173 | { |
---|
174 | m_physics.SetShapeToCapsule(BoxSize.x, BoxSize.y); |
---|
175 | ColGroup += 4; |
---|
176 | } |
---|
177 | |
---|
178 | m_physics.SetCollisionChannel(0, 0xFF); |
---|
179 | //m_physics.SetCollisionChannel(ColGroup, (1<<ColGroup)|(1)); |
---|
180 | m_physics.SetMass(base_mass); |
---|
181 | m_physics.SetTransform(base_location); |
---|
182 | m_physics.InitBodyToRigid(); |
---|
183 | m_physics.AddToSimulation(new_sim); |
---|
184 | } |
---|
185 | |
---|
186 | void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f))) |
---|
187 | { |
---|
188 | if (m_is_character) |
---|
189 | m_character.SetTransform(base_location, base_rotation); |
---|
190 | else |
---|
191 | m_physics.SetTransform(base_location, base_rotation); |
---|
192 | } |
---|
193 | |
---|
194 | lol::mat4 GetTransform() |
---|
195 | { |
---|
196 | if (m_is_character) |
---|
197 | return m_character.GetTransform(); |
---|
198 | else |
---|
199 | return m_physics.GetTransform(); |
---|
200 | } |
---|
201 | |
---|
202 | void SetRender(bool should_render) |
---|
203 | { |
---|
204 | m_should_render = should_render; |
---|
205 | } |
---|
206 | |
---|
207 | EasyMesh *GetMesh() { return &m_mesh; } |
---|
208 | EasyPhysic *GetPhysic() { return &m_physics; } |
---|
209 | EasyPhysic *GetCharacter() { return &m_character; } |
---|
210 | |
---|
211 | ~PhysicsObject() |
---|
212 | { |
---|
213 | } |
---|
214 | |
---|
215 | char const *GetName() { return "<PhysicsObject>"; } |
---|
216 | |
---|
217 | protected: |
---|
218 | virtual void TickGame(float seconds) |
---|
219 | { |
---|
220 | WorldEntity::TickGame(seconds); |
---|
221 | } |
---|
222 | |
---|
223 | virtual void TickDraw(float seconds) |
---|
224 | { |
---|
225 | WorldEntity::TickDraw(seconds); |
---|
226 | |
---|
227 | if (!m_ready) |
---|
228 | { |
---|
229 | m_mesh.MeshConvert(); |
---|
230 | m_ready = true; |
---|
231 | } |
---|
232 | |
---|
233 | if (m_should_render) |
---|
234 | { |
---|
235 | if (m_is_character) |
---|
236 | m_mesh.Render(m_character.GetTransform()); |
---|
237 | else |
---|
238 | m_mesh.Render(m_physics.GetTransform()); |
---|
239 | } |
---|
240 | } |
---|
241 | |
---|
242 | private: |
---|
243 | //Base datas |
---|
244 | EasyMesh m_mesh; |
---|
245 | EasyPhysic m_physics; |
---|
246 | EasyCharacterController m_character; |
---|
247 | |
---|
248 | bool m_ready; |
---|
249 | bool m_should_render; |
---|
250 | bool m_is_character; |
---|
251 | }; |
---|
252 | |
---|
253 | #endif /* __PHYSICOBJECT_H__ */ |
---|
254 | |
---|