//********************************** Banshee Engine (www.banshee3d.com) **************************************************//
//**************** Copyright (c) 2016 Marko Pintera (marko.pintera@gmail.com). All rights reserved. **********************//
using System;
namespace BansheeEngine
{
/** @addtogroup Physics
* @{
*/
///
/// Hinge joint removes all but a single rotation degree of freedom from its two attached bodies (for example a door
/// hinge).
///
public sealed class HingeJoint : Joint
{
///
/// Flags to control hinge joint behaviour.
///
[Flags]
internal enum Flag // Note: Must match C++ enum HingeJoint::Flag
{
Limit = 0x01,
Drive = 0x02,
}
[SerializeField]
private SerializableData data = new SerializableData();
///
/// Returns the current angle between the two attached bodes.
///
public Radian Angle
{
get
{
if (Native != null)
return Native.Angle;
return new Radian(0.0f);
}
}
///
/// Returns the current angular speed of the joint.
///
public float Speed
{
get
{
if (Native != null)
return Native.Speed;
return 0.0f;
}
}
///
/// Determines the limit of the joint. Limit constrains the motion to the specified angle range. You must enable
/// for this to be enforced.
///
public LimitAngularRange Limit
{
get { return data.@internal.limit; }
set
{
if (data.@internal.limit == value)
return;
data.@internal.limit = value;
if (Native != null)
Native.Limit = value;
}
}
///
/// Determines the drive properties of the joint. It drives the joint's angular velocity towards a particular value.
/// You must enable for this to be applied.
///
public HingeJointDrive Drive
{
get { return data.@internal.drive; }
set
{
if (data.@internal.drive == value)
return;
data.@internal.drive = value;
if (Native != null)
Native.Drive = value;
}
}
///
/// Enables or disables a limit that contrains the joint's motion to a specified angle range.
///
public bool EnableLimit
{
get { return (data.@internal.flags & Flag.Limit) != 0; }
set
{
if (!SetFlag(Flag.Limit, value))
return;
if (Native != null)
Native.EnableLimit = value;
}
}
///
/// Enables or disables a drive that drives the joint's angular velocity towards a particular value.
///
public bool EnableDrive
{
get { return (data.@internal.flags & Flag.Drive) != 0; }
set
{
if (!SetFlag(Flag.Drive, value))
return;
if (Native != null)
Native.EnableDrive = value;
}
}
///
/// Returns the native joint wrapped by this component.
///
private NativeHingeJoint Native
{
get { return (NativeHingeJoint)native; }
}
///
/// Toggles a specific distance joint flag on or off.
///
/// Flag to toggle.
/// Should the flag be turned on or off.
/// True if the new newly set flag state was different from the previous one.
private bool SetFlag(Flag flag, bool enabled)
{
Flag newFlags = data.@internal.flags;
if (enabled)
newFlags |= flag;
else
newFlags &= ~flag;
if (newFlags == data.@internal.flags)
return false;
data.@internal.flags = newFlags;
return true;
}
///
internal override NativeJoint CreateNative()
{
NativeHingeJoint joint = new NativeHingeJoint(commonData.@internal, data.@internal);
return joint;
}
///
/// Holds all data the joint component needs to persist through serialization.
///
[SerializeObject]
internal new class SerializableData
{
public ScriptHingeJointData @internal;
public SerializableData()
{
@internal.limit = new LimitAngularRange();
@internal.drive = new HingeJointDrive();
@internal.flags = 0;
}
}
}
/** @} */
}