Skip to content
Permalink
Browse files

[Physics] Cleanup RigidbodyComponent

  • Loading branch information
Eideren committed May 27, 2019
1 parent 79ef2b1 commit 4db7f42f34725e3a4471403ff1fe6bf3dce5ef55
Showing with 28 additions and 36 deletions.
  1. +28 −36 sources/engine/Xenko.Physics/Elements/RigidbodyComponent.cs
@@ -21,7 +21,6 @@ public sealed class RigidbodyComponent : PhysicsSkinnedComponentBase
[DataMemberIgnore]
internal XenkoMotionState MotionState;

private bool isKinematic;
private float mass = 1.0f;
private RigidBodyTypes type;
private Vector3 gravity = Vector3.Zero;
@@ -54,12 +53,9 @@ public RigidbodyComponent()
[DataMember(75)]
public bool IsKinematic
{
get { return isKinematic; }
get { return RigidBodyType == RigidBodyTypes.Kinematic; }
set
{
isKinematic = value;

if (InternalRigidBody == null) return;
RigidBodyType = value ? RigidBodyTypes.Kinematic : RigidBodyTypes.Dynamic;
}
}
@@ -202,13 +198,13 @@ public bool OverrideGravity

if (value)
{
if (((int)InternalRigidBody.Flags & (int)BulletSharp.RigidBodyFlags.DisableWorldGravity) != 0) return;
if ((InternalRigidBody.Flags & 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;
if ((InternalRigidBody.Flags & BulletSharp.RigidBodyFlags.DisableWorldGravity) == 0) return;
// ReSharper disable once BitwiseOperatorOnEnumWithoutFlags
InternalRigidBody.Flags ^= BulletSharp.RigidBodyFlags.DisableWorldGravity;
}
@@ -259,49 +255,45 @@ public RigidBodyTypes RigidBodyType
{
type = value;

if (InternalRigidBody == null) return;
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;
}
InternalRigidBody.CollisionFlags &= ~(BulletSharp.CollisionFlags.StaticObject | BulletSharp.CollisionFlags.KinematicObject);
break;

case RigidBodyTypes.Static:
if (((int)InternalRigidBody.CollisionFlags & (int)BulletSharp.CollisionFlags.KinematicObject) != 0) InternalRigidBody.CollisionFlags ^= BulletSharp.CollisionFlags.KinematicObject;
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.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;

default:
throw new NotSupportedException(nameof(value));
}
if (!OverrideGravity)
{
if (value == RigidBodyTypes.Dynamic)
{
InternalRigidBody.Gravity = Simulation.Gravity;
}
else
{
InternalRigidBody.Gravity = Vector3.Zero;
}
}
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.LinearVelocity = Vector3.Zero;
InternalRigidBody.InterpolationAngularVelocity = Vector3.Zero;
InternalRigidBody.AngularVelocity = Vector3.Zero;
}
}

0 comments on commit 4db7f42

Please sign in to comment.
You can’t perform that action at this time.