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