source: trunk/test/physicobject.h @ 2816

Last change on this file since 2816 was 2816, checked in by lolbot, 6 years ago

fixed 542 files out of 2754:

  • removed 0 CR characters
  • removed 0 trailing whitespaces
  • replaced 0 tabs with spaces
  • fixed 542 svn:eol-style properties
  • Property svn:eol-style set to LF
File size: 9.6 KB
Line 
1//
2// Orbital
3//
4// Copyright: (c) 2009-2013 Cédric Lecacheur <jordx@free.fr>
5//            (c) 2009-2013 Benjamin "Touky" 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#include "physics/easycharactercontroller.h"
18#include "physics/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 afcb 60 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 afcb 20 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 ty1]"
67                "[ac32 2 .5 .5 0 0]"
68                "[asph6 .1 ty.9 tx.5 tz.15]"
69                "[asph6 .1 ty.9 tx.5 tz-.15]"
70                "[asph8 .05 sy10 ty.6 tz.5]"
71                "[asph8 .05 sy10 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 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 afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
114        MeshRand << "[sc#dad afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
115        MeshRand << "[sc#dda afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
116        MeshRand << "[sc#daa afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
117        MeshRand << "[sc#ada afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
118        MeshRand << "[sc#aad afcb1.7 1.7 1.7 0.4][sc#000 tsw afcb1.9 1.9 1.9 0.4 sx-1 sy-1 sz-1]";
119
120        int SphereLimit = MeshRand.Count();
121
122        MeshRand << "[sc#add asph1 2]";
123        MeshRand << "[sc#dad asph1 2]";
124        MeshRand << "[sc#dda asph1 2]";
125        MeshRand << "[sc#daa asph1 2]";
126        MeshRand << "[sc#ada asph1 2]";
127        MeshRand << "[sc#aad asph1 2]";
128
129        int ConeLimit = MeshRand.Count();
130
131        MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
132        MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
133        MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
134        MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
135        MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
136        MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 ac8 2 2 0 0 0]";
137
138        int CylLimit = MeshRand.Count();
139
140        MeshRand << "[sc#add scb#add ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
141        MeshRand << "[sc#dad scb#dad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
142        MeshRand << "[sc#dda scb#dda ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
143        MeshRand << "[sc#daa scb#daa ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
144        MeshRand << "[sc#ada scb#ada ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
145        MeshRand << "[sc#aad scb#aad ad8 2 0 rx180 ty-1 my ac8 2 2 2 0 0]";
146
147        int CapsLimit = MeshRand.Count();
148
149        MeshRand << "[sc#add scb#add acap1 2 1]";
150        MeshRand << "[sc#dad scb#dad acap1 2 1]";
151        MeshRand << "[sc#dda scb#dda acap1 2 1]";
152        MeshRand << "[sc#daa scb#daa acap1 2 1]";
153        MeshRand << "[sc#ada scb#ada acap1 2 1]";
154        MeshRand << "[sc#aad scb#aad acap1 2 1]";
155
156        switch (RandValue)
157        {
158            case 0:
159            {
160                RandValue = rand(SphereLimit);
161                break;
162            }
163            case 1:
164            {
165                RandValue = rand(SphereLimit, ConeLimit);
166                break;
167            }
168            case 2:
169            {
170                RandValue = rand(ConeLimit, CylLimit);
171                break;
172            }
173            case 3:
174            {
175                RandValue = rand(CylLimit, CapsLimit);
176                break;
177            }
178            case 4:
179            {
180                RandValue = rand(CapsLimit, MeshRand.Count());
181                break;
182            }
183            default:
184            {
185                RandValue = rand(MeshRand.Count());
186            }
187        }
188
189        m_physics = new EasyPhysic(this);
190
191        m_mesh.Compile(MeshRand[RandValue]);
192        vec3 BoxSize = vec3(2.0f);
193        int ColGroup = 1;
194        if (RandValue < SphereLimit)
195        {
196            m_physics->SetShapeToBox(BoxSize);
197            ColGroup += 0;
198        }
199        else if (RandValue < ConeLimit)
200        {
201            m_physics->SetShapeToSphere(BoxSize.x * 2.f);
202            ColGroup += 1;
203        }
204        else if (RandValue < CylLimit)
205        {
206            m_physics->SetShapeToCone(BoxSize.x, BoxSize.y);
207            ColGroup += 2;
208        }
209        else if (RandValue < CapsLimit)
210        {
211            m_physics->SetShapeToCylinder(BoxSize);
212            ColGroup += 3;
213        }
214        else
215        {
216            m_physics->SetShapeToCapsule(BoxSize.x, BoxSize.y);
217            ColGroup += 4;
218        }
219
220        m_physics->SetCollisionChannel(0, 0xFF);
221        //m_physics->SetCollisionChannel(ColGroup, (1 << ColGroup)|(1));
222        m_physics->SetMass(base_mass);
223        m_physics->SetTransform(base_location);
224        m_physics->InitBodyToRigid();
225        m_physics->AddToSimulation(new_sim);
226    }
227
228    void SetTransform(const lol::vec3& base_location, const lol::quat& base_rotation=lol::quat(lol::mat4(1.0f)))
229    {
230        if (m_is_character)
231            m_character->SetTransform(base_location, base_rotation);
232        else
233            m_physics->SetTransform(base_location, base_rotation);
234    }
235
236    lol::mat4 GetTransform()
237    {
238        if (m_is_character)
239            return m_character->GetTransform();
240        else
241            return m_physics->GetTransform();
242    }
243
244    void SetRender(bool should_render)
245    {
246        m_should_render = should_render;
247    }
248
249    EasyMesh *GetMesh() { return &m_mesh; }
250    EasyPhysic *GetPhysic() { return m_physics; }
251    EasyCharacterController *GetCharacter() { return m_character; }
252
253    ~PhysicsObject()
254    {
255    }
256
257    char const *GetName() { return "<PhysicsObject>"; }
258
259protected:
260    virtual void TickGame(float seconds)
261    {
262        WorldEntity::TickGame(seconds);
263    }
264
265    virtual void TickDraw(float seconds)
266    {
267        WorldEntity::TickDraw(seconds);
268
269        if (!m_ready)
270        {
271            m_mesh.MeshConvert();
272            m_ready = true;
273        }
274
275        if (m_should_render)
276        {
277            if (m_is_character)
278                m_mesh.Render(m_character->GetTransform());
279            else
280                m_mesh.Render(m_physics->GetTransform());
281        }
282    }
283
284private:
285    //Base datas
286    EasyMesh                    m_mesh;
287    EasyPhysic*                    m_physics;
288    EasyCharacterController*    m_character;
289
290    bool                        m_ready;
291    bool                        m_should_render;
292    bool                        m_is_character;
293};
294
295#endif /* __PHYSICOBJECT_H__ */
296
Note: See TracBrowser for help on using the repository browser.