efl

Форк
0
/
physics_actions.edc 
573 строки · 19.0 Кб
1
/* This example demostrates physics actions, like applying impulses and forces
2
 * to a body.
3
 *
4
 * There are two ways of interacting with bodies. One is using the wanted
5
 * action in a program. E.g.:
6
 *
7
 * action: PHYSICS_VEL_SET 0 -200 0;
8
 * target: "part_name";
9
 *
10
 * The other way is via script. The same action described above would be
11
 * physics_set_velocity(PART:"part_name", 0, -200, 0);
12
 *
13
 * This a very extensive example, because it illustrate the usage of all
14
 * the possible actions in the both ways.
15
 *
16
 * It can be tested with edje_player slave mode
17
 * $ edje_player -S -p physics_actions.edj
18
 *
19
 * signal up impulse   -> will throw both balls up
20
 * signal down impulse   -> will throw both balls down
21
 * signal left impulse -> will throw blue ball to the left
22
 * signal right impulse -> will throw red ball to the right
23
 * signal clockwise impulse -> will roll blue ball in clockwise
24
 * signal counterclockwise impulse -> will roll blue ball in counterclockwise
25
 * signal up force -> will apply a force up in blue ball
26
 * signal down force -> will apply a force down in blue ball
27
 * signal left force -> will apply a force left in blue ball
28
 * signal right force -> will apply a force right in blue ball
29
 * signal clockwise torque -> will apply a clockwise torque in blue ball
30
 * signal counterclockwise torque -> will apply a counterclockwise torque
31
 *                                   in blue ball
32
 * signal clear force -> will clear all forces applied over blue ball
33
 * signal up velocity -> will set a velocity up in blue ball
34
 * signal down velocity -> will set a velocity down in blue ball
35
 * signal left velocity -> will set a velocity left in blue ball
36
 * signal right velocity -> will set a velocity right in blue ball
37
 * signal clockwise velocity -> will set a clockwise velocity in blue ball
38
 * signal counterclockwise velocity -> will set a counterclockwise velocity
39
 *                                   in blue ball
40
 * signal stop velocity -> will stop the blue ball
41
 * signal clockwise rotation -> will rotate blue ball 90o degrees clockwise
42
 * signal counterclockwise rotation -> will rotate blue ball 90o degrees
43
 *                                     counterclockwise
44
 *
45
 * message 1 FLOAT_SET 3 50 -100 0 -> apply an impulse on blue ball with
46
 *    x = 50, y = -100, z = 0, for example
47
 * message 2 FLOAT_SET 3 0 0 8.2 -> apply a torque impulse on blue ball with
48
 *    x = 4, y = 0, z = 0.8, for example
49
 * message 3 FLOAT_SET 3 80 100.4 0 -> apply a force on blue ball with
50
 *    x = 80, y = 100.4, z = 0, for example
51
 * message 4 FLOAT_SET 3 0 0 -5.6 -> apply a torque on blue ball with
52
 *    x = 0, y = 0, z = -5.6, for example
53
 * message 5 STRING "blue_circle" -> clear all forces of part.
54
 *    It will clear all forces (linear and torque) over "blue_circle",
55
 *    for example.
56
 * message 6 STRING "blue_circle" -> return a message with all forces applied
57
 *    over the part.
58
 * message 7 STRING "blue_circle" -> return a message with all torques applied
59
 *    over the part.
60
 * message 8 FLOAT_SET 3 300 -103.2 0 -> set linear velocity of the blue ball
61
 *    with x = 300, y = -103.2, z = 150, for example
62
 * message 9 STRING "blue_circle" -> return a message with part's linear
63
 *    velocity.
64
 * message 10 FLOAT_SET 3 0 0 150 -> set angular velocity of the blue ball
65
 *    with x = 0, y = 0, z = 150, for example
66
 * message 11 STRING "blue_circle" -> return a message with part's angular
67
 *    velocity.
68
 * message 12 STRING "blue_circle" -> stop the part.
69
 * message 13 FLOAT_SET 4 0.707 0 0 0.707 -> set blue ball rotation with
70
 *    quaternion w = 0.707, x = 0, y = 0, z = 0.707
71
 * message 14 STRING "blue_circle" -> return a message with part's rotation
72
 */
73

