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
|
C++RAW
#include "UnityPrefix.h"
#include "Configuration/UnityConfigure.h"
#include "Runtime/Scripting/ScriptingExportUtility.h"
#include "Runtime/Scripting/ScriptingUtility.h"
#include "Runtime/Scripting/ScriptingManager.h"
#include "Runtime/Physics2D/Physics2DManager.h"
#include "Runtime/Physics2D/Physics2DMaterial.h"
#include "Runtime/Physics2D/Physics2DSettings.h"
#include "Runtime/Physics2D/RigidBody2D.h"
#include "Runtime/Physics2D/PolygonCollider2D.h"
#include "Runtime/Physics2D/SpriteCollider2D.h"
#include "Runtime/Physics2D/CircleCollider2D.h"
#include "Runtime/Physics2D/BoxCollider2D.h"
#include "Runtime/Physics2D/EdgeCollider2D.h"
#include "Runtime/Physics2D/Joint2D.h"
#include "Runtime/Physics2D/SpringJoint2D.h"
#include "Runtime/Physics2D/DistanceJoint2D.h"
#include "Runtime/Physics2D/HingeJoint2D.h"
#include "Runtime/Physics2D/SliderJoint2D.h"
#include "Runtime/Geometry/Ray.h"
#include "Runtime/Graphics/SpriteFrame.h"
#include "Runtime/Graphics/Polygon2D.h"
#include "Runtime/Misc/GameObjectUtility.h"
CSRAW
using System;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace UnityEngine
{
CONDITIONAL ENABLE_2D_PHYSICS
NONSEALED_CLASS Physics2D
CSRAW public const int IgnoreRaycastLayer = 1 << 2;
CSRAW public const int DefaultRaycastLayers = ~IgnoreRaycastLayer;
CSRAW public const int AllLayers = ~0;
// --------------------------------------------------------------------------
THREAD_SAFE
CUSTOM_PROP static int velocityIterations { return GetPhysics2DSettings().GetVelocityIterations (); } { SCRIPTINGAPI_THREAD_CHECK(SetVelocityIterations) return GetPhysics2DSettings().SetVelocityIterations (value); }
CUSTOM_PROP static int positionIterations { return GetPhysics2DSettings().GetPositionIterations (); } { SCRIPTINGAPI_THREAD_CHECK(SetPositionIterations) return GetPhysics2DSettings().SetPositionIterations (value); }
CUSTOM_PROP static Vector2 gravity { return GetPhysics2DSettings().GetGravity (); } { SCRIPTINGAPI_THREAD_CHECK(set_gravity) return GetPhysics2DSettings().SetGravity (value); }
CUSTOM_PROP static bool raycastsHitTriggers { return GetPhysics2DSettings().GetRaycastsHitTriggers (); } { SCRIPTINGAPI_THREAD_CHECK(SetRaycastsHitTriggers) return GetPhysics2DSettings().SetRaycastsHitTriggers (value); }
// --------------------------------------------------------------------------
CUSTOM static public void IgnoreLayerCollision (int layer1, int layer2, bool ignore = true)
{
GetPhysics2DSettings().IgnoreCollision(layer1, layer2, ignore);
}
CUSTOM static public bool GetIgnoreLayerCollision (int layer1, int layer2)
{
return GetPhysics2DSettings().GetIgnoreCollision(layer1, layer2);
}
// --------------------------------------------------------------------------
C++RAW
// Create a managed array of native ray-cast hits.
static ScriptingArrayPtr CreateManagedRaycastArrayFromNative (RaycastHit2D* nativeHits, size_t size)
{
// Return an empty array if no hits.
if (size == 0)
return CreateEmptyStructArray (MONO_COMMON.raycastHit2D);
// Generate scripting array.
ScriptingArrayPtr scriptArray = CreateScriptingArray<RaycastHit2D> (MONO_COMMON.raycastHit2D, size);
// Transfer elements.
RaycastHit2D* scriptHit = Scripting::GetScriptingArrayStart<RaycastHit2D> (scriptArray);
for (size_t i = 0; i < size; ++i, ++scriptHit, ++nativeHits)
{
// Transfer members.
scriptHit->point = nativeHits->point;
scriptHit->normal = nativeHits->normal;
scriptHit->fraction = nativeHits->fraction;
scriptHit->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (nativeHits->collider));
}
return scriptArray;
}
CSRAW
// --------------------------------------------------------------------------
CUSTOM private static void Internal_Linecast (Vector2 start, Vector2 end, int layerMask, float minDepth, float maxDepth, out RaycastHit2D raycastHit)
{
if (GetPhysics2DManager ().Linecast (start, end, layerMask, minDepth, maxDepth, raycastHit, 1) == 1)
raycastHit->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHit->collider));
}
// Returns the first hit along the specified line.
CSRAW public static RaycastHit2D Linecast (Vector2 start, Vector2 end, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
RaycastHit2D raycastHit;
Internal_Linecast (start, end, layerMask, minDepth, maxDepth, out raycastHit);
return raycastHit;
}
// Returns all hits along the specified line.
CUSTOM public static RaycastHit2D[] LinecastAll (Vector2 start, Vector2 end, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
dynamic_array<RaycastHit2D> raycastHits(kMemTempAlloc);
if (GetPhysics2DManager ().LinecastAll (start, end, layerMask, minDepth, maxDepth, &raycastHits) == 0)
return CreateEmptyStructArray (MONO_COMMON.raycastHit2D);
return CreateManagedRaycastArrayFromNative (raycastHits.data (), raycastHits.size ());
}
// Returns all hits along the line (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int LinecastNonAlloc (Vector2 start, Vector2 end, RaycastHit2D[] results, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
RaycastHit2D* raycastHits = Scripting::GetScriptingArrayStart<RaycastHit2D>(results);
const int resultCount = GetPhysics2DManager ().Linecast (start, end, layerMask, minDepth, maxDepth, raycastHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i, ++raycastHits)
raycastHits->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHits->collider));
return resultCount;
}
CUSTOM private static void Internal_Raycast (Vector2 origin, Vector2 direction, float distance, int layerMask, float minDepth, float maxDepth, out RaycastHit2D raycastHit)
{
if (GetPhysics2DManager ().Raycast (origin, direction, distance, layerMask, minDepth, maxDepth, raycastHit, 1) == 1)
raycastHit->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHit->collider));
}
// Returns the first hit along the ray.
CSRAW public static RaycastHit2D Raycast (Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
RaycastHit2D raycastHit;
Internal_Raycast (origin, direction, distance, layerMask, minDepth, maxDepth, out raycastHit);
return raycastHit;
}
// Returns all hits along the ray.
CUSTOM public static RaycastHit2D[] RaycastAll (Vector2 origin, Vector2 direction, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
dynamic_array<RaycastHit2D> raycastHits(kMemTempAlloc);
if (GetPhysics2DManager ().RaycastAll (origin, direction, distance, layerMask, minDepth, maxDepth, &raycastHits) == 0)
return CreateEmptyStructArray(MONO_COMMON.raycastHit2D);
return CreateManagedRaycastArrayFromNative (raycastHits.data (), raycastHits.size ());
}
// Returns all hits along the ray (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int RaycastNonAlloc (Vector2 origin, Vector2 direction, RaycastHit2D[] results, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
RaycastHit2D* raycastHits = Scripting::GetScriptingArrayStart<RaycastHit2D>(results);
const int resultCount = GetPhysics2DManager ().Raycast (origin, direction, distance, layerMask, minDepth, maxDepth, raycastHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i, ++raycastHits)
raycastHits->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHits->collider));
return resultCount;
}
// --------------------------------------------------------------------------
CUSTOM private static void Internal_GetRayIntersection (Ray ray, float distance, int layerMask, out RaycastHit2D raycastHit)
{
if (GetPhysics2DManager ().GetRayIntersection (ray.GetOrigin (), ray.GetDirection (), distance, layerMask, raycastHit, 1) == 1)
raycastHit->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHit->collider));
}
// Returns the first hit intersecting the 3D ray.
CSRAW public static RaycastHit2D GetRayIntersection (Ray ray, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers)
{
RaycastHit2D raycastHit;
Internal_GetRayIntersection (ray, distance, layerMask, out raycastHit);
return raycastHit;
}
// Returns all hits intersecting the 3D ray.
CUSTOM public static RaycastHit2D[] GetRayIntersectionAll (Ray ray, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers)
{
dynamic_array<RaycastHit2D> raycastHits(kMemTempAlloc);
if (GetPhysics2DManager ().GetRayIntersectionAll (ray.GetOrigin (), ray.GetDirection (), distance, layerMask, &raycastHits) == 0)
return CreateEmptyStructArray(MONO_COMMON.raycastHit2D);
return CreateManagedRaycastArrayFromNative (raycastHits.data (), raycastHits.size ());
}
// Returns all hits intersecting the 3D ray (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int GetRayIntersectionNonAlloc (Ray ray, RaycastHit2D[] results, float distance = Mathf.Infinity, int layerMask = DefaultRaycastLayers)
{
RaycastHit2D* raycastHits = Scripting::GetScriptingArrayStart<RaycastHit2D>(results);
const int resultCount = GetPhysics2DManager ().GetRayIntersection (ray.GetOrigin (), ray.GetDirection (), distance, layerMask, raycastHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i, ++raycastHits)
raycastHits->collider = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (raycastHits->collider));
return resultCount;
}
// --------------------------------------------------------------------------
// Returns a collider overlapping the point.
CUSTOM public static Collider2D OverlapPoint (Vector2 point, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D* collider;
const int resultCount = GetPhysics2DManager ().OverlapPoint (point, layerMask, minDepth, maxDepth, &collider, 1);
if (resultCount == 0)
return SCRIPTING_NULL;
return Scripting::ScriptingWrapperFor (collider);
}
// Returns all colliders overlapping the point.
CUSTOM public static Collider2D[] OverlapPointAll (Vector2 point, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
dynamic_array<Collider2D*> colliderHits(kMemTempAlloc);
GetPhysics2DManager ().OverlapPointAll (point, layerMask, minDepth, maxDepth, &colliderHits);
// Generate scripting array.
return CreateScriptingArrayFromUnityObjects(colliderHits, ScriptingClassFor(Collider2D));
}
// Returns all colliders overlapping the point (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int OverlapPointNonAlloc (Vector2 point, Collider2D[] results, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D** colliderHits = Scripting::GetScriptingArrayStart<Collider2D*>(results);
const int resultCount = GetPhysics2DManager ().OverlapPoint (point, layerMask, minDepth, maxDepth, colliderHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i)
colliderHits[i] = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (colliderHits[i]));
return resultCount;
}
// Returns a collider overlapping the circle.
CUSTOM public static Collider2D OverlapCircle (Vector2 point, float radius, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D* collider;
const int resultCount = GetPhysics2DManager ().OverlapCircle (point, radius, layerMask, minDepth, maxDepth, &collider, 1);
if (resultCount == 0)
return SCRIPTING_NULL;
return Scripting::ScriptingWrapperFor (collider);
}
// Returns all colliders overlapping the circle.
CUSTOM public static Collider2D[] OverlapCircleAll (Vector2 point, float radius, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
dynamic_array<Collider2D*> colliderHits(kMemTempAlloc);
GetPhysics2DManager ().OverlapCircleAll (point, radius, layerMask, minDepth, maxDepth, &colliderHits);
// Generate scripting array.
return CreateScriptingArrayFromUnityObjects(colliderHits, ScriptingClassFor(Collider2D));
}
// Returns all colliders overlapping the circle (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int OverlapCircleNonAlloc (Vector2 point, float radius, Collider2D[] results, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D** colliderHits = Scripting::GetScriptingArrayStart<Collider2D*>(results);
const int resultCount = GetPhysics2DManager ().OverlapCircle (point, radius, layerMask, minDepth, maxDepth, colliderHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i)
colliderHits[i] = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (colliderHits[i]));
return resultCount;
}
// Returns a collider overlapping the area.
CUSTOM public static Collider2D OverlapArea (Vector2 pointA, Vector2 pointB, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D* collider;
const int resultCount = GetPhysics2DManager ().OverlapArea (pointA, pointB, layerMask, minDepth, maxDepth, &collider, 1);
if (resultCount == 0)
return SCRIPTING_NULL;
return Scripting::ScriptingWrapperFor (collider);
}
// Returns all colliders overlapping the area.
CUSTOM public static Collider2D[] OverlapAreaAll (Vector2 pointA, Vector2 pointB, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
dynamic_array<Collider2D*> colliderHits(kMemTempAlloc);
GetPhysics2DManager ().OverlapAreaAll (pointA, pointB, layerMask, minDepth, maxDepth, &colliderHits);
// Generate scripting array.
return CreateScriptingArrayFromUnityObjects(colliderHits, ScriptingClassFor(Collider2D));
}
// Returns all colliders overlapping the area (limited by the size of the array). This does not produce any garbage.
CUSTOM public static int OverlapAreaNonAlloc (Vector2 pointA, Vector2 pointB, Collider2D[] results, int layerMask = DefaultRaycastLayers, float minDepth = -Mathf.Infinity, float maxDepth = Mathf.Infinity)
{
Collider2D** colliderHits = Scripting::GetScriptingArrayStart<Collider2D*>(results);
const int resultCount = GetPhysics2DManager ().OverlapArea (pointA, pointB, layerMask, minDepth, maxDepth, colliderHits, GetScriptingArraySize(results));
for (size_t i = 0; i < resultCount; ++i)
colliderHits[i] = reinterpret_cast<Collider2D*>(ScriptingGetObjectReference (colliderHits[i]));
return resultCount;
}
END
// NOTE: must match memory layout of native RaycastHit2D
CONDITIONAL ENABLE_2D_PHYSICS
STRUCT RaycastHit2D
CSRAW private Vector2 m_Point;
CSRAW private Vector2 m_Normal;
CSRAW private float m_Fraction;
#if UNITY_WINRT
CSRAW private int m_ColliderHandle;
#else
CSRAW private Collider2D m_Collider;
#endif
CSRAW public Vector2 point { get { return m_Point; } set { m_Point = value; } }
CSRAW public Vector2 normal { get { return m_Normal; } set { m_Normal = value; } }
CSRAW public float fraction { get { return m_Fraction; } set { m_Fraction = value; } }
CONDITIONAL !UNITY_WINRT
CSRAW public Collider2D collider { get { return m_Collider; } }
CONDITIONAL UNITY_WINRT
CSRAW public Collider2D collider { get { return UnityEngineInternal.ScriptingUtils.GCHandleToObject<Collider2D>(m_ColliderHandle); } }
CSRAW public Rigidbody2D rigidbody { get { return collider != null ? collider.attachedRigidbody : null; } }
CSRAW public Transform transform { get {
Rigidbody2D body = rigidbody;
if (body != null)
return body.transform;
else if (collider != null)
return collider.transform;
else
return null;
} }
// Implicitly convert a hit to a boolean based upon whether a collider reference exists or not.
CSRAW public static implicit operator bool (RaycastHit2D hit) { return hit.collider != null; }
// Compare the hit by fraction along the ray. If no colliders exist then fraction is moved "up". This allows sorting an array of sparse results.
CSRAW public int CompareTo(RaycastHit2D other) { if (collider == null) return 1; if (other.collider == null) return -1; return fraction.CompareTo(other.fraction); }
END
// [[Rigidbody2D]] interpolation mode.
ENUM RigidbodyInterpolation2D
// No Interpolation.
None = 0,
// Interpolation will always lag a little bit behind but can be smoother than extrapolation.
Interpolate = 1,
// Extrapolation will predict the position of the rigidbody based on the current velocity.
Extrapolate = 2
END
// [[Rigidbody2D]] sleeping mode.
ENUM RigidbodySleepMode2D
// Never sleep.
NeverSleep = 0,
// Start the rigid body awake.
StartAwake = 1,
// Start the rigid body asleep.
StartAsleep = 2
END
// [[Rigidbody2D]] collision detection mode.
ENUM CollisionDetectionMode2D
// Provides standard (and least expensive) collision detection suitable for most circumstances.
None = 0,
// Provides the most accurate collision detection to present tunnelling but is more expensive. Use for vert fast moving objects only.
Continuous = 1
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS Rigidbody2D : Component
// The linear velocity vector of the object.
AUTO_PROP Vector2 velocity GetVelocity SetVelocity
// The angular velocity vector of the object in degrees/sec.
AUTO_PROP float angularVelocity GetAngularVelocity SetAngularVelocity
// The (linear) drag of the object.
AUTO_PROP float drag GetDrag SetDrag
// The angular drag of the object.
AUTO_PROP float angularDrag GetAngularDrag SetAngularDrag
// Controls the effect of gravity on the object.
AUTO_PROP float gravityScale GetGravityScale SetGravityScale
// Controls whether the object is kinematic or dynamic.
AUTO_PROP bool isKinematic GetIsKinematic SetIsKinematic
// Controls whether the objects angle can be changed by collisions with other objects.
AUTO_PROP bool fixedAngle IsFixedAngle SetFixedAngle
// Checks whether the rigid body is sleeping or not.
CUSTOM bool IsSleeping() { return self->IsSleeping(); }
// Checks whether the rigid body is awake or not.
CUSTOM bool IsAwake() { return !self->IsSleeping(); }
// Sets the rigid body into a sleep state.
CUSTOM void Sleep() { self->SetSleeping(true); }
// Wakes the rigid from sleeping.
CUSTOM void WakeUp() { self->SetSleeping(false); }
// Interpolation allows you to smooth out the effect of running physics at a fixed rate.
AUTO_PROP RigidbodyInterpolation2D interpolation GetInterpolation SetInterpolation
// Controls how the object sleeps.
AUTO_PROP RigidbodySleepMode2D sleepMode GetSleepMode SetSleepMode
// The Rigidbody's collision detection mode.
AUTO_PROP CollisionDetectionMode2D collisionDetectionMode GetCollisionDetectionMode SetCollisionDetectionMode
// Controls the mass of the object by adjusting the density of all colliders attached to the object.
AUTO_PROP float mass GetMass SetMass
CUSTOM void AddForce (Vector2 force) { self->AddForce (force); }
CUSTOM void AddTorque (float torque) { self->AddTorque (torque); }
CUSTOM void AddForceAtPosition (Vector2 force, Vector2 position) { self->AddForceAtPosition (force, position); }
END
CONDITIONAL ENABLE_2D_PHYSICS
NONSEALED_CLASS Collider2D : Behaviour
AUTO_PROP bool isTrigger GetIsTrigger SetIsTrigger
AUTO_PTR_PROP Rigidbody2D attachedRigidbody GetRigidbody
// Gets the number of shapes this collider has generated.
AUTO_PROP int shapeCount GetShapeCount
// Checks whether the specified point overlaps the collider or not.
CUSTOM public bool OverlapPoint (Vector2 point) { return self->OverlapPoint(point); }
// The shared physics material of this collider.
CUSTOM_PROP PhysicsMaterial2D sharedMaterial { return Scripting::ScriptingWrapperFor (self->GetMaterial ()); } { self->SetMaterial (value); }
// OnCollisionEnter2D is called when this [[Collider2D]] has begun touching another [[Collider2D]].
CSNONE void OnCollisionEnter2D (Collision2D info);
// OnCollisionStay2D is called once per frame for this [[Collider2D]] if it is continuing to touch another [[Collider2D]].
CSNONE void OnCollisionStay2D (Collision2D info);
// OnCollisionExit2D is called when this [[Collider2D]] has stopped touching another [[Collider2D]].
CSNONE void OnCollisionExit2D (Collision2D info);
// OnTriggerEnter2D is called when a [[Collider2D]] has begun touching another [[Collider2D]] configured as a trigger.
CSNONE void OnTriggerEnter2D (Collider2D other);
// OnTriggerStay2D is called once per frame for a [[Collider2D]] if it is continuing to touch another [[Collider2D]] configured as a trigger.
CSNONE void OnTriggerStay2D (Collider2D other);
// OnTriggerExit2D is called when a [[Collider2D]] has stopped touching another [[Collider2D]] configured as a trigger.
CSNONE void OnTriggerExit2D (Collider2D other);
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS CircleCollider2D : Collider2D
AUTO_PROP Vector2 center GetCenter SetCenter
AUTO_PROP float radius GetRadius SetRadius
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS BoxCollider2D : Collider2D
AUTO_PROP Vector2 center GetCenter SetCenter
AUTO_PROP Vector2 size GetSize SetSize
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS EdgeCollider2D : Collider2D
// Reset to a single horizontal edge.
CUSTOM void Reset() { self->Reset (); }
// Get the number of edges. This is one less than the number of points.
CUSTOM_PROP int edgeCount { { return self->GetEdgeCount (); } }
// Get the number of points. This cannot be less than two which will form a single edge.
CUSTOM_PROP int pointCount { { return self->GetPointCount (); } }
// Get or set the points defining multiple continuous edges.
CUSTOM_PROP Vector2[] points
{
return CreateScriptingArrayStride<Vector2f>(self->GetPoints ().data (), self->GetPointCount (), MONO_COMMON.vector2, sizeof(Vector2f));
}
{
if (self->SetPoints (Scripting::GetScriptingArrayStart<Vector2f> (value), GetScriptingArraySize (value)))
return;
ErrorString("Invalid points assigned to 2D edge collider.");
}
END
CONDITIONAL (ENABLE_2D_PHYSICS && ENABLE_SPRITECOLLIDER)
CLASS SpriteCollider2D : Collider2D
CUSTOM_PROP Sprite sprite
{
return Scripting::ScriptingWrapperFor(self->GetSprite());
}
{
self->SetSprite(value);
}
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS PolygonCollider2D : Collider2D
// Get/Set a single path of points.
CUSTOM_PROP Vector2[] points
{
return CreateScriptingArrayStride<Vector2f>(self->GetPoly().GetPoints(), self->GetPoly().GetPointCount(), MONO_COMMON.vector2, sizeof(*self->GetPoly().GetPoints()));
}
{
self->GetPoly().SetPoints(Scripting::GetScriptingArrayStart<Vector2f>(value), GetScriptingArraySize(value));
self->RefreshPoly();
}
// Get the specified path of points.
CUSTOM Vector2[] GetPath(int index)
{
if (index >= self->GetPoly().GetPathCount())
{
Scripting::RaiseOutOfRangeException("Path %d does not exist.", index);
return SCRIPTING_NULL;
}
const Polygon2D::TPath& path = self->GetPoly().GetPath(index);
return CreateScriptingArrayStride<Vector2f>(path.data(), path.size(), MONO_COMMON.vector2, sizeof(*path.data()));
}
// Set the specified path of points.
CUSTOM void SetPath(int index, Vector2[] points)
{
const Vector2f* arrayStart = Scripting::GetScriptingArrayStart<Vector2f>(points);
const int arraySize = GetScriptingArraySize(points);
Polygon2D::TPath path;
path.assign(arrayStart, arrayStart+arraySize);
self->GetPoly().SetPath(index, path);
self->RefreshPoly();
}
// Get the number of paths.
CUSTOM_PROP int pathCount { return self->GetPoly().GetPathCount(); } { self->GetPoly().SetPathCount(value); self->RefreshPoly(); }
// Get the total number of points in all paths.
CUSTOM int GetTotalPointCount() { return self->GetPoly().GetTotalPointCount(); }
// Create a primitive n-sided polygon.
CUSTOM void CreatePrimitive(int sides, Vector2 scale = Vector2.one, Vector2 offset = Vector2.zero)
{
if (sides < 3)
{
ErrorString ("Cannot create a 2D polygon primitive collider with less than two sides.");
return;
}
if (scale.x <= 0.0f || scale.y <= 0.0f)
{
ErrorString ("Cannot create a 2D polygon primitive collider with an axis scale less than or equal to zero.");
return;
}
self->CreatePrimitive (sides, scale, offset);
}
END
// Describes a contact point where the collision occurs.
CONDITIONAL ENABLE_2D_PHYSICS
STRUCT ContactPoint2D
CSRAW internal Vector2 m_Point;
CSRAW internal Vector2 m_Normal;
CSRAW internal Collider2D m_Collider;
CSRAW internal Collider2D m_OtherCollider;
// The point of contact.
CSRAW public Vector2 point { get { return m_Point; } }
// Normal of the contact point.
CSRAW public Vector2 normal { get { return m_Normal; } }
// The first collider in contact.
CSRAW public Collider2D collider { get { return m_Collider; } }
// The other collider in contact.
CSRAW public Collider2D otherCollider { get { return m_OtherCollider; } }
END
CONDITIONAL ENABLE_2D_PHYSICS
CSRAW [StructLayout (LayoutKind.Sequential)]
NONSEALED_CLASS Collision2D
CSRAW internal Rigidbody2D m_Rigidbody;
CSRAW internal Collider2D m_Collider;
CSRAW internal ContactPoint2D[] m_Contacts;
CSRAW internal Vector2 m_RelativeVelocity;
CSRAW public Rigidbody2D rigidbody { get { return m_Rigidbody; } }
CSRAW public Collider2D collider { get { return m_Collider; } }
CSRAW public Transform transform { get { return rigidbody != null ? rigidbody.transform : collider.transform; } }
CSRAW public GameObject gameObject { get { return m_Rigidbody != null ? m_Rigidbody.gameObject : m_Collider.gameObject; } }
CSRAW public ContactPoint2D[] contacts { get { return m_Contacts; } }
CSRAW public Vector2 relativeVelocity { get { return m_RelativeVelocity; } }
END
// JointAngleLimits2D is used by the [[HingeJoint2D]] to limit the joints angle.
CONDITIONAL ENABLE_2D_PHYSICS
STRUCT JointAngleLimits2D
CSRAW private float m_LowerAngle;
CSRAW private float m_UpperAngle;
// The lower angle limit of the joint.
CSRAW public float min { get { return m_LowerAngle; } set { m_LowerAngle = value; } }
// The upper angle limit of the joint.
CSRAW public float max { get { return m_UpperAngle; } set { m_UpperAngle = value; } }
END
// JointTranslationLimits2D is used by the [[SliderJoint2D]] to limit the joints translation.
CONDITIONAL ENABLE_2D_PHYSICS
STRUCT JointTranslationLimits2D
CSRAW private float m_LowerTranslation;
CSRAW private float m_UpperTranslation;
// The lower translation limit of the joint.
CSRAW public float min { get { return m_LowerTranslation; } set { m_LowerTranslation = value; } }
// The upper translation limit of the joint.
CSRAW public float max { get { return m_UpperTranslation; } set { m_UpperTranslation = value; } }
END
// JointMotor2D is used by the [[HingeJoint2D]] and [[SliderJoint2D]] to motorize a joint.
CONDITIONAL ENABLE_2D_PHYSICS
STRUCT JointMotor2D
CSRAW private float m_MotorSpeed;
CSRAW private float m_MaximumMotorTorque;
// The target motor speed in degrees/second.
CSRAW public float motorSpeed { get { return m_MotorSpeed; } set { m_MotorSpeed = value; } }
// The maximum torque in N-m the motor can use to achieve the desired motor speed.
CSRAW public float maxMotorTorque { get { return m_MaximumMotorTorque; } set { m_MaximumMotorTorque = value; } }
END
// Joint2D is the base class for all 2D joints.
CONDITIONAL ENABLE_2D_PHYSICS
NONSEALED_CLASS Joint2D : Behaviour
// A reference to another rigid-body this joint connects to.
AUTO_PTR_PROP Rigidbody2D connectedBody GetConnectedBody SetConnectedBody
// Whether the connected rigid-bodies should collide with each other or not.
AUTO_PROP bool collideConnected GetCollideConnected SetCollideConnected
END
// The SpringJoint2D ensures that the two connected rigid-bodies stay at a specific distance apart using a spring system.
CONDITIONAL ENABLE_2D_PHYSICS
CLASS SpringJoint2D : Joint2D
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 anchor GetAnchor SetAnchor
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 connectedAnchor GetConnectedAnchor SetConnectedAnchor
// The distance the joint should maintain between the two connected rigid-bodies.
AUTO_PROP float distance GetDistance SetDistance
// The damping ratio for the oscillation whilst trying to achieve the specified distance. 0 means no damping. 1 means critical damping. range { 0.0, 1.0 }
AUTO_PROP float dampingRatio GetDampingRatio SetDampingRatio
// The frequency in Hertz for the oscillation whilst trying to achieve the specified distance. range { 0.0, infinity }
AUTO_PROP float frequency GetFrequency SetFrequency
END
// The DistanceJoint2D ensures that the two connected rigid-bodies stay at a maximum specific distance apart.
CONDITIONAL ENABLE_2D_PHYSICS
CLASS DistanceJoint2D : Joint2D
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 anchor GetAnchor SetAnchor
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 connectedAnchor GetConnectedAnchor SetConnectedAnchor
// The maximum distance the joint should maintain between the two connected rigid-bodies.
AUTO_PROP float distance GetDistance SetDistance
END
// The HingeJoint2D constrains the two connected rigid-bodies around the anchor points not restricting the relative rotation of them. Can be used for wheels, rollers, chains, rag-dol joints, levers etc.
CONDITIONAL ENABLE_2D_PHYSICS
CLASS HingeJoint2D : Joint2D
// Setting the motor or limit automatically enabled them.
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 anchor GetAnchor SetAnchor
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 connectedAnchor GetConnectedAnchor SetConnectedAnchor
// Enables the joint's motor.
AUTO_PROP bool useMotor GetUseMotor SetUseMotor
// Enables the joint's limits.
AUTO_PROP bool useLimits GetUseLimits SetUseLimits
// The motor will apply a force up to a maximum torque to achieve the target velocity in degrees per second.
AUTO_PROP JointMotor2D motor GetMotor SetMotor
// The limits of the hinge joint.
AUTO_PROP JointAngleLimits2D limits GetLimits SetLimits
END
// The SliderJoint2D constrains the two connected rigid-bodies to have on degree of freedom: translation along a fixed axis. Relative motion is prevented.
CONDITIONAL ENABLE_2D_PHYSICS
CLASS SliderJoint2D : Joint2D
// Setting the motor or limit automatically enabled them.
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 anchor GetAnchor SetAnchor
// The Position of the anchor around which the joints motion is constrained.
AUTO_PROP Vector2 connectedAnchor GetConnectedAnchor SetConnectedAnchor
// The translation angle that the joint slides along.
AUTO_PROP float angle GetAngle SetAngle
// Enables the joint's motor.
AUTO_PROP bool useMotor GetUseMotor SetUseMotor
// Enables the joint's limits.
AUTO_PROP bool useLimits GetUseLimits SetUseLimits
// The motor will apply a force up to a maximum torque to achieve the target velocity in degrees per second.
AUTO_PROP JointMotor2D motor GetMotor SetMotor
// The limits of the slider joint.
AUTO_PROP JointTranslationLimits2D limits GetLimits SetLimits
END
CONDITIONAL ENABLE_2D_PHYSICS
CLASS PhysicsMaterial2D : Object
CUSTOM private static void Internal_Create ([Writable]PhysicsMaterial2D mat, string name)
{
PhysicsMaterial2D* material = NEW_OBJECT (PhysicsMaterial2D);
SmartResetObject(*material);
material->SetNameCpp (name);
Scripting::ConnectScriptingWrapperToObject (mat.GetScriptingObject(), material);
}
// Creates a new material.
CSRAW public PhysicsMaterial2D () { Internal_Create (this,null); }
// Creates a new material named /name/.
CSRAW public PhysicsMaterial2D (string name) { Internal_Create (this,name); }
// How bouncy is the surface? A value of 0 will not bounce. A value of 1 will bounce without any loss of energy.
AUTO_PROP float bounciness GetBounciness SetBounciness
// The friction.
AUTO_PROP float friction GetFriction SetFriction
END
CSRAW }
|