public TSRaycastHit Raycast(TSRay ray, FP maxDistance, RaycastCallback callback = null)
        {
            IBody    hitBody;
            TSVector hitNormal;
            FP       hitFraction;

            TSVector origin    = ray.origin;
            TSVector direction = ray.direction;

            if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction))
            {
                if (hitFraction <= maxDistance)
                {
                    GameObject  other              = PhysicsManager.instance.GetGameObject(hitBody);
                    TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                    TSCollider  colliderComponent  = other.GetComponent <TSCollider>();
                    TSTransform transformComponent = other.GetComponent <TSTransform>();
                    return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction));
                }
            }
            else
            {
                direction *= maxDistance;
                if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction))
                {
                    GameObject  other              = PhysicsManager.instance.GetGameObject(hitBody);
                    TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                    TSCollider  colliderComponent  = other.GetComponent <TSCollider>();
                    TSTransform transformComponent = other.GetComponent <TSTransform>();
                    return(new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, direction, hitFraction));
                }
            }
            return(null);
        }
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
        public int RaycastAll(TSVector rayOrigin, TSVector rayDirection, FP maxDistance, out TSRaycastHit[] hits, int layerMask)
        {
            List <TSRaycastHit> raycastHits = new List <TSRaycastHit>();
            var direction = rayDirection;

            direction *= maxDistance;
            var d = TSVector.Distance(rayOrigin, rayOrigin + direction);

            world.CollisionSystem.RaycastAll(rayOrigin, direction, (b, n, f) =>
            {
                //UnityEngine.Debug.LogWarning($"{maxDistance}   ,   {f}");
                GameObject other               = PhysicsManager.instance.GetGameObject(b);
                TSRigidBody bodyComponent      = other.GetComponent <TSRigidBody>();
                TSCollider colliderComponent   = other.GetComponent <TSCollider>();
                TSTransform transformComponent = other.GetComponent <TSTransform>();
                var hit = new TSRaycastHit(bodyComponent, colliderComponent, transformComponent, n, rayOrigin, direction, f);
                var tmp = TSVector.Distance(hit.point, rayOrigin);
                Debug.LogWarning($"碰撞的距离{tmp},最大距离{d}");
                if (tmp <= d)
                {
                    raycastHits.Add(hit);
                }
                return(true);
            }, layerMask);
            hits = raycastHits.ToArray();
            return(hits.Length);
        }
        private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation)
        {
            ICollider[] tsColliders = go.GetComponentsInChildren<ICollider>();
            if (tsColliders != null) {
                for (int index = 0, length = tsColliders.Length; index < length; index++) {
                    PhysicsManager.instance.AddBody(tsColliders[index]);
                }
            }

            TSTransform rootTSTransform = go.GetComponent<TSTransform>();
            if (rootTSTransform != null) {
                rootTSTransform.Initialize();

                rootTSTransform.position = position;
                rootTSTransform.rotation = rotation;
            }

            TSTransform[] tsTransforms = go.GetComponentsInChildren<TSTransform>();
            if (tsTransforms != null) {
                for (int index = 0, length = tsTransforms.Length; index < length; index++) {
                    TSTransform tsTransform = tsTransforms[index];

                    if (tsTransform != rootTSTransform) {
                        tsTransform.Initialize();
                    }
                }
            }
        }
Пример #5
0
        internal void Update(GameObject otherGO, Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.collider   = this.gameObject.GetComponent <TSCollider>();
                this.rigidbody  = this.gameObject.GetComponent <TSRigidBody>();
                this.transform  = this.collider.tsTransform;
            }

            if (c != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint();
                }

                this.relativeVelocity = c.CalculateRelativeVelocity();

                contacts[0].normal      = c.Normal;
                contacts[0].point       = c.p1;
                contacts[0].point2      = c.p2;
                contacts[0].Penetration = c.Penetration;
            }
        }
Пример #6
0
 public void Init(TSRigidBody rigidbody, TSCollider collider, TSTransform transform, TSVector normal, TSVector origin, TSVector direction, FP fraction)
 {
     this.rigidbody = rigidbody;
     this.collider  = collider;
     this.transform = transform;
     this.normal    = normal;
     this.point     = origin + direction * fraction;
     this.distance  = fraction * direction.magnitude;
 }
Пример #7
0
        /**
         *  @brief Creates a new {@link TSRigidBody} when there is no one attached to this GameObject.
         **/
        public void Awake()
        {
            tsTransform = this.GetComponent <TSTransform>();
            tsRigidBody = this.GetComponent <TSRigidBody>();

            if (lossyScale == TSVector.one)
            {
                lossyScale = TSVector.Abs(transform.localScale.ToTSVector());
            }
        }
