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);
        }
        public override void OnInspectorGUI()
        {
            TSRigidBody tsRB = (target as TSRigidBody);

            DrawDefaultInspector();

            serializedObject.Update();

            constraintsFoldout = EditorGUILayout.Foldout(constraintsFoldout, "Constraints");

            if (constraintsFoldout)
            {
                EditorGUI.indentLevel++;

                TSRigidBodyConstraints freezeConstraintPos = tsRB.constraints, freezeConstraintRot = tsRB.constraints;

                DrawFreezePanel(ref freezeConstraintPos, true);
                DrawFreezePanel(ref freezeConstraintRot, false);

                tsRB.constraints = (freezeConstraintPos | freezeConstraintRot);

                EditorGUI.indentLevel--;
            }

            serializedObject.ApplyModifiedProperties();

            if (GUI.changed)
            {
                EditorUtility.SetDirty(target);
            }
        }
        /**
         *  @brief Add a new RigidBody to the world.
         *
         *  @param jRigidBody Instance of a {@link TSRigidBody}.
         **/
        public void AddBody(ICollider iCollider)
        {
            if (!(iCollider is TSCollider))
            {
                Debug.LogError("You have a 2D object but your Physics 2D is disabled.");
                return;
            }

            TSCollider tsCollider = (TSCollider)iCollider;

            if (tsCollider._body != null)
            {
                //already added
                return;
            }

            TSRigidBody            tsRB        = tsCollider.GetComponent <TSRigidBody>();
            TSRigidBodyConstraints constraints = tsRB != null ? tsRB.constraints : TSRigidBodyConstraints.None;

            tsCollider.Initialize();
            world.AddBody(tsCollider._body);
            gameObjectMap[tsCollider._body] = tsCollider.gameObject;

            if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>() != null)
            {
                TSCollider parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>();
                world.AddConstraint(new ConstraintHierarchy(parentCollider.Body, tsCollider._body, (tsCollider.GetComponent <TSTransform>().position + tsCollider.ScaledCenter) - (parentCollider.GetComponent <TSTransform>().position + parentCollider.ScaledCenter)));
            }

            tsCollider._body.FreezeConstraints = constraints;
        }
        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);
        }
예제 #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
        public override void OnSyncedStart()
        {
            thisBody = GetComponent <TSRigidBody> ();
            IBody3D body1 = GetComponent <TSCollider> ().Body;
            IBody3D body2 = connectedBody.Body;

            thisJoint = new BasicJoint3D(PhysicsWorldManager.instance.GetWorld(), body1, body2, transform.TransformPoint(anchor).ToTSVector(),
                                         transform.TransformPoint(connectedAnchor).ToTSVector());
        }
예제 #8
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());
            }
        }
        public void Start()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            Initialize();
            rb = GetComponent <TSRigidBody> ();
        }
        public override void OnSyncedStart()
        {
            thisBody = GetComponent <TSRigidBody> ();


            Vector3  worldPos   = transform.TransformPoint(anchor);
            TSVector TSworldPos = worldPos.ToTSVector();

            Vector3 worldAxis = transform.TransformDirection(Axis);

            TSWorldAxis = worldAxis.ToTSVector();

            thisJoint = new CharacterJoint3D(PhysicsWorldManager.instance.GetWorld(), GetComponent <TSCollider> ().Body, connectedBody.Body, TSworldPos, TSWorldAxis);
        }
예제 #11
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;
        }
예제 #12
0
        /**
         *  @brief Add a new RigidBody to the world.
         *
         *  @param jRigidBody Instance of a {@link TSRigidBody}.
         **/
        public void AddBody(ICollider iCollider)
        {
            if (!(iCollider is TSCollider))
            {
                Debug.LogError("You have a 2D object but your Physics 2D is disabled.");
                return;
            }

            TSCollider tsCollider = (TSCollider)iCollider;

            if (tsCollider._body != null)
            {
                //already added
                return;
            }

            TSRigidBody            tsRB        = tsCollider.tsTransform.rb; // tsCollider.GetComponent<TSRigidBody>();
            TSRigidBodyConstraints constraints = tsRB != null ? tsRB.constraints : TSRigidBodyConstraints.None;

            tsCollider.Initialize();
            world.AddBody(tsCollider._body);
            GameObject gameObject = tsCollider.gameObject;

            gameObjectMap[tsCollider._body] = gameObject;

            HashList <TrueSyncBehaviour> behaviours = new HashList <TrueSyncBehaviour>();

            TrueSyncBehaviour[] behavioursArray = gameObject.GetComponents <TrueSyncBehaviour>();
            for (int i = 0, count = behavioursArray.Length; i < count; i++)
            {
                behaviours.Add(behavioursArray[i]);
            }
            behavioursMap[tsCollider._body] = behaviours;

            transformMap[tsCollider._body] = tsCollider.tsTransform;

            if (tsCollider.gameObject.transform.parent != null && tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>() != null)
            {
                TSCollider parentCollider = tsCollider.gameObject.transform.parent.GetComponentInParent <TSCollider>();
                world.AddConstraint(new ConstraintHierarchy(parentCollider.Body, tsCollider._body, (tsCollider.tsTransform.position + tsCollider.ScaledCenter) - (parentCollider.tsTransform.position + parentCollider.ScaledCenter)));
            }

            tsCollider._body.FreezeConstraints = constraints;
        }
        public override void OnSyncedStart()
        {
            base.OnSyncedStart();

            thisBody = GetComponent <TSRigidBody>();


            IBody3D body1 = GetComponent <TSCollider>().Body;
            IBody3D body2 = connectedBody.Body;

            Vector3  worldPos   = transform.TransformPoint(anchor);
            TSVector TSworldPos = worldPos.ToTSVector();

            Vector3 worldAxis = transform.TransformDirection(Axis);

            TSWorldAxis = worldAxis.ToTSVector();

            thisJoint = new PrismaticJoint3D(PhysicsWorldManager.instance.GetWorld(), body1, body2, minimumDistance, maximumDistance);
        }
예제 #14
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);
        }
예제 #15
0
        public override void OnSyncedStart()
        {
            thisBody = GetComponent <TSRigidBody> ();
            IBody3D body1 = GetComponent <TSCollider> ().Body;
            IBody3D body2 = connectedBody.Body;

            Vector3  worldPos   = transform.TransformPoint(anchor);
            TSVector TSworldPos = worldPos.ToTSVector();

            Vector3 worldAxis = transform.TransformDirection(Axis);

            TSWorldAxis = worldAxis.ToTSVector();

            Debug.Log("Hinge joint");

            //if (useLimits)
            //    thisJoint = new LimitedHingeJoint(PhysicsWorldManager.instance.GetWorld(), body1, body2, TSworldPos, TSWorldAxis, -Limits.Min, Limits.Max);
            //else
            thisJoint = new HingeJoint3D(PhysicsWorldManager.instance.GetWorld(), body1, body2, TSworldPos, TSWorldAxis);

            //charac joint = new CharacterJoint ();
        }