source: trunk/test/btphystest.cpp @ 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: 15.3 KB
Line 
1//
2// BtPhysTest
3//
4// Copyright: (c) 2009-2013 Benjamin "Touky" Huet <huet.benjamin@gmail.com>
5//            (c) 2012-2013 Sam Hocevar <sam@hocevar.net>
6//
7
8#if defined HAVE_CONFIG_H
9#   include "config.h"
10#endif
11
12#include "core.h"
13#include "loldebug.h"
14
15using namespace lol;
16
17#ifndef HAVE_PHYS_USE_BULLET
18#define HAVE_PHYS_USE_BULLET
19#endif /* HAVE_PHYS_USE_BULLET */
20
21#include "physics/lolphysics.h"
22#include "physics/easyphysics.h"
23#include "physicobject.h"
24#include "btphystest.h"
25
26using namespace lol::phys;
27
28#define CUBE_HALF_EXTENTS .5f
29#define EXTRA_HEIGHT 1.f
30
31int gNumObjects = 64;
32
33#define USE_WALL        1
34#define USE_PLATFORM    1
35#define USE_ROPE        0
36#define USE_BODIES        1
37#define USE_ROTATION    0
38#define USE_CHARACTER    1
39#define USE_STAIRS        1
40
41#define    IPT_MOVE_FORWARD        "Move_Forward"
42#define    IPT_MOVE_BACKWARD        "Move_Backward"
43#define    IPT_MOVE_LEFT            "Move_Left"
44#define    IPT_MOVE_RIGHT            "Move_Right"
45#define    IPT_MOVE_UP                "Move_Up"
46#define    IPT_MOVE_DOWN            "Move_Down"
47#define    IPT_MOVE_JUMP            "Move_Jump"
48
49BtPhysTest::BtPhysTest(bool editor)
50{
51    m_loop_value = .0f;
52
53    /* Create a camera that matches the settings of XNA BtPhysTest */
54    m_camera = new Camera();
55    m_camera->SetView(vec3(50.f, 50.f, 0.f),
56                      vec3(0.f, 0.f, 0.f),
57                      vec3(0, 1, 0));
58    m_camera->SetProjection(mat4::perspective(45.f, 1280.f, 960.f, .1f, 1000.f));
59    g_scene->PushCamera(m_camera);
60
61    m_ready = false;
62
63    m_simulation = new Simulation();
64    m_simulation->SetWorldLimit(vec3(-1000.0f, -1000.0f, -1000.0f), vec3(1000.0f, 1000.0f, 1000.0f));
65    m_simulation->Init();
66    vec3 NewGravity = vec3(.0f, -10.0f, .0f);
67    m_simulation->SetGravity(NewGravity);
68    m_simulation->SetContinuousDetection(true);
69    m_simulation->SetTimestep(1.f / 120.f);
70    Ticker::Ref(m_simulation);
71
72    /* Add a white directional light */
73    m_light1 = new Light();
74    m_light1->SetPosition(vec4(0.2f, 0.2f, 0.f, 0.f));
75    m_light1->SetColor(vec4(0.5f, 0.5f, 0.5f, 1.f));
76    Ticker::Ref(m_light1);
77
78    /* Add an orangeish point light */
79    m_light2 = new Light();
80    m_light2->SetPosition(vec4(-15.f, 15.f, 15.f, 1.f));
81    m_light2->SetColor(vec4(0.4f, 0.3f, 0.2f, 1.f));
82    Ticker::Ref(m_light2);
83
84    float offset = 29.5f;
85    vec3 pos_offset = vec3(.0f, 30.f, .0f);
86    if (USE_STAIRS)
87    {
88        vec3 new_offset = vec3(1.0f, .125f, .0f);
89        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
90        vec3 NewPosition = pos_offset + vec3(5.0f, -29.f, 15.0f);
91        {
92            NewRotation = quat::fromeuler_xyz(0.f, 0.f, 30.f);
93            NewPosition += vec3(4.0f, .0f, -4.0f);
94
95            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
96            Ticker::Ref(NewPhyobj);
97            m_stairs_list << NewPhyobj;
98        }
99        {
100            NewRotation = quat::fromeuler_xyz(0.f, 0.f, 40.f);
101            NewPosition += vec3(4.0f, .0f, -4.0f);
102
103            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
104            Ticker::Ref(NewPhyobj);
105            m_stairs_list << NewPhyobj;
106        }
107        NewPosition = pos_offset + vec3(5.0f, -29.5f, 15.0f);
108        NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
109        for (int i=0; i < 15; i++)
110        {
111            NewPosition += new_offset;
112
113            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 3);
114            Ticker::Ref(NewPhyobj);
115            m_stairs_list << NewPhyobj;
116        }
117    }
118
119    if (USE_WALL)
120    {
121        for (int i=0; i < 6; i++)
122        {
123            vec3 NewPosition = vec3(.0f);
124            quat NewRotation = quat(1.f);
125
126            PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation);
127
128            int idx = i/2;
129            NewPosition = pos_offset;
130            NewPosition[idx] += offset;
131            offset *= -1.f;
132
133            if (idx != 1)
134            {
135                vec3 NewAxis = vec3(.0f);
136                NewAxis[2 - idx] = 1;
137                NewRotation = quat::rotate(90.f, NewAxis);
138            }
139
140            NewPhyobj->SetTransform(NewPosition, NewRotation);
141            Ticker::Ref(NewPhyobj);
142            m_ground_list << NewPhyobj;
143        }
144    }
145
146    PhysicsObject* BasePhyobj = NULL;
147    if (USE_PLATFORM)
148    {
149        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
150        vec3 NewPosition = pos_offset + vec3(5.0f, -25.0f, -15.0f);
151
152        PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
153
154        m_platform_list << NewPhyobj;
155        Ticker::Ref(NewPhyobj);
156
157        NewPosition = pos_offset + vec3(-15.0f, -25.0f, 5.0f);
158
159        NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
160        BasePhyobj = NewPhyobj;
161
162        m_platform_list << NewPhyobj;
163        Ticker::Ref(NewPhyobj);
164
165        NewRotation = quat::fromeuler_xyz(0.f, 0.f, 90.f);
166        NewPosition = pos_offset + vec3(-20.0f, -25.0f, 5.0f);
167
168        NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
169
170        NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, true);
171        m_platform_list << NewPhyobj;
172        Ticker::Ref(NewPhyobj);
173
174        //NewPosition += vec3(-0.0f, .0f, .0f);
175        //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
176
177        //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), true, false);
178        //m_platform_list << NewPhyobj;
179        //Ticker::Ref(NewPhyobj);
180
181        //NewPosition += vec3(-2.0f, .0f, .0f);
182        //NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 1);
183
184        //NewPhyobj->GetPhysic()->AttachTo(BasePhyobj->GetPhysic(), false, false);
185        //m_platform_list << NewPhyobj;
186        //Ticker::Ref(NewPhyobj);
187    }
188
189    if (USE_CHARACTER)
190    {
191        quat NewRotation = quat::fromeuler_xyz(0.f, 0.f, 0.f);
192        vec3 NewPosition = pos_offset + vec3(-5.0f, -10.0f, 15.0f);
193
194        PhysicsObject* NewPhyobj = new PhysicsObject(m_simulation, NewPosition, NewRotation, 2);
195
196        m_character_list << NewPhyobj;
197        Ticker::Ref(NewPhyobj);
198
199
200        Input::LinkActionToKey(IPT_MOVE_FORWARD,        Key::Up);
201        Input::LinkActionToKey(IPT_MOVE_BACKWARD,        Key::Down);
202        Input::LinkActionToKey(IPT_MOVE_LEFT,            Key::Left);
203        Input::LinkActionToKey(IPT_MOVE_RIGHT,            Key::Right);
204        Input::LinkActionToKey(IPT_MOVE_JUMP,            Key::Space);
205        Input::LinkActionToKey(IPT_MOVE_UP,                Key::PageUp);
206        Input::LinkActionToKey(IPT_MOVE_DOWN,            Key::PageDown);
207
208        //NewPhyobj->GetCharacter()->AttachTo(BasePhyobj->GetPhysic(), true, true);
209    }
210
211    if (USE_BODIES)
212    {
213        for (int x=0; x < 6; x++)
214        {
215            for (int y=0; y < 6; y++)
216            {
217                for (int z=0; z < 5; z++)
218                {
219                    PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
220                        vec3(-20.f, 15.f, -20.f) +
221                        vec3(8.f * (float)x, 8.f * (float)y, 8.f * (float)z));
222                    m_physobj_list << new_physobj;
223                    Ticker::Ref(new_physobj);
224                }
225            }
226        }
227    }
228
229    if (USE_ROPE)
230    {
231        Array<PhysicsObject*> RopeElements;
232        for (int i = 0; i < 14; i++)
233        {
234            PhysicsObject* new_physobj = new PhysicsObject(m_simulation, 1000.f,
235                vec3(0.f, 15.f, -20.f) +
236                vec3(0.f, 0.f, 2.f * (float)i), 1);
237            RopeElements << new_physobj;
238            m_physobj_list << new_physobj;
239            Ticker::Ref(new_physobj);
240            if (RopeElements.Count() > 1)
241            {
242                EasyConstraint* new_constraint = new EasyConstraint();
243
244                vec3 A2B = .5f * (RopeElements[i]->GetPhysic()->GetTransform().v3.xyz -
245                            RopeElements[i - 1]->GetPhysic()->GetTransform().v3.xyz);
246                new_constraint->SetPhysObjA(RopeElements[i - 1]->GetPhysic(), lol::mat4::translate(A2B));
247                new_constraint->SetPhysObjB(RopeElements[i]->GetPhysic(), lol::mat4::translate(-A2B));
248                new_constraint->InitConstraintToPoint2Point();
249                new_constraint->DisableCollisionBetweenObjs(true);
250                new_constraint->AddToSimulation(m_simulation);
251                m_constraint_list << new_constraint;
252            }
253        }
254    }
255}
256
257void BtPhysTest::TickGame(float seconds)
258{
259    WorldEntity::TickGame(seconds);
260
261    if (Input::WasReleased(Key::Escape))
262        Ticker::Shutdown();
263
264    m_loop_value += seconds;
265    if (m_loop_value > F_PI * 2.0f)
266        m_loop_value -= F_PI * 2.0f;
267
268    vec3 GroundBarycenter = vec3(.0f);
269    vec3 PhysObjBarycenter = vec3(.0f);
270    float factor = .0f;
271
272    if (USE_WALL)
273    {
274        for (int i = 0; i < m_ground_list.Count(); i++)
275        {
276            PhysicsObject* PhysObj = m_ground_list[i];
277            mat4 GroundMat = PhysObj->GetTransform();
278
279            GroundBarycenter += GroundMat.v3.xyz;
280            factor += 1.f;
281        }
282
283        GroundBarycenter /= factor;
284
285        for (int i = 0; i < m_ground_list.Count(); i++)
286        {
287            PhysicsObject* PhysObj = m_ground_list[i];
288
289            mat4 GroundMat = PhysObj->GetTransform();
290            vec3 CenterToGround = GroundMat.v3.xyz - GroundBarycenter;
291            vec3 CenterToCam = m_camera->GetPosition() - GroundBarycenter;
292
293            if (dot(normalize(CenterToCam - CenterToGround),
294                    normalize(CenterToGround)) > 0.f)
295                PhysObj->SetRender(false);
296            else
297                PhysObj->SetRender(true);
298        }
299    }
300
301    if (USE_ROTATION)
302    {
303        for (int i = 0; i < m_ground_list.Count(); i++)
304        {
305            PhysicsObject* PhysObj = m_ground_list[i];
306
307            mat4 GroundMat = PhysObj->GetTransform();
308            mat4 CenterMx = mat4::translate(GroundBarycenter);
309            GroundMat = inverse(CenterMx) * GroundMat;
310            GroundMat = CenterMx *
311                        mat4(quat::fromeuler_xyz(vec3(.0f, 20.f, 20.0f) * seconds))
312                        * GroundMat;
313            PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
314        }
315    }
316
317    if (USE_PLATFORM)
318    {
319        for (int i = 0; i < m_platform_list.Count(); i++)
320        {
321            PhysicsObject* PhysObj = m_platform_list[i];
322
323            mat4 GroundMat = PhysObj->GetTransform();
324            if (i == 0)
325            {
326                GroundMat = GroundMat * mat4(quat::fromeuler_xyz(vec3(20.f, .0f, .0f) * seconds));
327                PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
328            }
329            else if (i == 1)
330            {
331                GroundMat =
332                    mat4::translate(vec3(-15.0f, 5.0f, lol::cos(m_loop_value) * 8.f)) *
333                    mat4(quat::fromeuler_xyz(vec3(.0f, lol::cos(m_loop_value) * 20.f, .0f)));
334                PhysObj->SetTransform(GroundMat.v3.xyz, quat(GroundMat));
335            }
336        }
337    }
338
339    if (USE_CHARACTER)
340    {
341        for (int i = 0; i < m_character_list.Count(); i++)
342        {
343            PhysicsObject* PhysObj = m_character_list[i];
344            EasyCharacterController* Character = (EasyCharacterController*)PhysObj->GetCharacter();
345            mat4 CtlrMx = Character->GetTransform();
346
347            int HMovement = Input::GetStatus(IPT_MOVE_RIGHT) - Input::GetStatus(IPT_MOVE_LEFT);
348            int VMovement = Input::GetStatus(IPT_MOVE_FORWARD) - Input::GetStatus(IPT_MOVE_BACKWARD);
349            int RMovement = Input::GetStatus(IPT_MOVE_UP) - Input::GetStatus(IPT_MOVE_DOWN);
350            vec3 CharMove = vec3((float)VMovement * seconds * 4.f, (float)RMovement * seconds * 10.f, (float)HMovement * seconds * 4.f);
351
352            if (Input::WasReleased(IPT_MOVE_JUMP))
353                Character->Jump();
354            Character->SetMovementForFrame(CharMove);
355
356            RayCastResult HitResult;
357            if (m_simulation->RayHits(HitResult, ERT_Closest, Character->GetTransform().v3.xyz, (Character->GetTransform().v3.xyz + vec3(.0f, -1.f, .0f)), Character))
358                Character->AttachTo(HitResult.m_collider_list[0], true, true);
359            else
360                Character->AttachTo(NULL);
361        }
362    }
363
364    if (USE_CHARACTER)
365    {
366        PhysObjBarycenter = vec3(.0f);
367        factor = .0f;
368
369        for (int i = 0; i < m_character_list.Count(); i++)
370        {
371            PhysicsObject* PhysObj = m_character_list[i];
372            mat4 GroundMat = PhysObj->GetTransform();
373
374            PhysObjBarycenter += GroundMat.v3.xyz;
375            factor += 1.f;
376        }
377
378        PhysObjBarycenter /= factor;
379
380#if 0
381        m_camera->SetTarget(m_camera->GetTarget() + (seconds / (seconds + 0.18f)) * (PhysObjBarycenter - m_camera->GetTarget()));
382        vec3 CamPosCenter = m_camera->GetTarget() + vec3(.0f, 5.0f, .0f);
383        m_camera->SetPosition(CamPosCenter + normalize(m_camera->GetPosition() - CamPosCenter) * 20.0f);
384#endif
385    }
386    else
387    {
388        PhysObjBarycenter = vec3(.0f);
389        for (int i = 0; i < m_physobj_list.Count(); i++)
390        {
391            PhysicsObject* PhysObj = m_physobj_list[i];
392            mat4 GroundMat = PhysObj->GetTransform();
393
394            PhysObjBarycenter += GroundMat.v3.xyz;
395            factor += 1.f;
396        }
397
398        PhysObjBarycenter /= factor;
399
400#if 0
401        m_camera->SetTarget(PhysObjBarycenter);
402        m_camera->SetPosition(GroundBarycenter + normalize(GroundBarycenter - PhysObjBarycenter) * 60.0f);
403#endif
404    }
405
406}
407
408void BtPhysTest::TickDraw(float seconds)
409{
410    WorldEntity::TickDraw(seconds);
411
412    if (!m_ready)
413    {
414        /* FIXME: this object never cleans up */
415        m_ready = true;
416    }
417
418    //Video::SetClearColor(vec4(0.0f, 0.0f, 0.12f, 1.0f));
419
420}
421
422BtPhysTest::~BtPhysTest()
423{
424    g_scene->PopCamera(m_camera);
425    Ticker::Unref(m_light1);
426    Ticker::Unref(m_light2);
427
428    while (m_constraint_list.Count())
429    {
430        EasyConstraint* CurPop = m_constraint_list.Last();
431        m_constraint_list.Pop();
432        CurPop->RemoveFromSimulation(m_simulation);
433        delete CurPop;
434    }
435    while (m_ground_list.Count())
436    {
437        PhysicsObject* CurPop = m_ground_list.Last();
438        m_ground_list.Pop();
439        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
440        Ticker::Unref(CurPop);
441    }
442    while (m_stairs_list.Count())
443    {
444        PhysicsObject* CurPop = m_stairs_list.Last();
445        m_stairs_list.Pop();
446        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
447        Ticker::Unref(CurPop);
448    }
449    while (m_character_list.Count())
450    {
451        PhysicsObject* CurPop = m_character_list.Last();
452        m_character_list.Pop();
453        CurPop->GetCharacter()->RemoveFromSimulation(m_simulation);
454        Ticker::Unref(CurPop);
455    }
456    while (m_platform_list.Count())
457    {
458        PhysicsObject* CurPop = m_platform_list.Last();
459        m_platform_list.Pop();
460        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
461        Ticker::Unref(CurPop);
462    }
463    while (m_physobj_list.Count())
464    {
465        PhysicsObject* CurPop = m_physobj_list.Last();
466        m_physobj_list.Pop();
467        CurPop->GetPhysic()->RemoveFromSimulation(m_simulation);
468        Ticker::Unref(CurPop);
469    }
470    Ticker::Unref(m_simulation);
471
472}
473
474int main(int argc, char **argv)
475{
476    System::Init(argc, argv);
477
478    Application app("BtPhysTest", ivec2(1280, 720), 60.0f);
479
480    new BtPhysTest(argc > 1);
481    app.ShowPointer(false);
482
483    app.Run();
484
485    return EXIT_SUCCESS;
486}
487
Note: See TracBrowser for help on using the repository browser.