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