| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
- //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
- namespace BansheeEngine
- {
- /** @addtogroup Physics
- * @{
- */
- /// <summary>
- /// Collider with capsule geometry.
- /// </summary>
- public sealed class CapsuleCollider : Collider
- {
- [SerializeField]
- private float radius = 0.2f;
- [SerializeField]
- private float halfHeight = 1.0f;
- [SerializeField]
- private Vector3 normal = Vector3.YAxis;
- /// <summary>
- /// Radius of the capsule.
- /// </summary>
- public float Radius
- {
- get { return radius; }
- set
- {
- value = MathEx.Max(value, 0.01f);
- if (radius == value)
- return;
- radius = value;
- if (Native != null)
- {
- Native.Radius = value;
- if (parent != null)
- parent.UpdateMassDistribution();
- }
- }
- }
- /// <summary>
- /// Half height of the capsule, from the origin to one of the hemispherical centers, along the normal vector.
- /// </summary>
- public float HalfHeight
- {
- get { return halfHeight; }
- set
- {
- value = MathEx.Max(value, 0.01f);
- if (halfHeight == value)
- return;
- halfHeight = value;
- if (Native != null)
- {
- Native.HalfHeight = value;
- if (parent != null)
- parent.UpdateMassDistribution();
- }
- }
- }
-
- /// <summary>
- /// Position of the capsule shape, relative to the component's scene object.
- /// </summary>
- public Vector3 Center
- {
- get { return serializableData.localPosition; }
- set
- {
- if (serializableData.localPosition == value)
- return;
- serializableData.localPosition = value;
- if (Native != null)
- UpdateTransform();
- }
- }
- /// <summary>
- /// Normal vector of the capsule. It determines how is the capsule oriented.
- /// </summary>
- public Vector3 Normal
- {
- get { return normal; }
- set
- {
- if (normal == value)
- return;
- normal = value.Normalized;
- serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
- if (Native != null)
- UpdateTransform();
- }
- }
- public CapsuleCollider()
- {
- serializableData.localRotation = Quaternion.FromToRotation(Vector3.XAxis, normal);
- }
- /// <summary>
- /// Returns the native capsule collider wrapped by this component.
- /// </summary>
- private NativeCapsuleCollider Native
- {
- get { return (NativeCapsuleCollider)native; }
- }
- /// <inheritdoc/>
- internal override NativeCollider CreateCollider()
- {
- NativeCapsuleCollider capsuleCollider = new NativeCapsuleCollider();
- capsuleCollider.Radius = radius;
- capsuleCollider.HalfHeight = halfHeight;
- return capsuleCollider;
- }
- }
- /** @} */
- }
|