74
#define ID_IMPULSE (1)
75
#define ID_TORQUE_IMPULSE (2)
76
#define ID_FORCE (3)
77
#define ID_TORQUE (4)
78
#define ID_FORCES_CLEAR (5)
79
#define ID_FORCES_GET (6)
80
#define ID_TORQUES_GET (7)
81
#define ID_VEL_SET (8)
82
#define ID_VEL_GET (9)
83
#define ID_ANG_VEL_SET (10)
84
#define ID_ANG_VEL_GET (11)
85
#define ID_STOP (12)
86
#define ID_ROT_SET (13)
87
#define ID_ROT_GET (14)
88

89
collections {
90

91
   images {
92
      image: "bubble-blue.png" COMP;
93
   }
94

95
   group {
96
      name: "example_group";
97

98
      script {
99
         public message(Msg_Type:type, id, ...) {
100
            if ((id == ID_IMPULSE) && (type == MSG_FLOAT_SET)) {
101
               new Float:x, Float:y, Float:z;
102
               new n = numargs();
103
               if (n < 5) return;
104
               x = getfarg(2);
105
               y = getfarg(3);
106
               z = getfarg(4);
107
               physics_impulse(PART:"blue_circle", x, y, z);
108
            }
109
            else if ((id == ID_TORQUE_IMPULSE) && (type == MSG_FLOAT_SET)) {
110
               new Float:x, Float:y, Float:z;
111
               new n = numargs();
112
               if (n < 5) return;
113
               x = getfarg(2);
114
               y = getfarg(3);
115
               z = getfarg(4);
116
               physics_torque_impulse(PART:"blue_circle", x, y, z);
117
            }
118
            else if ((id == ID_FORCE) && (type == MSG_FLOAT_SET)) {
119
               new Float:x, Float:y, Float:z;
120
               new n = numargs();
121
               if (n < 5) return;
122
               x = getfarg(2);
123
               y = getfarg(3);
124
               z = getfarg(4);
125
               physics_force(PART:"blue_circle", x, y, z);
126
            }
127
            else if ((id == ID_TORQUE) && (type == MSG_FLOAT_SET)) {
128
               new Float:x, Float:y, Float:z;
129
               new n = numargs();
130
               if (n < 5) return;
131
               x = getfarg(2);
132
               y = getfarg(3);
133
               z = getfarg(4);
134
               physics_torque(PART:"blue_circle", x, y, z);
135
            }
136
            else if ((id == ID_FORCES_CLEAR) && (type == MSG_STRING)) {
137
               new pid, name[1024];
138
               getsarg(2, name, sizeof(name));
139
               pid = get_part_id(name);
140
               if (!pid) return;
141
               physics_clear_forces(pid);
142
            }
143
            else if ((id == ID_FORCES_GET) && (type == MSG_STRING)) {
144
               new Float:x, Float:y, Float:z;
145
               new pid, name[1024];
146
               getsarg(2, name, sizeof(name));
147
               pid = get_part_id(name);
148
               if (!pid) return;
149
               physics_get_forces(pid, x, y, z);
150
               send_message(MSG_FLOAT_SET, id, x, y, z);
151
            }
152
            else if ((id == ID_TORQUES_GET) && (type == MSG_STRING)) {
153
               new Float:x, Float:y, Float:z;
154
               new pid, name[1024];
155
               getsarg(2, name, sizeof(name));
156
               pid = get_part_id(name);
157
               if (!pid) return;
158
               physics_get_torques(pid, x, y, z);
159
               send_message(MSG_FLOAT_SET, id, x, y, z);
160
            }
161
            else if ((id == ID_VEL_SET) && (type == MSG_FLOAT_SET)) {
162
               new Float:x, Float:y, Float:z;
163
               new n = numargs();
164
               if (n < 5) return;
165
               x = getfarg(2);
166
               y = getfarg(3);
167
               z = getfarg(4);
168
               physics_set_velocity(PART:"blue_circle", x, y, z);
169
            }
170
            else if ((id == ID_VEL_GET) && (type == MSG_STRING)) {
171
               new Float:x, Float:y, Float:z;
172
               new pid, name[1024];
173
               getsarg(2, name, sizeof(name));
174
               pid = get_part_id(name);
175
               if (!pid) return;
176
               physics_get_velocity(pid, x, y, z);
177
               send_message(MSG_FLOAT_SET, id, x, y, z);
178
            }
179
            else if ((id == ID_ANG_VEL_SET) && (type == MSG_FLOAT_SET)) {
180
               new Float:x, Float:y, Float:z;
181
               new n = numargs();
182
               if (n < 5) return;
183
               x = getfarg(2);
184
               y = getfarg(3);
185
               z = getfarg(4);
186
               physics_set_ang_velocity(PART:"blue_circle", x, y, z);
187
            }
188
            else if ((id == ID_ANG_VEL_GET) && (type == MSG_STRING)) {
189
               new Float:x, Float:y, Float:z;
190
               new pid, name[1024];
191
               getsarg(2, name, sizeof(name));
192
               pid = get_part_id(name);
193
               if (!pid) return;
194
               physics_get_ang_velocity(pid, x, y, z);
195
               send_message(MSG_FLOAT_SET, id, x, y, z);
196
            }
197
            else if ((id == ID_STOP) && (type == MSG_STRING)) {
198
               new pid, name[1024];
199
               getsarg(2, name, sizeof(name));
200
               pid = get_part_id(name);
201
               if (!pid) return;
202
               physics_stop(pid);
203
            }
204
            else if ((id == ID_ROT_SET) && (type == MSG_FLOAT_SET)) {
205
               new Float:w, Float:x, Float:y, Float:z;
206
               new n = numargs();
207
               if (n < 6) return;
208
               w = getfarg(2);
209
               x = getfarg(3);
210
               y = getfarg(4);
211
               z = getfarg(5);
212
               physics_set_rotation(PART:"blue_circle", w, x, y, z);
213
            }
214
            else if ((id == ID_ROT_GET) && (type == MSG_STRING)) {
215
               new Float:w, Float:x, Float:y, Float:z;
216
               new pid, name[1024];
217
               getsarg(2, name, sizeof(name));
218
               pid = get_part_id(name);
219
               if (!pid) return;
220
               physics_get_rotation(pid, w, x, y, z);
221
               send_message(MSG_FLOAT_SET, id, w, x, y, z);
222
            }
223
         }
224
      }
225

226
      parts {
227
         part {
228
            name: "background";
229
            type: RECT;
230
            physics_body: NONE;
231
            description {
232
               state: "default" 0.0;
233
               color: 255 255 255 255; /* white */
234
               rel1.relative: 0.0 0.0;
235
               rel2.relative: 1.0 1.0;
236
            }
237
         }
238

239
         part {
240
            name: "blue_circle";
241
            type: IMAGE;
242
            physics_body: RIGID_SPHERE;
243
            description {
244
               state: "default" 0.0;
245
               rel1.relative: 0.35 0.1;
246
               rel2.relative: 0.55 0.2;
247
               aspect: 1 1;
248
               image {
249
                  normal: "bubble-blue.png";
250
               }
251
               physics {
252
                  restitution: 0.85;
253
                  friction: 1.0;
254
               }
255
            }
256
         }
257

258
         part {
259
            name: "red_circle";
260
            type: IMAGE;
261
            physics_body: RIGID_SPHERE;
262
            description {
263
               state: "default" 0.0;
264
               color: 255 0 0 255; /* light red */
265
               rel1.relative: 0.65 0.1;
266
               rel2.relative: 0.85 0.2;
267
               aspect: 1 1;
268
               image {
269
                  normal: "bubble-blue.png";
270
               }
271
               physics {
272
                  restitution: 0.85;
273
                  friction: 1.0;
274
               }
275
            }
276
         }
277

278
         part {
279
            name: "floor";
280
            type: RECT;
281
            physics_body: BOUNDARY_BOTTOM;
282
            description {
283
               state: "default" 0.0;
284
               visible: 0;
285
               physics {
286
                  restitution: 0.6;
287
                  friction: 1.0;
288
               }
289
            }
290
         }
291

292
         part {
293
            name: "right_wall";
294
            type: RECT;
295
            physics_body: BOUNDARY_RIGHT;
296
            description {
297
               state: "default" 0.0;
298
               visible: 0;
299
               physics {
300
                  restitution: 0.3;
301
               }
302
            }
303
         }
304

305
         part {
306
            name: "left_wall";
307
            type: RECT;
308
            physics_body: BOUNDARY_LEFT;
309
            description {
310
               state: "default" 0.0;
311
               visible: 0;
312
               physics {
313
                  restitution: 0.3;
314
               }
315
            }
316
         }
317

318
         part {
319
            name: "roof";
320
            type: RECT;
321
            physics_body: BOUNDARY_TOP;
322
            description {
323
               state: "default" 0.0;
324
               visible: 0;
325
               physics {
326
                  restitution: 0.2;
327
               }
328
            }
329
         }
330
      }
331

332
      programs {
333

334
         program {
335
            name: "impulse_up";
336
            signal: "up";
337
            source: "impulse";
338
            action: PHYSICS_IMPULSE 0 -300 0;
339
            target: "blue_circle";
340
            target: "red_circle";
341
         }
342

343
         program {
344
            name: "impulse_down";
345
            signal: "down";
346
            source: "impulse";
347
            action: PHYSICS_IMPULSE 0 300 0;
348
            target: "red_circle";
349
            target: "blue_circle";
350
         }
351

352
         program {
353
            name: "impulse_left";
354
            signal: "left";
355
            source: "impulse";
356
            action: PHYSICS_IMPULSE -300 0 0;
357
            target: "blue_circle";
358
         }
359

360
         program {
361
            name: "impulse_right";
362
            signal: "right";
363
            source: "impulse";
364
            action: PHYSICS_IMPULSE 300 0 0;
365
            target: "red_circle";
366
         }
367

368
         program {
369
            name: "impulse_clockwise";
370
            signal: "clockwise";
371
            source: "impulse";
372
            action: PHYSICS_TORQUE_IMPULSE 0 0 4;
373
            target: "blue_circle";
374
         }
375

376
         program {
377
            name: "impulse_counterclockwise";
378
            signal: "counterclockwise";
379
            source: "impulse";
380
            action: PHYSICS_TORQUE_IMPULSE 0 0 -4;
381
            target: "blue_circle";
382
         }
383

384
         program {
385
            name: "force_up";
386
            signal: "up";
387
            source: "force";
388
            action: PHYSICS_FORCE 0 -300 0;
389
            target: "blue_circle";
390
         }
391

392
         program {
393
            name: "force_down";
394
            signal: "down";
395
            source: "force";
396
            action: PHYSICS_FORCE 0 300 0;
397
            target: "blue_circle";
398
         }
399

400
         program {
401
            name: "force_left";
402
            signal: "left";
403
            source: "force";
404
            action: PHYSICS_FORCE -300 0 0;
405
            target: "blue_circle";
406
         }
407

408
         program {
409
            name: "force_right";
410
            signal: "right";
411
            source: "force";
412
            action: PHYSICS_FORCE 300 0 0;
413
            target: "blue_circle";
414
         }
415

416
         program {
417
            name: "torque_clockwise";
418
            signal: "clockwise";
419
            source: "torque";
420
            action: PHYSICS_TORQUE 0 0 4;
421
            target: "blue_circle";
422
         }
423

424
         program {
425
            name: "torque_counterclockwise";
426
            signal: "counterclockwise";
427
            source: "torque";
428
            action: PHYSICS_TORQUE 0 0 -4;
429
            target: "blue_circle";
430
         }
431

432
         program {
433
            name: "forces_clear";
434
            signal: "clear";
435
            source: "force";
436
            action: PHYSICS_FORCES_CLEAR;
437
            target: "blue_circle";
438
         }
439

440
         program {
441
            name: "velocity_up";
442
            signal: "up";
443
            source: "velocity";
444
            action: PHYSICS_VEL_SET 0 -200 0;
445
            target: "blue_circle";
446
         }
447

448
         program {
449
            name: "velocity_down";
450
            signal: "down";
451
            source: "velocity";
452
            action: PHYSICS_VEL_SET 0 200 0;
453
            target: "blue_circle";
454
         }
455

456
         program {
457
            name: "velocity_left";
458
            signal: "left";
459
            source: "velocity";
460
            action: PHYSICS_VEL_SET -200 0 0;
461
            target: "blue_circle";
462
         }
463

464
         program {
465
            name: "velocity_right";
466
            signal: "right";
467
            source: "velocity";
468
            action: PHYSICS_VEL_SET 200 0 0;
469
            target: "blue_circle";
470
         }
471

472
         program {
473
            name: "velocity_clockwise";
474
            signal: "clockwise";
475
            source: "velocity";
476
            action: PHYSICS_ANG_VEL_SET 0 0 80;
477
            target: "blue_circle";
478
         }
479

480
         program {
481
            name: "velocity_counterclockwise";
482
            signal: "counterclockwise";
483
            source: "velocity";
484
            action: PHYSICS_ANG_VEL_SET 0 0 -80;
485
            target: "blue_circle";
486
         }
487

488
         program {
489
            name: "stop";
490
            signal: "stop";
491
            source: "velocity";
492
            action: PHYSICS_STOP;
493
            target: "blue_circle";
494
         }
495

496
         program {
497
            name: "rotation_clockwise";
498
            signal: "clockwise";
499
            source: "rotation";
500
            action: PHYSICS_ROT_SET 0.707 0 0 0.707;
501
            target: "blue_circle";
502
         }
503

504
         program {
505
            name: "rotation_counterclockwise";
506
            signal: "counterclockwise";
507
            source: "rotation";
508
            action: PHYSICS_ROT_SET 0.707 0 0 -0.707;
509
            target: "blue_circle";
510
         }
511

512
         program {
513
            name: "customize";
514
            signal: "custom";
515
            script {
516
               new Float: mass, Float:rest, Float:fric;
517
               new Float: linear, Float:angular;
518
               new val;
519

520
               custom_state(PART:"red_circle", "default", 0.0);
521
               set_state_val(PART:"red_circle", STATE_COLOR, 0, 0, 0, 255);
522
               set_state_val(PART:"red_circle", STATE_PHYSICS_MASS, 4.5);
523
               set_state_val(PART:"red_circle", STATE_PHYSICS_RESTITUTION, 0.1);
524
               set_state_val(PART:"red_circle", STATE_PHYSICS_FRICTION, 0.345);
525
               set_state_val(PART:"red_circle", STATE_PHYSICS_DAMPING, 0.3,
526
                       0.1);
527
               set_state_val(PART:"red_circle", STATE_PHYSICS_SLEEP, 34.1,
528
                       12.83);
529
               set_state_val(PART:"red_circle", STATE_PHYSICS_LIGHT_ON, 1);
530
               set_state_val(PART:"red_circle",
531
                       STATE_PHYSICS_IGNORE_PART_POS, 1);
532
               set_state_val(PART:"red_circle", STATE_PHYSICS_Z, -40);
533
               set_state_val(PART:"red_circle", STATE_PHYSICS_DEPTH, 80);
534
               set_state(PART:"red_circle", "custom", 0.0);
535

536
               get_state_val(PART:"red_circle", STATE_PHYSICS_MASS, mass);
537
               get_state_val(PART:"red_circle", STATE_PHYSICS_RESTITUTION,
538
                     rest);
539
               get_state_val(PART:"red_circle", STATE_PHYSICS_FRICTION,
540
                     fric);
541
               send_message(MSG_STRING_FLOAT_SET, 1, "Mass", mass);
542
               send_message(MSG_STRING_FLOAT_SET, 1, "Friction", fric);
543
               send_message(MSG_STRING_FLOAT_SET, 1, "Restitution", rest);
544

545
               get_state_val(PART:"red_circle", STATE_PHYSICS_DAMPING,
546
                     linear, angular);
547
               send_message(MSG_STRING_FLOAT_SET, 1, "Damping", linear,
548
                       angular);
549

550
               get_state_val(PART:"red_circle", STATE_PHYSICS_SLEEP,
551
                     linear, angular);
552
               send_message(MSG_STRING_FLOAT_SET, 1, "Sleep", linear,
553
                       angular);
554

555
               get_state_val(PART:"red_circle", STATE_PHYSICS_LIGHT_ON, val);
556
               send_message(MSG_STRING_INT, 1, "Light On", val);
557

558
               get_state_val(PART:"red_circle",
559
                       STATE_PHYSICS_IGNORE_PART_POS, val);
560
               send_message(MSG_STRING_INT, 1, "Ignore Part Pos", val);
561

562
               get_state_val(PART:"red_circle", STATE_PHYSICS_Z, val);
563
               send_message(MSG_STRING_INT, 1, "Z", val);
564

565
               get_state_val(PART:"red_circle", STATE_PHYSICS_DEPTH, val);
566
               send_message(MSG_STRING_INT, 1, "Depth", val);
567
            }
568
         }
569

570
      }
571

572
   }
573
}
574

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.