-
-
Notifications
You must be signed in to change notification settings - Fork 934
/
RigidbodyComponent.cs
669 lines (584 loc) · 22.7 KB
/
RigidbodyComponent.cs
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
// Copyright (c) Xenko contributors (https://xenko.com) and Silicon Studio Corp. (https://www.siliconstudio.co.jp)
// Distributed under the MIT license. See the LICENSE.md file in the project root for more information.
using System;
using System.Collections.Generic;
using Xenko.Core;
using Xenko.Core.Annotations;
using Xenko.Core.Collections;
using Xenko.Core.Mathematics;
using Xenko.Rendering;
namespace Xenko.Physics
{
[DataContract("RigidbodyComponent")]
[Display("Rigidbody")]
public sealed class RigidbodyComponent : PhysicsSkinnedComponentBase
{
[DataMemberIgnore]
internal BulletSharp.RigidBody InternalRigidBody;
[DataMemberIgnore]
internal XenkoMotionState MotionState;
private bool isKinematic;
private float mass = 1.0f;
private RigidBodyTypes type;
private Vector3 gravity = Vector3.Zero;
private float angularDamping;
private float linearDamping;
private bool overrideGravity;
/// <summary>
/// Gets the linked constraints.
/// </summary>
/// <value>
/// The linked constraints.
/// </value>
[DataMemberIgnore]
public List<Constraint> LinkedConstraints { get; }
public RigidbodyComponent()
{
LinkedConstraints = new List<Constraint>();
ProcessCollisions = true;
}
/// <summary>
/// Gets or sets the kinematic property
/// </summary>
/// <value>true, false</value>
/// <userdoc>
/// Move the rigidbody only by the transform property, not other forces
/// </userdoc>
[DataMember(75)]
public bool IsKinematic
{
get { return isKinematic; }
set
{
isKinematic = value;
if (InternalRigidBody == null) return;
RigidBodyType = value ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;
}
}
/// <summary>
/// Gets or sets the mass of this Rigidbody
/// </summary>
/// <value>
/// true, false
/// </value>
/// <userdoc>
/// Objects with higher mass push objects with lower mass more when they collide. For large differences, use point values; for example, write 0.1 or 10, not 1 or 100000.
/// </userdoc>
[DataMember(80)]
[DataMemberRange(0, 6)]
public float Mass
{
get
{
return mass;
}
set
{
if (value < 0)
{
throw new InvalidOperationException("the Mass of a Rigidbody cannot be negative.");
}
mass = value;
if (InternalRigidBody == null) return;
var inertia = ColliderShape.InternalShape.CalculateLocalInertia(value);
InternalRigidBody.SetMassProps(value, inertia);
InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint
}
}
/// <summary>
/// Gets the collider shape.
/// </summary>
/// <value>
/// The collider shape
/// </value>
[DataMemberIgnore]
public override ColliderShape ColliderShape
{
get
{
return colliderShape;
}
set
{
colliderShape = value;
if (value == null)
return;
if (InternalRigidBody == null)
return;
if (NativeCollisionObject != null)
NativeCollisionObject.CollisionShape = value.InternalShape;
var inertia = colliderShape.InternalShape.CalculateLocalInertia(mass);
InternalRigidBody.SetMassProps(mass, inertia);
InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint
}
}
/// <summary>
/// Gets or sets the linear damping of this rigidbody
/// </summary>
/// <value>
/// true, false
/// </value>
/// <userdoc>
/// The amount of damping for directional forces
/// </userdoc>
[DataMember(85)]
public float LinearDamping
{
get
{
return linearDamping;
}
set
{
linearDamping = value;
InternalRigidBody?.SetDamping(value, AngularDamping);
}
}
/// <summary>
/// Gets or sets the angular damping of this rigidbody
/// </summary>
/// <value>
/// true, false
/// </value>
/// <userdoc>
/// The amount of damping for rotational forces
/// </userdoc>
[DataMember(90)]
public float AngularDamping
{
get
{
return angularDamping;
}
set
{
angularDamping = value;
InternalRigidBody?.SetDamping(LinearDamping, value);
}
}
/// <summary>
/// Gets or sets if this Rigidbody overrides world gravity
/// </summary>
/// <value>
/// true, false
/// </value>
/// <userdoc>
/// Override gravity with the vector specified in Gravity
/// </userdoc>
[DataMember(95)]
public bool OverrideGravity
{
get
{
return overrideGravity;
}
set
{
overrideGravity = value;
if (InternalRigidBody == null) return;
if (value)
{
if (((int)InternalRigidBody.Flags & (int)BulletSharp.RigidBodyFlags.DisableWorldGravity) != 0) return;
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
InternalRigidBody.Flags |= BulletSharp.RigidBodyFlags.DisableWorldGravity;
}
else
{
if (((int)InternalRigidBody.Flags & (int)BulletSharp.RigidBodyFlags.DisableWorldGravity) == 0) return;
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
InternalRigidBody.Flags ^= BulletSharp.RigidBodyFlags.DisableWorldGravity;
}
}
}
/// <summary>
/// Gets or sets the gravity acceleration applied to this RigidBody
/// </summary>
/// <value>
/// A vector representing moment and direction
/// </value>
/// <userdoc>
/// The gravity acceleration applied to this rigidbody
/// </userdoc>
[DataMember(100)]
public Vector3 Gravity
{
get
{
return gravity;
}
set
{
gravity = value;
if (InternalRigidBody != null)
{
InternalRigidBody.Gravity = value;
}
}
}
/// <summary>
/// Gets or sets the type.
/// </summary>
/// <value>
/// The type.
/// </value>
[DataMemberIgnore]
public RigidBodyTypes RigidBodyType
{
get
{
return type;
}
set
{
type = value;
if (InternalRigidBody == null) return;
switch (value)
{
case RigidBodyTypes.Dynamic:
if (((int)InternalRigidBody.CollisionFlags & (int)BulletSharp.CollisionFlags.StaticObject) != 0) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.StaticObject;
if (((int)InternalRigidBody.CollisionFlags & (int)BulletSharp.CollisionFlags.KinematicObject) != 0) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.KinematicObject;
if (InternalRigidBody != null && Simulation != null && !OverrideGravity) InternalRigidBody.Gravity = Simulation.Gravity;
if (InternalRigidBody != null)
{
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.LinearVelocity = Vector3.Zero;
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.AngularVelocity = Vector3.Zero;
}
break;
case RigidBodyTypes.Static:
if (((int)InternalRigidBody.CollisionFlags & (int)BulletSharp.CollisionFlags.KinematicObject) != 0) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.KinematicObject;
InternalRigidBody.CollisionFlags |= BulletSharp.CollisionFlags.StaticObject;
if (InternalRigidBody != null && !OverrideGravity) InternalRigidBody.Gravity = Vector3.Zero;
if (InternalRigidBody != null)
{
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.LinearVelocity = Vector3.Zero;
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.AngularVelocity = Vector3.Zero;
}
break;
case RigidBodyTypes.Kinematic:
if (((int)InternalRigidBody.CollisionFlags & (int)BulletSharp.CollisionFlags.StaticObject) != 0) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.StaticObject;
InternalRigidBody.CollisionFlags |= BulletSharp.CollisionFlags.KinematicObject;
if (InternalRigidBody != null && !OverrideGravity) InternalRigidBody.Gravity = Vector3.Zero;
if (InternalRigidBody != null)
{
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.LinearVelocity = Vector3.Zero;
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.AngularVelocity = Vector3.Zero;
}
break;
}
}
}
protected override void OnAttach()
{
MotionState = new XenkoMotionState(this);
SetupBoneLink();
var rbci = new BulletSharp.RigidBodyConstructionInfo(0.0f, MotionState, ColliderShape.InternalShape, Vector3.Zero);
InternalRigidBody = new BulletSharp.RigidBody(rbci)
{
UserObject = this,
};
NativeCollisionObject = InternalRigidBody;
NativeCollisionObject.ContactProcessingThreshold = !Simulation.CanCcd ? 1e18f : 1e30f;
if (ColliderShape.NeedsCustomCollisionCallback)
{
NativeCollisionObject.CollisionFlags |= BulletSharp.CollisionFlags.CustomMaterialCallback;
}
if (ColliderShape.Is2D) //set different defaults for 2D shapes
{
InternalRigidBody.LinearFactor = new Vector3(1.0f, 1.0f, 0.0f);
InternalRigidBody.AngularFactor = new Vector3(0.0f, 0.0f, 1.0f);
}
var inertia = ColliderShape.InternalShape.CalculateLocalInertia(mass);
InternalRigidBody.SetMassProps(mass, inertia);
InternalRigidBody.UpdateInertiaTensor(); //this was the major headache when I had to debug Slider and Hinge constraint
base.OnAttach();
Mass = mass;
LinearDamping = linearDamping;
AngularDamping = angularDamping;
OverrideGravity = overrideGravity;
Gravity = gravity;
RigidBodyType = IsKinematic ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;
Simulation.AddRigidBody(this, (CollisionFilterGroupFlags)CollisionGroup, CanCollideWith);
}
protected override void OnDetach()
{
MotionState.Dispose();
MotionState.Clear();
if (NativeCollisionObject == null)
return;
//Remove constraints safely
var toremove = new FastList<Constraint>();
foreach (var c in LinkedConstraints)
{
toremove.Add(c);
}
foreach (var disposable in toremove)
{
disposable.Dispose();
}
LinkedConstraints.Clear();
//~Remove constraints
Simulation.RemoveRigidBody(this);
InternalRigidBody = null;
base.OnDetach();
}
protected internal override void OnUpdateDraw()
{
base.OnUpdateDraw();
if (type == RigidBodyTypes.Dynamic && BoneIndex != -1)
{
//write to ModelViewHierarchy
var model = Data.ModelComponent;
model.Skeleton.NodeTransformations[BoneIndex].Flags = !IsKinematic ? ModelNodeFlags.EnableRender | ModelNodeFlags.OverrideWorldMatrix : ModelNodeFlags.Default;
if (!IsKinematic) model.Skeleton.NodeTransformations[BoneIndex].WorldMatrix = BoneWorldMatrixOut;
}
}
//This is called by the physics engine to update the transformation of Dynamic rigidbodies.
private void RigidBodySetWorldTransform(ref Matrix physicsTransform)
{
Data.PhysicsComponent.Simulation.SimulationProfiler.Mark();
Data.PhysicsComponent.Simulation.UpdatedRigidbodies++;
if (BoneIndex == -1)
{
UpdateTransformationComponent(ref physicsTransform);
}
else
{
UpdateBoneTransformation(ref physicsTransform);
}
}
//This is valid for Dynamic rigidbodies (called once at initialization)
//and Kinematic rigidbodies, called every simulation tick (if body not sleeping) to let the physics engine know where the kinematic body is.
private void RigidBodyGetWorldTransform(out Matrix physicsTransform)
{
Data.PhysicsComponent.Simulation.SimulationProfiler.Mark();
Data.PhysicsComponent.Simulation.UpdatedRigidbodies++;
if (BoneIndex == -1)
{
DerivePhysicsTransformation(out physicsTransform);
}
else
{
DeriveBonePhysicsTransformation(out physicsTransform);
}
}
/// <summary>
/// Gets the total torque.
/// </summary>
/// <value>
/// The total torque.
/// </value>
public Vector3 TotalTorque => InternalRigidBody?.TotalTorque ?? Vector3.Zero;
/// <summary>
/// Applies the impulse.
/// </summary>
/// <param name="impulse">The impulse.</param>
public void ApplyImpulse(Vector3 impulse)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyCentralImpulse(impulse);
}
/// <summary>
/// Applies the impulse.
/// </summary>
/// <param name="impulse">The impulse.</param>
/// <param name="localOffset">The local offset.</param>
public void ApplyImpulse(Vector3 impulse, Vector3 localOffset)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyImpulse(impulse, localOffset);
}
/// <summary>
/// Applies the force.
/// </summary>
/// <param name="force">The force.</param>
public void ApplyForce(Vector3 force)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyCentralForce(force);
}
/// <summary>
/// Applies the force.
/// </summary>
/// <param name="force">The force.</param>
/// <param name="localOffset">The local offset.</param>
public void ApplyForce(Vector3 force, Vector3 localOffset)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyForce(force, localOffset);
}
/// <summary>
/// Applies the torque.
/// </summary>
/// <param name="torque">The torque.</param>
public void ApplyTorque(Vector3 torque)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyTorque(torque);
}
/// <summary>
/// Applies the torque impulse.
/// </summary>
/// <param name="torque">The torque.</param>
public void ApplyTorqueImpulse(Vector3 torque)
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.ApplyTorqueImpulse(torque);
}
/// <summary>
/// Clears all forces being applied to this rigidbody
/// </summary>
public void ClearForces()
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody?.ClearForces();
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.LinearVelocity = Vector3.Zero;
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.AngularVelocity = Vector3.Zero;
}
/// <summary>
/// Gets or sets the angular velocity.
/// </summary>
/// <value>
/// The angular velocity.
/// </value>
[DataMemberIgnore]
public Vector3 AngularVelocity
{
get
{
return InternalRigidBody?.AngularVelocity ?? Vector3.Zero;
}
set
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.AngularVelocity = value;
}
}
/// <summary>
/// Gets or sets the linear velocity.
/// </summary>
/// <value>
/// The linear velocity.
/// </value>
[DataMemberIgnore]
public Vector3 LinearVelocity
{
get
{
return InternalRigidBody?.LinearVelocity ?? Vector3.Zero;
}
set
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.LinearVelocity = value;
}
}
/// <summary>
/// Gets the total force.
/// </summary>
/// <value>
/// The total force.
/// </value>
public Vector3 TotalForce => InternalRigidBody?.TotalForce ?? Vector3.Zero;
/// <summary>
/// Gets or sets the angular factor.
/// </summary>
/// <value>
/// The angular factor.
/// </value>
[DataMemberIgnore]
public Vector3 AngularFactor
{
get
{
return InternalRigidBody?.AngularFactor ?? Vector3.Zero;
}
set
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.AngularFactor = value;
}
}
/// <summary>
/// Gets or sets the linear factor.
/// </summary>
/// <value>
/// The linear factor.
/// </value>
[DataMemberIgnore]
public Vector3 LinearFactor
{
get
{
return InternalRigidBody?.LinearFactor ?? Vector3.Zero;
}
set
{
if (InternalRigidBody == null)
{
throw new InvalidOperationException("Attempted to call a Physics function that is avaliable only when the Entity has been already added to the Scene.");
}
InternalRigidBody.LinearFactor = value;
}
}
internal class XenkoMotionState : BulletSharp.MotionState
{
private RigidbodyComponent rigidBody;
public XenkoMotionState(RigidbodyComponent rb)
{
rigidBody = rb;
}
public void Clear()
{
rigidBody = null;
}
public override void GetWorldTransform(out BulletSharp.Math.Matrix transform)
{
rigidBody.RigidBodyGetWorldTransform(out var xenkoMatrix);
transform = xenkoMatrix;
}
public override void SetWorldTransform(ref BulletSharp.Math.Matrix transform)
{
Matrix asXenkoMatrix = transform;
rigidBody.RigidBodySetWorldTransform(ref asXenkoMatrix);
}
}
}
}