summaryrefslogtreecommitdiff
path: root/legacy/ephysics/src/lib/ephysics_world.cpp
blob: 0136a07e5d3a39296845b3e9a5e4ae77b70ec44b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include "ephysics_private.h"
#include <Ecore.h>
#include <Evas.h>

#ifdef  __cplusplus
extern "C" {
#endif

typedef struct _EPhysics_World_Callback EPhysics_World_Callback;

struct _EPhysics_World_Callback {
     EINA_INLIST;
     void (*func) (void *data, EPhysics_World *world, void *event_info);
     void *data;
     EPhysics_Callback_Type type;
};

struct _EPhysics_World {
     btBroadphaseInterface* broadphase;
     btDefaultCollisionConfiguration* collision;
     btCollisionDispatcher* dispatcher;
     btSequentialImpulseConstraintSolver* solver;
     btDiscreteDynamicsWorld* dynamics_world;

     EPhysics_Body *boundaries[4];
     EPhysics_Camera *camera;
     Evas_Coord x, y, w, h;
     Eina_Inlist *callbacks;
     Eina_List *bodies;
     double last_update;
     double rate;
     Eina_Bool running:1;
     Eina_Bool active:1;
};

static int _ephysics_world_init_count = 0;
static int _worlds_running = 0;
static Eina_List *_worlds = NULL;
static Ecore_Animator *_anim_simulate = NULL;

static void
_ephysics_world_tick_cb(btDynamicsWorld *dynamics_world, btScalar timeStep)
{
   EPhysics_World *world;
   btCollisionObjectArray objects = dynamics_world->getCollisionObjectArray();
   EPhysics_World_Callback *cb;
   Eina_Bool world_active = EINA_FALSE;

   world = (EPhysics_World *) dynamics_world->getWorldUserInfo();

   for (int i = 0; i < objects.size(); i++)
     {
        EPhysics_Body *body;
        btRigidBody *rigid_body = btRigidBody::upcast(objects[i]);
        if (!rigid_body)
          continue;

        body = (EPhysics_Body *) rigid_body->getUserPointer();
        if (rigid_body->isActive())
          {
             ephysics_body_active_set(body, EINA_TRUE);
             ephysics_body_evas_object_update_select(body);
             world_active = EINA_TRUE;
          }
        else
          ephysics_body_active_set(body, EINA_FALSE);
     }

   if (world->active == world_active) return;
   world->active = world_active;
   if (world_active) return;

   EINA_INLIST_FOREACH(world->callbacks, cb)
      if (cb->type == EPHYSICS_CALLBACK_WORLD_STOPPED)
        cb->func(cb->data, world, NULL);
}

static Eina_Bool
_simulate_worlds(void *data)
{
   Eina_List *l, *lworlds = (Eina_List *) data;
   double time_now;
   void *ldata;

   DBG("Step");

   EINA_LIST_FOREACH(lworlds, l, ldata)
     {
        double time_now, delta;
        EPhysics_World *world = (EPhysics_World *) ldata;

        if (!world->running)
          continue;

        time_now = ecore_time_get();
        delta = time_now - world->last_update;
        world->last_update = time_now;

        world->dynamics_world->stepSimulation(delta, 1, 1/40.f);
     }

   return EINA_TRUE;
}

static bool
_ephysics_world_contact_processed_cb(btManifoldPoint &cp, void *b0, void *b1)
{
   btRigidBody *rigid_body_0, *rigid_body_1;
   EPhysics_Body *body_0, *body_1;

   rigid_body_0 = (btRigidBody *) b0;
   rigid_body_1 = (btRigidBody *) b1;

   body_0 = (EPhysics_Body *) rigid_body_0->getUserPointer();
   body_1 = (EPhysics_Body *) rigid_body_1->getUserPointer();

   ephysics_body_contact_processed(body_0, body_1);
   ephysics_body_contact_processed(body_1, body_0);
}

static void
_ephysics_world_boundary_del_cb(void *data, EPhysics_Body *body, void *event_info)
{
   EPhysics_World *world = (EPhysics_World *) data;
   int i;

   for (i = 0; i < EPHYSICS_WORLD_BOUNDARY_LAST; i++)
     {
        if (world->boundaries[i] == body)
          {
             world->boundaries[i] = NULL;
             return;
          }
     }
}

Eina_Bool
ephysics_world_body_add(EPhysics_World *world, const EPhysics_Body *body, btRigidBody *rigid_body)
{
   world->bodies = eina_list_append(world->bodies, body);
   if (eina_error_get())
     {
        ERR("Couldn't add body to bodies list.");
        return EINA_FALSE;
     }
   world->dynamics_world->addRigidBody(rigid_body);
   return EINA_TRUE;
}

void
ephysics_world_body_del(EPhysics_World *world, const EPhysics_Body *body, btRigidBody *rigid_body)
{
   world->dynamics_world->removeRigidBody(rigid_body);
   world->bodies = eina_list_remove(world->bodies, body);
}

void
ephysics_world_constraint_add(EPhysics_World *world, btTypedConstraint *bt_constraint)
{
   world->dynamics_world->addConstraint(bt_constraint);
}

void
ephysics_world_constraint_del(EPhysics_World *world, btTypedConstraint *bt_constraint)
{
   world->dynamics_world->removeConstraint(bt_constraint);
}

int
ephysics_world_init(void)
{
   if (++_ephysics_world_init_count != 1)
     return _ephysics_world_init_count;

   gContactProcessedCallback = _ephysics_world_contact_processed_cb;

   INF("EPhysics World initialized.");

   return _ephysics_world_init_count;
}

int
ephysics_world_shutdown(void)
{
   void *ldata;

   if (--_ephysics_world_init_count != 0)
     return _ephysics_world_init_count;

   if (_anim_simulate)
     {
        ecore_animator_del(_anim_simulate);
        _anim_simulate = NULL;
     }

   EINA_LIST_FREE(_worlds, ldata)
     {
        EPhysics_World *world = (EPhysics_World *) ldata;
        ephysics_world_del(world);
     }

   _worlds_running = 0;

   INF("EPhysics World shutdown.");

   return _ephysics_world_init_count;
}

void
ephysics_world_boundary_set(EPhysics_World *world, EPhysics_World_Boundary boundary, EPhysics_Body *body)
{
   world->boundaries[boundary] = body;
   ephysics_body_event_callback_add(body, EPHYSICS_CALLBACK_BODY_DEL,
                                    _ephysics_world_boundary_del_cb, world);
}

EPhysics_Body *
ephysics_world_boundary_get(const EPhysics_World *world, EPhysics_World_Boundary boundary)
{
   return world->boundaries[boundary];
}

EAPI EPhysics_World *
ephysics_world_new(void)
{
   EPhysics_World *world;

   world = (EPhysics_World *) calloc(1, sizeof(EPhysics_World));
   if (!world)
     {
        ERR("Couldn't create a new world instance.");
        return NULL;
     }

   world->camera = ephysics_camera_add(world);
   if (!world->camera)
     {
        ERR("Couldn't create a camera for this world.");
        goto no_camera;
     }

   world->broadphase = new btDbvtBroadphase();
   if (!world->broadphase)
     {
        ERR("Couldn't set broadphase.");
        goto no_broadphase;
     }

   world->collision = new btDefaultCollisionConfiguration();
   if (!world->collision)
     {
        ERR("Couldn't configure collision.");
        goto no_collision;
     }

   world->dispatcher = new btCollisionDispatcher(world->collision);
   if (!world->dispatcher)
     {
        ERR("Couldn't create dispatcher.");
        goto no_dispatcher;
     }

   world->solver = new btSequentialImpulseConstraintSolver;
   if (!world->solver)
     {
        ERR("Couldn't create solver.");
        goto no_solver;
     }

   world->dynamics_world = new btDiscreteDynamicsWorld(
      world->dispatcher, world->broadphase, world->solver,
      world->collision);
   if (!world->dynamics_world)
     {
        ERR("Couldn't create dynamic world.");
        goto no_world;
     }

   _worlds = eina_list_append(_worlds, world);
   if (eina_error_get())
     {
        ERR("Couldn't add world to worlds list.");
        goto no_list;
     }

   world->dynamics_world->setGravity(btVector3(0, -9.81, 0));
   world->rate = 30;
   world->dynamics_world->setInternalTickCallback(_ephysics_world_tick_cb,
                                                  (void *) world);

   world->running = EINA_TRUE;
   world->last_update = ecore_time_get();
   _worlds_running++;
   if (!_anim_simulate)
     _anim_simulate = ecore_animator_add(_simulate_worlds, _worlds);

   INF("World added.");
   return world;

no_list:
   delete world->dynamics_world;
no_world:
   delete world->solver;
no_solver:
   delete world->dispatcher;
no_dispatcher:
   delete world->collision;
no_collision:
   delete world->broadphase;
no_broadphase:
   ephysics_camera_del(world->camera);
no_camera:
   free(world);
   return NULL;
}

EAPI Eina_Bool
ephysics_world_serialize(const EPhysics_World *world, const char *path)
{
   btDefaultSerializer *serializer;
   FILE *file;

   if (!world)
     {
        WRN("Could not serialize, world not provided.");
        return EINA_FALSE;
      }

   file = fopen(path, "wb");
   if (!file)
     {
        WRN("Could not serialize, could not open file: %s", path);
        return EINA_FALSE;
     }

   serializer = new btDefaultSerializer();
   world->dynamics_world->serialize(serializer);

   if (!fwrite(serializer->getBufferPointer(),
              serializer->getCurrentBufferSize(), 1, file))
     {
        WRN("Problems on writing to: %s.", path);
        fclose(file);
        delete serializer;
        return EINA_FALSE;
     }

   fclose(file);
   delete serializer;

   INF("Serialization written to file: %s.", path);

   return EINA_TRUE;
}

EAPI void
ephysics_world_del(EPhysics_World *world)
{
   EPhysics_World_Callback *cb;
   void *data;

   if (!world)
     {
        ERR("Can't delete world, it wasn't provided.");
        return;
     }

   EINA_INLIST_FOREACH(world->callbacks, cb)
     {
        if (cb->type == EPHYSICS_CALLBACK_WORLD_DEL)
          cb->func(cb->data, world, NULL);
     }

   ephysics_world_running_set(world, EINA_FALSE);
   _worlds = eina_list_remove(_worlds, world);

   while (world->callbacks)
     {
        cb = EINA_INLIST_CONTAINER_GET(world->callbacks,
                                       EPhysics_World_Callback);
        world->callbacks = eina_inlist_remove(world->callbacks,
                                              world->callbacks);
        free(cb);
     }

   EINA_LIST_FREE(world->bodies, data)
      ephysics_orphan_body_del((EPhysics_Body *) data);

   ephysics_camera_del(world->camera);
   delete world->dynamics_world;
   delete world->solver;
   delete world->dispatcher;
   delete world->collision;
   delete world->broadphase;

   free(world);
   INF("World deleted.");
}

EAPI void
ephysics_world_running_set(EPhysics_World *world, Eina_Bool running)
{
   if (!world)
     {
        ERR("Can't (un)pause world, it wasn't provided.");
        return;
     }

   if ((!!running) == world->running) return;

   world->running = !!running;

   if (world->running)
     {
        world->last_update = ecore_time_get();
        _worlds_running++;
        INF("World unpaused.");
     }
   else
     {
        _worlds_running--;
        INF("World paused.");
     }

   if (!_worlds_running)
     {
        if (_anim_simulate)
          {
             ecore_animator_del(_anim_simulate);
             _anim_simulate = NULL;
          }
        return;
     }

   if (_anim_simulate)
     return;

   _anim_simulate = ecore_animator_add(_simulate_worlds, _worlds);
}

EAPI Eina_Bool
ephysics_world_running_get(const EPhysics_World *world)
{
   if (!world)
     {
	ERR("No world, no running status for you.");
	return EINA_FALSE;
     }

   return world->running;
}

EAPI void
ephysics_world_gravity_set(EPhysics_World *world, double gx, double gy)
{
   if (!world)
     {
        ERR("Can't set gravity, no world provided.");
        return;
     }

   world->dynamics_world->setGravity(btVector3(gx, gy, 0));
   DBG("World gravity set to X:%lf, Y:%lf.", gx, gy);
}

EAPI void
ephysics_world_gravity_get(const EPhysics_World *world, double *gx, double *gy)
{
   btVector3 vector;

   if (!world)
     {
	ERR("No world, can't get gravity.");
	return;
     }

   vector = world->dynamics_world->getGravity();

   if (gx) *gx = vector.x();
   if (gy) *gy = vector.y();
}

EAPI void
ephysics_world_rate_set(EPhysics_World *world, double rate)
{
   if (!world)
     {
	ERR("No world, can't set rate.");
	return;
     }

   if (rate <= 0)
     {
	ERR("Rate should be a positive value. Keeping the old value: %lf.",
            world->rate);
        return;
     }

   /* FIXME update objects considering new rate */
   world->rate = rate;
}

EAPI double
ephysics_world_rate_get(const EPhysics_World *world)
{
   if (!world)
     {
	ERR("No world, can't get rate.");
	return 0;
     }

   return world->rate;
}

EAPI EPhysics_Camera *
ephysics_world_camera_get(const EPhysics_World *world)
{
   if (!world)
     {
	ERR("No world, no camera for you.");
	return NULL;
     }

   return world->camera;
}

EAPI void
ephysics_world_event_callback_add(EPhysics_World *world, EPhysics_Callback_Type type, EPhysics_World_Event_Cb func, const void *data)
{
   EPhysics_World_Callback *cb;

   if (!world)
     {
        ERR("Can't set world event callback, world is null.");
        return;
     }

   if (!func)
     {
        ERR("Can't set world event callback, function is null.");
        return;
     }

   if ((type <= EPHYSICS_CALLBACK_WORLD_FIRST) ||
       (type >= EPHYSICS_CALLBACK_WORLD_LAST))
     {
        ERR("Can't set world event callback, callback type is wrong.");
        return;
     }

   cb = (EPhysics_World_Callback *) malloc(sizeof(EPhysics_World_Callback));
   if (!cb)
     {
        ERR("Can't set world event callback, can't create cb instance.");
        return;
     }

   cb->func = func;
   cb->type = type;
   cb->data = (void *) data;

   world->callbacks = eina_inlist_append(world->callbacks, EINA_INLIST_GET(cb));
}

EAPI void *
ephysics_world_event_callback_del(EPhysics_World *world, EPhysics_Callback_Type type, EPhysics_World_Event_Cb func)
{
   EPhysics_World_Callback *cb;
   void *cb_data;

   if (!world)
     {
        ERR("Can't delete world event callback, world is null.");
        return NULL;
     }

   EINA_INLIST_FOREACH(world->callbacks, cb)
     {
        if ((cb->type == type) && (cb->func == func)) {
             cb_data = cb->data;
             world->callbacks = eina_inlist_remove(world->callbacks,
                                                  EINA_INLIST_GET(cb));
             free(cb);
             return cb_data;
        }
     }

   return NULL;
}

EAPI const Eina_List *
ephysics_world_bodies_get(const EPhysics_World *world)
{
   if (!world)
     {
        ERR("Couldn't get the bodies list, no world provided.");
        return NULL;
     }

   return world->bodies;
}

EAPI void
ephysics_world_render_geometry_set(EPhysics_World *world, Evas_Coord x, Evas_Coord y, Evas_Coord w, Evas_Coord h)
{
   if (!world)
     {
        ERR("Can't set geometry, world wasn't provided.");
        return;
     }

   if ((w <= 0) || (h <= 0))
     {
        ERR("Invalid width or height sizes. They must to be positive values.");
        return;
     }

   world->x = x;
   world->y = y;
   world->w = w;
   world->h = h;

   ephysics_body_world_boundaries_resize(world);
}

EAPI void
ephysics_world_render_geometry_get(EPhysics_World *world, Evas_Coord *x, Evas_Coord *y, Evas_Coord *w, Evas_Coord *h)
{
   if (!world)
     {
        ERR("Can't get geometry, world is null.");
        return;
     }

   if (x) *x = world->x;
   if (y) *y = world->y;
   if (w) *w = world->w;
   if (h) *h = world->h;
}

#ifdef  __cplusplus
}
#endif