-
Notifications
You must be signed in to change notification settings - Fork 2
/
physics.inc
1350 lines (1205 loc) · 41.9 KB
/
physics.inc
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* <SA-MP physics.inc - Handle collisions and more.>
Copyright (C) <2013> <Peppe>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>. */
#include <modelsizes>
#pragma compress 1
#if !defined _inc_y_iterate
#include <foreach>
#endif
#if !defined _inc_streamer
#include <streamer>
#endif
#if defined COLANDREAS
#include <colandreas>
#endif
#if !defined PHY_TIMER_INTERVAL
#define PHY_TIMER_INTERVAL (20)
#endif
#if !defined PHY_MAX_WALLS
#define PHY_MAX_WALLS (512)
#endif
#if !defined PHY_MAX_CYLINDERS
#define PHY_MAX_CYLINDERS (512)
#endif
#if !defined PHY_MAX_OBJECTS
#define PHY_MAX_OBJECTS (1024)
#endif
#define PHY_MODE_3D (0)
#define PHY_MODE_2D (1)
#define PHY_ROLLING_MODE_DEFAULT (0)
#define PHY_ROLLING_MODE_ADVANCED (1)
#if !defined FLOAT_INFINITY
#define FLOAT_INFINITY (Float:0x7F800000)
#endif
#if !defined FLOAT_NEG_INFINITY
#define FLOAT_NEG_INFINITY (Float:0xFF800000)
#endif
#if !defined FLOAT_NAN
#define FLOAT_NAN (Float:0xFFFFFFFF)
#endif
/* Callbacks */
forward PHY_OnObjectUpdate(objectid);
forward PHY_OnObjectCOLWithObj(object1, object2); // PHY_OnObjectCollideWithObject
forward PHY_OnObjectCollideWithZBound(objectid, lowhigh); // low bound = 0, high bound = 1
forward PHY_OnObjectCollideWithSAWorld(objectid, Float:cx, Float:cy, Float:cz);
forward PHY_OnObjectCollideWithWall(objectid, wallid);
forward PHY_OnObjectCollideWithCylinder(objectid, cylinderid);
forward PHY_OnObjectCollideWithPlayer(objectid, playerid);
enum (<<= 1)
{
PHY_OBJECT_USED = 0b01,
PHY_OBJECT_MODE,
PHY_OBJECT_ROLL,
PHY_OBJECT_ROLLING_MODE,
PHY_OBJECT_COLANDREAS_BOUNDS,
PHY_OBJECT_COLANDREAS_COLLS,
PHY_OBJECT_GHOST_OBJECTS,
PHY_OBJECT_GHOST_WALLS,
PHY_OBJECT_GHOST_CYLINDERS,
PHY_OBJECT_PLAYER_COLLISIONS
}
enum E_PHY_OBJECT
{
PHY_DynamicObject,
PHY_Properties,
PHY_World,
Float:PHY_Size,
Float:PHY_Mass,
Float:PHY_VX,
Float:PHY_VY,
Float:PHY_VZ,
Float:PHY_AX,
Float:PHY_AY,
Float:PHY_AZ,
Float:PHY_AngularVelocity,
Float:PHY_AxisX,
Float:PHY_AxisY,
Float:PHY_AxisZ,
Float:PHY_QW,
Float:PHY_QX,
Float:PHY_QY,
Float:PHY_QZ,
Float:PHY_Friction,
Float:PHY_AirResistance,
Float:PHY_Gravity,
Float:PHY_LowZBound,
/*Float:PHY_LowZRX,
Float:PHY_LowZRY,*/
Float:PHY_HighZBound,
Float:PHY_BoundConst,
Float:PHY_PlayerConst,
Float:PHY_PlayerDist,
Float:PHY_PlayerLowZ,
Float:PHY_PlayerHighZ
}
new
PHY_Object[PHY_MAX_OBJECTS][E_PHY_OBJECT],
Iterator:ITER_Object<PHY_MAX_OBJECTS>;
enum E_PHY_WALL
{
PHY_Created,
PHY_World,
Float:PHY_X1,
Float:PHY_Y1,
Float:PHY_X2,
Float:PHY_Y2,
Float:PHY_Z1,
Float:PHY_Z2,
Float:PHY_BounceConst,
Float:PHY_ANG
}
new
PHY_Wall[PHY_MAX_WALLS][E_PHY_WALL],
Iterator:ITER_Wall<PHY_MAX_WALLS>;
enum E_PHY_CYLINDER
{
PHY_Created,
PHY_World,
Float:PHY_X,
Float:PHY_Y,
Float:PHY_Z1,
Float:PHY_Z2,
Float:PHY_Size,
Float:PHY_BounceConst
}
new
PHY_Cylinder[PHY_MAX_CYLINDERS][E_PHY_CYLINDER],
Iterator:ITER_Cylinder<PHY_MAX_CYLINDERS>;
enum E_PHY_PLAYER
{
PHY_World
}
new
PHY_Player[MAX_PLAYERS][E_PHY_PLAYER];
/* Macros are self-explanatory */
#define PHY_IsObjectUsingPhysics(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_USED)
#define PHY_IsObjectUsing3D(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_MODE)
#define PHY_IsObjectUsingColAndreasCollisions(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_COLANDREAS_COLLS)
#define PHY_IsObjectUsingColAndreasBounds(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_COLANDREAS_BOUNDS)
#define PHY_IsObjectGhostWithObjects(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_GHOST_OBJECTS)
#define PHY_IsObjectGhostWithWalls(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_GHOST_WALLS)
#define PHY_IsObjectGhostWithCylinders(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_GHOST_CYLINDERS)
#define PHY_IsObjectCollidingWithPlayers(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_PLAYER_COLLISIONS)
#define PHY_IsObjectMoving(%1) (PHY_Object[%1][PHY_VX] != 0 || PHY_Object[%1][PHY_VY] != 0 || PHY_Object[%1][PHY_VZ] != 0)
#define PHY_IsObjectRolling(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_ROLL)
#define PHY_GetObjectFriction(%1) (PHY_Object[%1][PHY_Friction])
#define PHY_GetObjectAirResistance(%1) (PHY_Object[%1][PHY_AirResistance])
#define PHY_GetObjectGravity(%1) (PHY_Object[%1][PHY_Gravity])
#define PHY_GetObjectMode(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_MODE)
#define PHY_IsObjectRollingModeAdvanced(%1) (PHY_Object[%1][PHY_Properties] & PHY_OBJECT_ROLLING_MODE)
static
cb_Connect;
public OnGameModeInit()
{
SetTimer("PHY_CoreTimer", PHY_TIMER_INTERVAL, true);
#if defined COLANDREAS
CA_Init();
#endif
cb_Connect = funcidx("PHY_OnPlayerConnect") != -1;
if(funcidx("PHY_OnGameModeInit") != -1)
return CallLocalFunction("PHY_OnGameModeInit", "");
return 1;
}
public OnPlayerConnect(playerid)
{
PHY_Player[playerid][PHY_World] = 0;
if(cb_Connect)
return CallLocalFunction("PHY_OnPlayerConnect", "i", playerid);
return 1;
}
forward PHY_CoreTimer();
public PHY_CoreTimer()
{
new
#if defined COLANDREAS
Float:mod,
Float:crx,
Float:cry,
Float:crz,
Float:cx,
Float:cy,
Float:cz,
#endif
Float:x,
Float:y,
Float:z,
Float:x1,
Float:y1,
Float:z1,
Float:xclosest,
Float:yclosest,
Float:x2,
Float:y2,
Float:z2,
Float:speed,
Float:dx,
Float:dy,
Float:dz,
Float:dist,
Float:maxdist,
Float:angle,
Float:moveangle,
Float:dvx,
Float:dvy,
Float:dvz,
Float:mag,
Float:tmpvx,
Float:tmpvy,
Float:tmpvz,
Float:newvy1,
Float:newvy2,
Float:newvx1,
Float:newvx2,
Float:newvz1,
Float:newvz2
/*Float:sx,
Float:sy*/;
foreach(new a : ITER_Object)
{
if(PHY_Object[a][PHY_Properties] & PHY_OBJECT_USED)
{
new obj_a = PHY_Object[a][PHY_DynamicObject];
GetDynamicObjectPos(obj_a, x, y, z);
x1 = x + PHY_Object[a][PHY_VX] * (PHY_TIMER_INTERVAL/1000.0);
y1 = y + PHY_Object[a][PHY_VY] * (PHY_TIMER_INTERVAL/1000.0);
if(PHY_GetObjectMode(a) == PHY_MODE_3D)
{
z1 = z + PHY_Object[a][PHY_VZ] * (PHY_TIMER_INTERVAL/1000.0);
if(z1 > PHY_Object[a][PHY_HighZBound])
{
if(PHY_Object[a][PHY_VZ] > 0)
{
PHY_Object[a][PHY_VZ] = -PHY_Object[a][PHY_VZ] * PHY_Object[a][PHY_BoundConst];
CallLocalFunction("PHY_OnObjectCollideWithZBound", "dd", obj_a, 1);
}
z1 = PHY_Object[a][PHY_HighZBound];
}
else if(z1 < PHY_Object[a][PHY_LowZBound])
{
if(PHY_Object[a][PHY_VZ] < 0)
{
PHY_Object[a][PHY_VZ] = -PHY_Object[a][PHY_VZ] * PHY_Object[a][PHY_BoundConst];
CallLocalFunction("PHY_OnObjectCollideWithZBound", "dd", obj_a, 0);
}
z1 = PHY_Object[a][PHY_LowZBound];
}
if(PHY_GetObjectGravity(a) != 0)
{
// ColAndreas Incline testing
/*if(z1 == PHY_Object[a][PHY_LowZBound])
{
if(PHY_Object[a][PHY_LowZRX] != 0.0 || PHY_Object[a][PHY_LowZRY] != 0.0)
{
sx = floatsin(PHY_Object[a][PHY_LowZRY], degrees);
cx = floatcos(PHY_Object[a][PHY_LowZRY], degrees);
sy = floatsin(-PHY_Object[a][PHY_LowZRX], degrees);
cy = floatcos(-PHY_Object[a][PHY_LowZRX], degrees);
PHY_Object[a][PHY_VZ] -= PHY_Object[a][PHY_Gravity] * (PHY_TIMER_INTERVAL/1000.0) * (sx * sx + sy * sy);
PHY_Object[a][PHY_VX] += PHY_Object[a][PHY_Gravity] * (PHY_TIMER_INTERVAL/1000.0) * cx * sx;
PHY_Object[a][PHY_VY] += PHY_Object[a][PHY_Gravity] * (PHY_TIMER_INTERVAL/1000.0) * cy * sy;
}
}*/
if(PHY_Object[a][PHY_VZ] > 0)
{
PHY_Object[a][PHY_VZ] -= PHY_Object[a][PHY_Gravity] * (PHY_TIMER_INTERVAL/1000.0);
if(PHY_Object[a][PHY_VZ] < 0)
PHY_Object[a][PHY_VZ] = 0;
}
else if(z1 > PHY_Object[a][PHY_LowZBound])
PHY_Object[a][PHY_VZ] -= PHY_Object[a][PHY_Gravity] * (PHY_TIMER_INTERVAL/1000.0);
}
}
else
z1 = z;
if(PHY_IsObjectMoving(a))
{
if(!PHY_IsObjectGhostWithObjects(a))
{
foreach(new b : ITER_Object)
{
if(a != b && PHY_Object[b][PHY_Properties] & PHY_OBJECT_USED && !PHY_IsObjectGhostWithObjects(b) && (!PHY_Object[a][PHY_World] || !PHY_Object[b][PHY_World] || PHY_Object[a][PHY_World] == PHY_Object[b][PHY_World]))
{
new obj_b = PHY_Object[b][PHY_DynamicObject];
GetDynamicObjectPos(obj_b, x2, y2, z2);
dx = x1 - x2;
dy = y1 - y2;
dz = (PHY_GetObjectMode(a) == PHY_MODE_3D && PHY_GetObjectMode(b) == PHY_MODE_3D) ? (z1 - z2) : (0.0);
dist = (dx * dx) + (dy * dy) + (dz * dz);
maxdist = PHY_Object[a][PHY_Size] + PHY_Object[b][PHY_Size];
if(dist < (maxdist * maxdist))
{
dvx = PHY_Object[a][PHY_VX] - PHY_Object[b][PHY_VX];
dvy = PHY_Object[a][PHY_VY] - PHY_Object[b][PHY_VY];
dvz = PHY_Object[a][PHY_VZ] - PHY_Object[b][PHY_VZ];
mag = dvx * dx + dvy * dy + dvz * dz;
if(mag < 0.0)
{
newvx1 = PHY_Object[a][PHY_VX];
newvy1 = PHY_Object[a][PHY_VY];
newvz1 = PHY_Object[a][PHY_VZ];
newvx2 = PHY_Object[b][PHY_VX];
newvy2 = PHY_Object[b][PHY_VY];
newvz2 = PHY_Object[b][PHY_VZ];
projectVonU(PHY_Object[a][PHY_VX], PHY_Object[a][PHY_VY], PHY_Object[a][PHY_VZ], dx, dy, dz, tmpvx, tmpvy, tmpvz);
newvx1 -= tmpvx;
newvy1 -= tmpvy;
newvz1 -= tmpvz;
projectVonU(PHY_Object[b][PHY_VX], PHY_Object[b][PHY_VY], PHY_Object[b][PHY_VZ], -dx, -dy, -dz, tmpvx, tmpvy, tmpvz);
tmpvx = ((PHY_Object[a][PHY_Mass] - PHY_Object[b][PHY_Mass]) * PHY_Object[a][PHY_VX] + 2 * PHY_Object[b][PHY_Mass] * tmpvx) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
tmpvy = ((PHY_Object[a][PHY_Mass] - PHY_Object[b][PHY_Mass]) * PHY_Object[a][PHY_VY] + 2 * PHY_Object[b][PHY_Mass] * tmpvy) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
tmpvz = ((PHY_Object[a][PHY_Mass] - PHY_Object[b][PHY_Mass]) * PHY_Object[a][PHY_VZ] + 2 * PHY_Object[b][PHY_Mass] * tmpvz) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
newvx1 += tmpvx;
newvy1 += tmpvy;
newvz1 += tmpvz;
projectVonU(PHY_Object[b][PHY_VX], PHY_Object[b][PHY_VY], PHY_Object[b][PHY_VZ], dx, dy, dz, tmpvx, tmpvy, tmpvz);
newvx2 -= tmpvx;
newvy2 -= tmpvy;
newvz2 -= tmpvz;
projectVonU(PHY_Object[a][PHY_VX], PHY_Object[a][PHY_VY], PHY_Object[a][PHY_VZ], -dx, -dy, -dz, tmpvx, tmpvy, tmpvz);
tmpvx = ((PHY_Object[b][PHY_Mass] - PHY_Object[a][PHY_Mass]) * PHY_Object[b][PHY_VX] + 2 * PHY_Object[a][PHY_Mass] * tmpvx) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
tmpvy = ((PHY_Object[b][PHY_Mass] - PHY_Object[a][PHY_Mass]) * PHY_Object[b][PHY_VY] + 2 * PHY_Object[a][PHY_Mass] * tmpvy) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
tmpvz = ((PHY_Object[b][PHY_Mass] - PHY_Object[a][PHY_Mass]) * PHY_Object[b][PHY_VZ] + 2 * PHY_Object[a][PHY_Mass] * tmpvz) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
newvx2 += tmpvx;
newvy2 += tmpvy;
newvz2 += tmpvz;
PHY_Object[a][PHY_VX] = newvx1;
PHY_Object[a][PHY_VY] = newvy1;
PHY_Object[a][PHY_VZ] = newvz1;
PHY_Object[b][PHY_VX] = newvx2;
PHY_Object[b][PHY_VY] = newvy2;
PHY_Object[b][PHY_VZ] = newvz2;
CallLocalFunction("PHY_OnObjectCOLWithObj", "dd", obj_a, obj_b);
}
/*
// Old algorithm, works good only in 2D
dvx = PHY_Object[a][PHY_VX] - PHY_Object[b][PHY_VX];
dvy = PHY_Object[a][PHY_VY] - PHY_Object[b][PHY_VY];
mag = dvx * dx + dvy * dy;
if(mag < 0.0)
{
angle = -atan2(dy, dx);
tmpvx1 = PHY_Object[a][PHY_VX] * floatcos(angle, degrees) - PHY_Object[a][PHY_VY] * floatsin(angle, degrees);
newvy1 = PHY_Object[a][PHY_VX] * floatsin(angle, degrees) + PHY_Object[a][PHY_VY] * floatcos(angle, degrees);
tmpvx2 = PHY_Object[b][PHY_VX] * floatcos(angle, degrees) - PHY_Object[b][PHY_VY] * floatsin(angle, degrees);
newvy2 = PHY_Object[b][PHY_VX] * floatsin(angle, degrees) + PHY_Object[b][PHY_VY] * floatcos(angle, degrees);
if(PHY_Object[a][PHY_Mass] == FLOAT_INFINITY)
newvx2 = -tmpvx2;
else if(PHY_Object[b][PHY_Mass] == FLOAT_INFINITY)
newvx1 = -tmpvx1;
else
{
newvx1 = ((PHY_Object[a][PHY_Mass] - PHY_Object[b][PHY_Mass]) * tmpvx1 + 2 * PHY_Object[b][PHY_Mass] * tmpvx2) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
newvx2 = ((PHY_Object[b][PHY_Mass] - PHY_Object[a][PHY_Mass]) * tmpvx2 + 2 * PHY_Object[a][PHY_Mass] * tmpvx1) / (PHY_Object[a][PHY_Mass] + PHY_Object[b][PHY_Mass]);
}
angle = -angle;
PHY_Object[a][PHY_VX] = newvx1 * floatcos(angle, degrees) - newvy1 * floatsin(angle, degrees);
PHY_Object[a][PHY_VY] = newvx1 * floatsin(angle, degrees) + newvy1 * floatcos(angle, degrees);
PHY_Object[b][PHY_VX] = newvx2 * floatcos(angle, degrees) - newvy2 * floatsin(angle, degrees);
PHY_Object[b][PHY_VY] = newvx2 * floatsin(angle, degrees) + newvy2 * floatcos(angle, degrees);
CallLocalFunction("PHY_OnObjectCOLWithObj", "dd", a, b);
}
*/
}
}
}
}
#if defined COLANDREAS
if(PHY_IsObjectUsingColAndreasBounds(a))
{
if(PHY_GetObjectMode(a) == PHY_MODE_3D)
PHY_UpdateBounds(a, x1, y1, z1);
}
if(PHY_IsObjectUsingColAndreasCollisions(a))
{
mod = floatsqroot(PHY_Object[a][PHY_VX] * PHY_Object[a][PHY_VX] + PHY_Object[a][PHY_VY] * PHY_Object[a][PHY_VY]);
cx = 0.0;
cy = 0.0;
cz = 0.0;
crx = 0.0;
cry = 0.0;
crz = 0.0;
CA_RayCastLineAngle(x1, y1, z1, x1 + PHY_Object[a][PHY_Size] * (PHY_Object[a][PHY_VX] / mod), y1 + PHY_Object[a][PHY_Size] * (PHY_Object[a][PHY_VY] / mod), z1, cx, cy, cz, crx, cry, crz);
if((cx != 0.0 || cy != 0.0 || cz != 0.0) && cx == cx && cy == cy && cz == cz && (crx != 0.0 || cry != 0.0) && crx == crx && cry == cry && crz == crz)
{
angle = atan2(-cry, crx);
newvx1 = (PHY_Object[a][PHY_VX] * floatcos(angle, degrees) - PHY_Object[a][PHY_VY] * floatsin(angle, degrees));
newvy1 = -(PHY_Object[a][PHY_VX] * floatsin(angle, degrees) + PHY_Object[a][PHY_VY] * floatcos(angle, degrees));
angle = -angle;
PHY_Object[a][PHY_VX] = newvx1 * floatcos(angle, degrees) - newvy1 * floatsin(angle, degrees);
PHY_Object[a][PHY_VY] = newvx1 * floatsin(angle, degrees) + newvy1 * floatcos(angle, degrees);
angle = angle + (newvy1 > 0 ? 90.0 : -90.0);
x1 = cx + (PHY_Object[a][PHY_Size] + 0.001) * floatcos(angle, degrees);
y1 = cy + (PHY_Object[a][PHY_Size] + 0.001) * floatsin(angle, degrees);
CallLocalFunction("PHY_OnObjectCollideWithSAWorld", "dfff", obj_a, cx, cy, cz);
}
}
#endif
if(!PHY_IsObjectGhostWithWalls(a))
{
foreach(new w : ITER_Wall)
{
if(PHY_Wall[w][PHY_Created])
{
if((!PHY_Object[a][PHY_World] || !PHY_Wall[w][PHY_World] || PHY_Object[a][PHY_World] == PHY_Wall[w][PHY_World]))
{
//dist = (y1 - PHY_Wall[w][PHY_M] * x1 - PHY_Wall[w][PHY_Q])/floatsqroot(1 + PHY_Wall[w][PHY_M] * PHY_Wall[w][PHY_M]);
//dist = (PHY_Wall[w][PHY_A] * x1 + PHY_Wall[w][PHY_B] * y1 + PHY_Wall[w][PHY_C])/floatsqroot(PHY_Wall[w][PHY_A] * PHY_Wall[w][PHY_A] + PHY_Wall[w][PHY_B] * PHY_Wall[w][PHY_B]);
angle = PHY_Wall[w][PHY_ANG];
if(PHY_Wall[w][PHY_Z1] - PHY_Object[a][PHY_Size] < z1 < PHY_Wall[w][PHY_Z2] + PHY_Object[a][PHY_Size] &&
(check_segment_intersection(PHY_Wall[w][PHY_X1], PHY_Wall[w][PHY_Y1], PHY_Wall[w][PHY_X2], PHY_Wall[w][PHY_Y2], x1, y1, PHY_Object[a][PHY_Size], xclosest, yclosest) || /* && floatabs(dist) < PHY_Object[a][PHY_Size]*/
check_segment_intersection(PHY_Wall[w][PHY_X1], PHY_Wall[w][PHY_Y1], PHY_Wall[w][PHY_X2], PHY_Wall[w][PHY_Y2], (x + x1)/2, (y + y1)/2, PHY_Object[a][PHY_Size], xclosest, yclosest)))
{
//mag = y1 + PHY_Object[a][PHY_Size] * floatcos(-moveangle, degrees) - (x1 + PHY_Object[a][PHY_Size] * floatsin(-moveangle, degrees)) * PHY_Wall[w][PHY_M] - PHY_Wall[w][PHY_Q];
//mag = PHY_Wall[w][PHY_A] * (x1 + PHY_Object[a][PHY_Size] * floatsin(-moveangle, degrees)) + PHY_Wall[w][PHY_B] * (y1 + PHY_Object[a][PHY_Size] * floatcos(-moveangle, degrees)) + PHY_Wall[w][PHY_C];
//if((dist >= 0) ? (mag <= 0) : (mag >= 0))
newvx1 = PHY_Wall[w][PHY_BounceConst] * (PHY_Object[a][PHY_VX] * floatcos(angle, degrees) - PHY_Object[a][PHY_VY] * floatsin(angle, degrees));
newvy1 = -PHY_Wall[w][PHY_BounceConst] * (PHY_Object[a][PHY_VX] * floatsin(angle, degrees) + PHY_Object[a][PHY_VY] * floatcos(angle, degrees));
angle = -angle;
PHY_Object[a][PHY_VX] = newvx1 * floatcos(angle, degrees) - newvy1 * floatsin(angle, degrees);
PHY_Object[a][PHY_VY] = newvx1 * floatsin(angle, degrees) + newvy1 * floatcos(angle, degrees);
angle = angle + (newvy1 > 0 ? 90.0 : -90.0);
x1 = xclosest + (PHY_Object[a][PHY_Size] + 0.001) * floatcos(angle, degrees);
y1 = yclosest + (PHY_Object[a][PHY_Size] + 0.001) * floatsin(angle, degrees);
CallLocalFunction("PHY_OnObjectCollideWithWall", "dd", obj_a, w);
}
}
}
else
Iter_SafeRemove(ITER_Wall, w, w);
}
}
if(!PHY_IsObjectGhostWithCylinders(a))
{
foreach(new c : ITER_Cylinder)
{
if(PHY_Cylinder[c][PHY_Created])
{
if((!PHY_Object[a][PHY_World] || !PHY_Cylinder[c][PHY_World] || PHY_Object[a][PHY_World] == PHY_Cylinder[c][PHY_World]))
{
if(PHY_Cylinder[c][PHY_Z1] - PHY_Object[a][PHY_Size] < z1 < PHY_Cylinder[c][PHY_Z2] + PHY_Object[a][PHY_Size])
{
x2 = PHY_Cylinder[c][PHY_X];
y2 = PHY_Cylinder[c][PHY_Y];
dx = x1 - x2;
dy = y1 - y2;
dist = (dx * dx) + (dy * dy);
maxdist = PHY_Object[a][PHY_Size] + PHY_Cylinder[c][PHY_Size];
if(dist < (maxdist * maxdist))
{
mag = PHY_Object[a][PHY_VX] * dx + PHY_Object[a][PHY_VY] * dy;
if(mag < 0.0)
{
angle = -atan2(dy, dx);
newvx1 = -PHY_Cylinder[c][PHY_BounceConst] * (PHY_Object[a][PHY_VX] * floatcos(angle, degrees) - PHY_Object[a][PHY_VY] * floatsin(angle, degrees));
newvy1 = PHY_Cylinder[c][PHY_BounceConst] * (PHY_Object[a][PHY_VX] * floatsin(angle, degrees) + PHY_Object[a][PHY_VY] * floatcos(angle, degrees));
angle = -angle;
PHY_Object[a][PHY_VX] = newvx1 * floatcos(angle, degrees) - newvy1 * floatsin(angle, degrees);
PHY_Object[a][PHY_VY] = newvx1 * floatsin(angle, degrees) + newvy1 * floatcos(angle, degrees);
CallLocalFunction("PHY_OnObjectCollideWithCylinder", "dd", obj_a, c);
}
}
}
}
}
else
Iter_SafeRemove(ITER_Cylinder, c, c);
}
}
if(PHY_IsObjectCollidingWithPlayers(a))
{
foreach(new i : Player)
{
if((!PHY_Object[a][PHY_World] || !PHY_Player[i][PHY_World] || PHY_Object[a][PHY_World] == PHY_Player[i][PHY_World]))
{
GetPlayerPos(i, x2, y2, z2);
if(z2 - PHY_Object[a][PHY_PlayerLowZ] - PHY_Object[a][PHY_Size] < z1 < z2 + PHY_Object[a][PHY_PlayerHighZ] + PHY_Object[a][PHY_Size])
{
dx = x1 - x2;
dy = y1 - y2;
dist = (dx * dx) + (dy * dy);
maxdist = PHY_Object[a][PHY_Size] + PHY_Object[a][PHY_PlayerDist];
if(dist < (maxdist * maxdist))
{
mag = PHY_Object[a][PHY_VX] * dx + PHY_Object[a][PHY_VY] * dy;
if(mag < 0.0)
{
angle = -atan2(dy, dx);
newvx1 = -PHY_Object[a][PHY_PlayerConst] * (PHY_Object[a][PHY_VX] * floatcos(angle, degrees) - PHY_Object[a][PHY_VY] * floatsin(angle, degrees));
newvy1 = PHY_Object[a][PHY_PlayerConst] * (PHY_Object[a][PHY_VX] * floatsin(angle, degrees) + PHY_Object[a][PHY_VY] * floatcos(angle, degrees));
angle = -angle;
PHY_Object[a][PHY_VX] = newvx1 * floatcos(angle, degrees) - newvy1 * floatsin(angle, degrees);
PHY_Object[a][PHY_VY] = newvx1 * floatsin(angle, degrees) + newvy1 * floatcos(angle, degrees);
CallLocalFunction("PHY_OnObjectCollideWithPlayer", "dd", obj_a, i);
}
}
}
}
}
}
moveangle = atan2(PHY_Object[a][PHY_VY], PHY_Object[a][PHY_VX]) - 90.0;
speed = floatsqroot(PHY_Object[a][PHY_VX] * PHY_Object[a][PHY_VX] + PHY_Object[a][PHY_VY] * PHY_Object[a][PHY_VY]);
if(PHY_GetObjectFriction(a) != 0 && z1 == PHY_Object[a][PHY_LowZBound])
{
speed -= PHY_Object[a][PHY_Friction] * (PHY_TIMER_INTERVAL/1000.0);
if(speed < 0.001)
speed = 0;
PHY_Object[a][PHY_VX] = speed * floatsin(-moveangle, degrees);
PHY_Object[a][PHY_VY] = speed * floatcos(-moveangle, degrees);
}
if(PHY_GetObjectAirResistance(a) != 0)
{
PHY_Object[a][PHY_VX] -= PHY_Object[a][PHY_VX] * PHY_Object[a][PHY_AirResistance] * (PHY_TIMER_INTERVAL/1000.0);
PHY_Object[a][PHY_VY] -= PHY_Object[a][PHY_VY] * PHY_Object[a][PHY_AirResistance] * (PHY_TIMER_INTERVAL/1000.0);
PHY_Object[a][PHY_VZ] -= PHY_Object[a][PHY_VZ] * PHY_Object[a][PHY_AirResistance] * (PHY_TIMER_INTERVAL/1000.0);
}
if(PHY_IsObjectRolling(a) && speed > 0.0)
if(PHY_IsObjectRollingModeAdvanced(a))
PHY_ApplyQuaternionsRotation(a, speed, moveangle);
else
PHY_ApplyRotation(a, speed, moveangle);
}
PHY_Object[a][PHY_VX] += PHY_Object[a][PHY_AX];
PHY_Object[a][PHY_VY] += PHY_Object[a][PHY_AY];
PHY_Object[a][PHY_VZ] += PHY_Object[a][PHY_AZ];
SetDynamicObjectPos(obj_a, x1, y1, z1);
CallLocalFunction("PHY_OnObjectUpdate", "d", obj_a);
}
else
Iter_SafeRemove(ITER_Object, a, a);
}
return 1;
}
/* Starts using physics for objectid.
modelid - object's modelid, used to get its size with modelsizes include.
mass - object's mass, it is like its weight and is used in collisions.
size - object's sphere radius, taken from modelsizes.inc by default.
mode - PHY_MODE_3D or PHY_MODE_2D. */
stock PHY_InitObject(objectid, modelid = 0, Float:mass = 1.0, Float:size = FLOAT_NAN, mode = PHY_MODE_3D)
{
if(!IsValidDynamicObject(objectid)) return false;
new
handleid = Iter_Free(ITER_Object);
if(handleid != ITER_NONE)
{
PHY_Object[handleid][PHY_Properties] = PHY_OBJECT_USED | (mode ? PHY_OBJECT_MODE : 0);
PHY_Object[handleid][PHY_DynamicObject] = objectid;
PHY_Object[handleid][PHY_Mass] = mass;
PHY_Object[handleid][PHY_World] = 0;
PHY_Object[handleid][PHY_VX] = 0;
PHY_Object[handleid][PHY_VY] = 0;
PHY_Object[handleid][PHY_VZ] = 0;
PHY_Object[handleid][PHY_Gravity] = 0;
new
Float:x, Float:y, Float:z;
GetDynamicObjectPos(objectid, x, y, z);
PHY_Object[handleid][PHY_LowZBound] = z;
PHY_Object[handleid][PHY_HighZBound] = FLOAT_INFINITY;
PHY_Object[handleid][PHY_BoundConst] = 0;
if(size != size)
{
if(modelid)
PHY_Object[handleid][PHY_Size] = GetColSphereRadius(modelid);
}
else
PHY_Object[handleid][PHY_Size] = size;
Iter_Add(ITER_Object, handleid);
return true;
}
return false;
}
/* Stops using physics for objectid (doesn't destroy it). */
stock PHY_DeleteObject(objectid)
{
if(PHY_IsHandleValid(objectid))
{
PHY_Object[objectid][PHY_Properties] = 0;
// Iter_Remove(ITER_Object, objectid);
}
return 1;
}
/* Moves the object with vx, vy, vz velocities. */
stock PHY_SetObjectVelocity(objectid, Float:vx, Float:vy, Float:vz = 0.0)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
PHY_Object[objectid][PHY_VX] = vx;
PHY_Object[objectid][PHY_VY] = vy;
PHY_Object[objectid][PHY_VZ] = vz;
return 1;
}
}
return 0;
}
/* Self-explanatory */
stock PHY_GetObjectVelocity(objectid, &Float:vx, &Float:vy, &Float:vz)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
vx = PHY_Object[objectid][PHY_VX];
vy = PHY_Object[objectid][PHY_VY];
vz = PHY_Object[objectid][PHY_VZ];
return 1;
}
}
return 0;
}
/* Sets the object's acceleration. */
stock PHY_SetObjectAcceleration(objectid, Float:ax, Float:ay, Float:az = 0.0)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
PHY_Object[objectid][PHY_AX] = ax;
PHY_Object[objectid][PHY_AY] = ay;
PHY_Object[objectid][PHY_AZ] = az;
return 1;
}
}
return 0;
}
/* Self-explanatory */
stock PHY_GetObjectAcceleration(objectid, &Float:ax, &Float:ay, &Float:az)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
ax = PHY_Object[objectid][PHY_AX];
ay = PHY_Object[objectid][PHY_AY];
az = PHY_Object[objectid][PHY_AZ];
return 1;
}
}
return 0;
}
/* Self-explanatory */
stock PHY_GetObjectSpeed(objectid, &Float:speed, _3D = 0)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
speed = floatsqroot(PHY_Object[objectid][PHY_VX] * PHY_Object[objectid][PHY_VX] + PHY_Object[objectid][PHY_VY] * PHY_Object[objectid][PHY_VY] + _3D ? (PHY_Object[objectid][PHY_VZ] * PHY_Object[objectid][PHY_VZ]) : 0.0);
return 1;
}
}
return 0;
}
/* Self-explanatory */
stock PHY_GetObjectMoveAngle(objectid, &Float:moveangle)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
moveangle = atan2(PHY_Object[objectid][PHY_VY], PHY_Object[objectid][PHY_VX]) - 90.0;
return 1;
}
}
return 0;
}
/* Sets ColAndreas mode for the object. Modes: 0 none, 1 collisions + z bounds, 2 collisions only, 3 z bounds only*/
stock PHY_UseColAndreas(objectid, mode = 1)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
new Float:x, Float:y, Float:z;
GetDynamicObjectPos(objectid, x, y, z);
switch(mode)
{
case 0:
{
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_COLANDREAS_BOUNDS;
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_COLANDREAS_COLLS;
}
case 1:
{
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_COLANDREAS_BOUNDS;
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_COLANDREAS_COLLS;
PHY_UpdateBounds(objectid, x, y, z);
}
case 2:
{
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_COLANDREAS_BOUNDS;
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_COLANDREAS_COLLS;
}
case 3:
{
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_COLANDREAS_BOUNDS;
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_COLANDREAS_COLLS;
PHY_UpdateBounds(objectid, x, y, z);
}
}
return 1;
}
}
return 0;
}
/* Starts rolling the object when it moves of toggle = 1 or stops if toggle = 0.
rollingmode = PHY_ROLLING_MODE_DEFAULT (Euler angles) or PHY_ROLLING_MODE_ADVANCED (Quaternions)
When using advanced rolling mode, if you manually use SetDynamicObjectRot in your script, it is adviced to
call PHY_RollObject again, in order to recalculate its quaternion angles.*/
stock PHY_RollObject(objectid, toggle = 1, rollingmode = PHY_ROLLING_MODE_DEFAULT)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
if(toggle)
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_ROLL;
else
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_ROLL;
if(rollingmode == PHY_ROLLING_MODE_ADVANCED){
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_ROLLING_MODE;
new
Float:rx, Float:ry, Float:rz, obj_a = PHY_Object[objectid][PHY_DynamicObject];
GetDynamicObjectRot(obj_a, rx, ry, rz);
PHY_GetRotationQuaternion(rx, ry, rz, PHY_Object[objectid][PHY_QW], PHY_Object[objectid][PHY_QX], PHY_Object[objectid][PHY_QY], PHY_Object[objectid][PHY_QZ]);
}
else
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_ROLLING_MODE;
return 1;
}
}
return 0;
}
/* Applies friction to the object when it moves on the floor. */
stock PHY_SetObjectFriction(objectid, Float:friction)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
if(friction >= 0.0)
PHY_Object[objectid][PHY_Friction] = friction;
return 1;
}
}
return 0;
}
/* Applies air resistance to the object when it moves. */
stock PHY_SetObjectAirResistance(objectid, Float:resistance)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
if(0.0 <= resistance <= 1.0)
PHY_Object[objectid][PHY_AirResistance] = resistance;
return 1;
}
}
return 0;
}
/* Limits the object's Z position.
low - The lowest Z that the object can have (you can use FLOAT_NEG_INFINITY). If it is set to NaN it doesn't change.
high - The highest Z that the object can have (you can use FLOAT_INFINITY). If it is set to NaN it doesn't change.
(When you use PHY_InitObject lowest Z is set to the current object's Z and highest Z to FLOAT_INFINITY.
constant - It should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it bounces. It could be a middle ground.*/
stock PHY_SetObjectZBound(objectid, Float:low = FLOAT_NAN, Float:high = FLOAT_NAN, Float:constant = 0.0)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
if(low == low)
PHY_Object[objectid][PHY_LowZBound] = low;
if(high == high)
PHY_Object[objectid][PHY_HighZBound] = high;
PHY_Object[objectid][PHY_BoundConst] = constant;
return 1;
}
}
return 0;
}
/* Sets the gravity's acceleration that the object is subjected to. */
stock PHY_SetObjectGravity(objectid, Float:gravity)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
PHY_Object[objectid][PHY_Gravity] = gravity;
return 1;
}
}
return 0;
}
/* Object and walls collide only if the are in the same world or one of them is in the world 0 (default). */
stock PHY_SetObjectWorld(objectid, world)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
PHY_Object[objectid][PHY_World] = world;
return 1;
}
}
return 0;
}
/* Toggles object's collisions with players.
- constant - It should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it bounces. It could be a middle ground.
- distoffset - The distance at which the object collides with the player.
- zoffsetlow/zoffsethigh - The max Z distance (downward/upward) at which the object collides with the player. */
stock PHY_ToggleObjectPlayerColls(objectid, toggle = 1, Float:constant = 1.0, Float:distoffset = 0.8, Float:zoffsetlow = 1.0, Float:zoffsethigh = 1.0)
{
if(PHY_IsHandleValid(objectid))
{
if(PHY_Object[objectid][PHY_Properties] & PHY_OBJECT_USED)
{
if(toggle)
{
PHY_Object[objectid][PHY_Properties] |= PHY_OBJECT_PLAYER_COLLISIONS;
PHY_Object[objectid][PHY_PlayerConst] = constant;
PHY_Object[objectid][PHY_PlayerDist] = distoffset;
PHY_Object[objectid][PHY_PlayerLowZ] = zoffsetlow;
PHY_Object[objectid][PHY_PlayerHighZ] = zoffsethigh;
}
else
PHY_Object[objectid][PHY_Properties] &= ~PHY_OBJECT_PLAYER_COLLISIONS;
return 1;
}
}
return 0;
}
/* Used internally to rotate the objects */
stock PHY_ApplyRotation(objectid, Float:speed, Float:moveangle)
{
if(PHY_IsHandleValid(objectid))
{
new
Float:rx, Float:ry, Float:rz;
new a_obj = PHY_Object[objectid][PHY_DynamicObject];
GetDynamicObjectRot(a_obj, rx, ry, rz);
rx -= speed * (PHY_TIMER_INTERVAL/1000.0) * (180.0/3.14159) / PHY_Object[objectid][PHY_Size];
if(rx < 0.0)
rx += 360.0;
rz = moveangle;
SetDynamicObjectRot(a_obj, rx, ry, rz);
}
return 1;
}
/* New mode of rolling, based on Quaternions */
stock PHY_ApplyQuaternionsRotation(objectid, Float:speed, Float:moveangle)
{
if(PHY_IsHandleValid(objectid))
{
new
Float:rx, Float:ry, Float:rz,
Float:dx, Float:dy,
Float:q_roll[4],
Float:vx, Float:vy, Float:vz,
Float:quat[4];
dx = floatsin(-moveangle, degrees);
dy = floatcos(-moveangle, degrees);
PHY_vectorcrossp(dx, dy, 0.0, 0.0, 0.0, 1.0, vx, vy, vz);
PHY_QuatFromAxisAngle(vx, vy, vz, speed * (PHY_TIMER_INTERVAL/1000.0) * (180.0/3.14159) / PHY_Object[objectid][PHY_Size], q_roll[0], q_roll[1], q_roll[2], q_roll[3]);
quat[0] = PHY_Object[objectid][PHY_QW];
quat[1] = PHY_Object[objectid][PHY_QX];
quat[2] = PHY_Object[objectid][PHY_QY];
quat[3] = PHY_Object[objectid][PHY_QZ];
PHY_GetQuatProduct(quat, q_roll, quat);
PHY_Object[objectid][PHY_QW] = quat[0];
PHY_Object[objectid][PHY_QX] = quat[1];
PHY_Object[objectid][PHY_QY] = quat[2];
PHY_Object[objectid][PHY_QZ] = quat[3];
PHY_GetQuaternionAngles(quat[0], quat[1], quat[2], quat[3], rx, ry, rz);
new a_obj = PHY_Object[objectid][PHY_DynamicObject];
SetDynamicObjectRot(a_obj, rx, ry, rz);
}
return 1;
}
/* Creates a collision wall (straight line) from A(x1, y1) to B(x2, y2).
constant should be from 0.0 to 1.0. If it is 1.0 the object doesn't lose velocity,
if it is 0.0 the object stops when it collides.
low is the lowest wall's Z, high is the highest. If they're set to default the wall is like infinitely high.*/
stock PHY_CreateWall(Float:x1, Float:y1, Float:x2, Float:y2, Float:constant = 1.0, Float:low = FLOAT_NEG_INFINITY, Float:high = FLOAT_INFINITY)
{