Пример #8
0
        private static void InitializeGameObject(GameObject go, TSVector position, TSQuaternion rotation)
        {
            ICollider[] tsColliders = go.GetComponentsInChildren <ICollider>();
            if (tsColliders != null)
            {
                foreach (ICollider tsCollider in tsColliders)
                {
                    PhysicsManager.instance.AddBody(tsCollider);
                }
            }

            TSTransform rootTSTransform = go.GetComponent <TSTransform>();

            if (rootTSTransform != null)
            {
                rootTSTransform.Initialize();

                rootTSTransform.position = position;
                rootTSTransform.rotation = rotation;
            }

            TSTransform[] tsTransforms = go.GetComponentsInChildren <TSTransform>();
            if (tsTransforms != null)
            {
                foreach (TSTransform tsTransform in tsTransforms)
                {
                    if (tsTransform != rootTSTransform)
                    {
                        tsTransform.Initialize();
                    }
                }
            }

            TSTransform2D rootTSTransform2D = go.GetComponent <TSTransform2D>();

            if (rootTSTransform2D != null)
            {
                rootTSTransform2D.Initialize();

                rootTSTransform2D.position = new TSVector2(position.x, position.y);
                rootTSTransform2D.rotation = rotation.ToQuaternion().eulerAngles.z;
            }

            TSTransform2D[] tsTransforms2D = go.GetComponentsInChildren <TSTransform2D>();
            if (tsTransforms2D != null)
            {
                foreach (TSTransform2D tsTransform2D in tsTransforms2D)
                {
                    if (tsTransform2D != rootTSTransform2D)
                    {
                        tsTransform2D.Initialize();
                    }
                }
            }
        }
Пример #9
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            rb             = GetComponent <TSRigidBody>();
            transformCache = transform;

            tsCollider = GetComponent <TSCollider>();
            if (transformCache.parent != null)
            {
                tsParent = transformCache.parent.GetComponent <TSTransform>();
            }

            foreach (Transform child in transformCache)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Пример #10
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform>();
            }

            foreach (Transform child in transform)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = transform.position.ToTSVector() + scaledCenter;
                    tsCollider.Body.TSOrientation = transform.rotation.ToTSMatrix();
                    localScale = transform.localScale.ToTSVector();
                }
            }
            else
            {
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Пример #11
0
        /**
         *  @brief Initializes internal properties based on whether there is a {@link TSCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            tsCollider = GetComponent <TSCollider>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <TSTransform>();
            }

            foreach (Transform child in transform)
            {
                TSTransform tsChild = child.GetComponent <TSTransform>();
                if (tsChild != null)
                {
                    tsChildren.Add(tsChild);
                }
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (tsCollider != null)
            {
                if (tsCollider.IsBodyInitialized)
                {
                    tsCollider.Body.TSPosition    = _position + scaledCenter;
                    tsCollider.Body.TSOrientation = TSMatrix.CreateFromQuaternion(_rotation);
                }
            }
            else
            {
                //配合 [AddTracking] Attribute 使用, StateTracker.AddTracking(object obj)通过反射获取obj的成员变量
                StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Пример #12
0
        internal void Update(GameObject otherGO, Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.transform  = this.collider.tsTransform;
            }

            if (c != null)
            {
                if (contacts[0] == null)
                {
                    contacts[0] = new TSContactPoint();
                }

                this.relativeVelocity = c.CalculateRelativeVelocity();

                contacts[0].normal = c.Normal;
                contacts[0].point  = c.p1;
            }
        }
Пример #13
0
        public TSRaycastHit Raycast(TSRay ray, FP maxDistance, int layerMask, RaycastCallback callback = null)
        {
            IBody    hitBody;
            TSVector hitNormal;
            FP       hitFraction;

            TSVector origin    = ray.origin;
            TSVector direction = ray.direction;

            direction *= maxDistance;
            if (Raycast(origin, direction, callback, layerMask, out hitBody, out hitNormal, out hitFraction))
            {
                GameObject  other = PhysicsManager.instance.GetGameObject(hitBody);
                TSTransform transformComponent = transformMap[hitBody];
                TSRigidBody bodyComponent      = transformComponent.rb;
                TSCollider  colliderComponent  = transformComponent.tsCollider;
                hit.Init(bodyComponent, colliderComponent, transformComponent, hitNormal, ray.origin, ray.direction, hitFraction);
                return(hit);
            }

            return(null);
        }
Пример #14
0
 /**
  *  @brief Moves game object based on provided translation vector and a relative {@link TSTransform}.
  *
  *  The game object will move based on TSTransform's forward vector.
  **/
 public void Translate(TSVector translation, TSTransform relativeTo)
 {
     this.position += TSVector.Transform(translation, TSMatrix.CreateFromQuaternion(relativeTo.rotation));
 }
Пример #15
0
 /**
  *  @brief Moves game object based on provided axis values and a relative {@link TSTransform}.
  *
  *  The game object will move based on TSTransform's forward vector.
  **/
 public void Translate(FP x, FP y, FP z, TSTransform relativeTo)
 {
     Translate(new TSVector(x, y, z), relativeTo);
 }
Пример #16
0
 /**
  *  @brief Rotates game object to point forward vector to a target position.
  *
  *  @param other TSTrasform used to get target position.
  **/
 public void LookAt(TSTransform other)
 {
     LookAt(other.position);
 }