source: trunk/test/PhysicObject.h @ 1912

Last change on this file since 1912 was 1912, checked in by sam, 11 years ago

test: fix mesh settings in the physics demo.

File size: 10.1 KB
Line 
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/Include/EasyPhysics.h"
17#include "Physics/Include/EasyCharacterController.h"
18#include "Physics/Include/EasyConstraint.h"
19
20using namespace lol;
21using namespace lol::phys;
22
23class PhysicsObject : public WorldEntity
24{
25public:
26    PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation)
27        : m_ready(false), m_should_render(true), m_is_character(false)
28    {
29        m_physics = new EasyPhysic(this);
30
31        m_mesh.Compile("[sc#ddd afcb60 1 60 -.1]");
32        vec3 BoxSize = vec3(60.f, 1.f, 60.f);
33        m_physics->SetCollisionChannel(0, 0xFF);
34        m_physics->SetShapeToBox(BoxSize);
35        m_physics->SetMass(.0f);
36        m_physics->SetTransform(base_location, base_rotation);
37        m_physics->InitBodyToRigid(true);
38        m_physics->AddToSimulation(new_sim);
39    }
40
41    PhysicsObject(Simulation* new_sim, const vec3 &base_location, const quat &base_rotation, int dummy)
42        : m_ready(false), m_should_render(true), m_is_character(false)
43    {
44        if (dummy == 1) //for platform purpose
45        {
46            m_physics = new EasyPhysic(this);
47
48            m_mesh.Compile("[sc#ddd afcb20 1 20 -.1]");
49            vec3 BoxSize = vec3(20.f, 1.f, 20.f);
50            m_physics->SetCollisionChannel(0, 0xFF);
51            m_physics->SetShapeToBox(BoxSize);
52            m_physics->SetMass(.0f);
53            m_physics->SetTransform(base_location, base_rotation);
54            m_physics->InitBodyToRigid(true);
55            m_physics->AddToSimulation(new_sim);
56        }
57        else if (dummy == 2) //for character purpose
58        {
59            m_character = new EasyCharacterController(this);
60            m_is_character = true;
61            //m_mesh.Compile("[sc#f00 afcb10 10 10 -.1]");
62            m_mesh.Compile(
63                "[sc#000 scb#000"
64                //"[sc#aaa scb#aaa"
65                "[ad8 2 0 rx180 ty-1]"
66                "[asph8 .5 .5 .5 ty1]"
67                "[ac32 2 .5 .5 0 0]"
68                "[asph6 .1 .1 .1 ty.9 tx.5 tz.15]"
69                "[asph6 .1 .1 .1 ty.9 tx.5 tz-.15]"
70                "[asph8 .05 .5 .05 ty.6 tz.5]"
71                "[asph8 .05 .5 .05 ty.6 tz-.5]"
72                "]"
73                "[sc#fd0 scb#fd0"
74                "[ac8 .4 .1 0 0 0 ty.25 rz-90 ty.7 tx.5]"
75                "]"
76                "["
77                "[sc#fff scb#fff"
78                "[ad8 2 0 rx180 ty-1]"
79                "[asph8 .5 .5 .5 ty1]"
80                "[ac32 1.9 .5 .5 0 0]"
81                "]"
82                " ty-.1 tx.05]"
83                );
84            vec3 BoxSize = vec3(1.f, 2.f, 1.f);
85            m_character->SetCollisionChannel(0, 0xFF);
86            m_character->SetShapeToCapsule(BoxSize.x, BoxSize.y);
87            m_character->SetMass(.0f);
88            //m_character->SetStepHeight(1.f);
89            m_character->SetTransform(base_location, base_rotation);
90            m_character->InitBodyToGhost();
91            m_character->AddToSimulation(new_sim);
92        }
93        else if (dummy == 3) //for Stairs purpose
94        {
95            m_physics = new EasyPhysic(this);
96
97            m_mesh.Compile("[sc#aae afcb4 .25 4 -.01]");
98            vec3 BoxSize = vec3(4.f, .25f, 4.f);
99            m_physics->SetCollisionChannel(0, 0xFF);
100            m_physics->SetShapeToBox(BoxSize);
101            m_physics->SetMass(.0f);
102            m_physics->SetTransform(base_location, base_rotation);
103            m_physics->InitBodyToRigid(true);
104            m_physics->AddToSimulation(new_sim);
105        }
106    }
107
108    PhysicsObject(Simulation* new_sim, float base_mass, const vec3 &base_location, int RandValue = -1)
109        : m_ready(false), m_should_render(true), m_is_character(false)
110    {
111        Array<char const *> MeshRand;
112
113        //MeshRand << "[sc#add afcb2 2 2 -.1]";
114        //MeshRand << "[sc#dad afcb2 2 2 -.1]";
115        //MeshRand << "[sc#dda afcb2 2 2 -.1]";
116        //MeshRand << "[sc#daa afcb2 2 2 -.1]";
117        //MeshRand << "[sc#ada afcb2 2 2 -.1]";
118        //MeshRand << "[sc#aad afcb2 2 2 -.1]";
119        MeshRand << "[sc#add afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
120        MeshRand << "[sc#dad afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
121        MeshRand << "[sc#dda afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
122        MeshRand << "[sc#daa afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
123        MeshRand << "[sc#ada afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
124        MeshRand << "[sc#aad afcb1.7 1.7 1.7 0][sc#000 afcb1.9 1.9 1.9 0 sx-1 sy-1 sz-1]";
125
126        int SphereLimit = MeshRand.Count();
127
128        MeshRand << "[sc#add asph1 2 2 2]";
129        MeshRand << "[sc#dad asph1 2 2 2]";
130        MeshRand << "[sc#dda asph1 2 2 2]";
131        MeshRand << "[sc#daa asph1 2 2 2]";
132        MeshRand << "[sc#ada asph1 2 2 2]";
133        MeshRand << "[sc#aad asph1 2 2 2]";
134
135        int ConeLimit = MeshRand.Count();
136
137        MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
138        MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
139        MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
140        MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
141        MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
142        MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
143
144        int CylLimit = MeshRand.Count();
145
146        MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
147        MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
148        MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
149        MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
150        MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
151        MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
152
153        int CapsLimit = MeshRand.Count();
154
155        MeshRand << "[sc#add scb#add acap1 2 1]";
156        MeshRand << "[sc#dad scb#dad acap1 2 1]";
157        MeshRand << "[sc#dda scb#dda acap1 2 1]";
158        MeshRand << "[sc#daa scb#daa acap1 2 1]";
159        MeshRand << "[sc#ada scb#ada acap1 2 1]";
160        MeshRand << "[sc#aad scb#aad acap1 2 1]";
161
162        switch (RandValue)
163        {
164            case 0:
165            {
166                RandValue = (int)(lol::RandF() * (SphereLimit - 1));
167                break;
168            }
169            case 1:
170            {
171                RandValue = SphereLimit + (int)(lol::RandF() * ((ConeLimit - SphereLimit) - 1));
172                break;
173            }
174            case 2:
175            {
176                RandValue = ConeLimit + (int)(lol::RandF() * ((CylLimit - ConeLimit) - 1));
177                break;
178            }
179            case 3:
180            {
181                RandValue = CylLimit + (int)(lol::RandF() * ((CapsLimit - CylLimit) - 1));
182                break;
183            }
184            case 4:
185            {
186                RandValue = CapsLimit + (int)(lol::RandF() * ((MeshRand.Count() - CapsLimit) - 1));
187                break;
188            }
189            default:
190            {
191                RandValue = (int)(lol::RandF() * (MeshRand.Count() - 1));
192            }
193        }
194
195        m_physics = new EasyPhysic(this);
196
197        m_mesh.Compile(MeshRand[RandValue]);
198        vec3 BoxSize = vec3(2.0f);
199        int ColGroup = 1;
200        if (RandValue < SphereLimit)
201        {
202            m_physics->SetShapeToBox(BoxSize);
203            ColGroup += 0;
204        }
205        else if (RandValue < ConeLimit)
206        {
207            m_physics->SetShapeToSphere(BoxSize.x * 2.f);
208            ColGroup += 1;
209        }
210        else if (RandValue < CylLimit)
211        {
212            m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
213            ColGroup += 2;
214        }
215        else if (RandValue < CapsLimit)
216        {
217            m_physics->SetShapeToCylinder(BoxSize);
218            ColGroup += 3;
219        }
220        else
221        {
222            m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
223            ColGroup += 4;
224        }
225
226        m_physics->SetCollisionChannel(0, 0xFF);
227        //m_physics->SetCollisionChannel(ColGroup, (1<<ColGroup)|(1));
228        m_physics->SetMass(base_mass);
229        m_physics->SetTransform(base_location);
230        m_physics->InitBodyToRigid();
231        m_physics->AddToSimulation(new_sim);
232    }
233
234    void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
235    {
236        if (m_is_character)
237            m_character->SetTransform(base_location, base_rotation);
238        else
239            m_physics->SetTransform(base_location, base_rotation);
240    }
241
242    lol::mat4 GetTransform()
243    {
244        if (m_is_character)
245            return m_character->GetTransform();
246        else
247            return m_physics->GetTransform();
248    }
249
250    void SetRender(bool should_render)
251    {
252        m_should_render = should_render;
253    }
254
255    EasyMesh *GetMesh() { return &m_mesh; }
256    EasyPhysic *GetPhysic() { return m_physics; }
257    EasyCharacterController *GetCharacter() { return m_character; }
258
259    ~PhysicsObject()
260    {
261    }
262
263    char const *GetName() { return "<PhysicsObject>"; }
264
265protected:
266    virtual void TickGame(float seconds)
267    {
268        WorldEntity::TickGame(seconds);
269    }
270
271    virtual void TickDraw(float seconds)
272    {
273        WorldEntity::TickDraw(seconds);
274
275        if (!m_ready)
276        {
277            m_mesh.MeshConvert();
278            m_ready = true;
279        }
280
281        if (m_should_render)
282        {
283            if (m_is_character)
284                m_mesh.Render(m_character->GetTransform());
285            else
286                m_mesh.Render(m_physics->GetTransform());
287        }
288    }
289
290private:
291    //Base datas
292    EasyMesh                    m_mesh;
293    EasyPhysic*                    m_physics;
294    EasyCharacterController*    m_character;
295
296    bool                        m_ready;
297    bool                        m_should_render;
298    bool                        m_is_character;
299};
300
301#endif /* __PHYSICOBJECT_H__ */
302
Note: See TracBrowser for help on using the repository browser.