source: trunk/test/BtPhysTest.cpp @ 1888

Last change on this file since 1888 was 1888, checked in by lolbot, 8 years ago

fixed 23 files out of 277:

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