Beispiel #1
0
        /**
         *  @brief Add a new RigidBody to the world.
         *
         *  @param jRigidBody Instance of a {@link FPRigidBody}.
         **/
        public void AddBody(ICollider iCollider)
        {
            if (!(iCollider is FPCollider))
            {
                Debug.LogError("You have a 2D object but your Physics 2D is disabled.");
                return;
            }

            FPCollider tsCollider = (FPCollider)iCollider;

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

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

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

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

            tsCollider._body.FreezeConstraints = constraints;
        }
Beispiel #2
0
        public FPRaycastHit Raycast(FPRay ray, FP maxDistance, RaycastCallback callback = null)
        {
            IBody    hitBody;
            FPVector hitNormal;
            FP       hitFraction;

            FPVector origin    = ray.origin;
            FPVector direction = ray.direction;

            if (Raycast(origin, direction, callback, out hitBody, out hitNormal, out hitFraction))
            {
                if (hitFraction <= maxDistance)
                {
                    GameObject  other              = PhysicsManager.instance.GetGameObject(hitBody);
                    FPRigidBody bodyComponent      = other.GetComponent <FPRigidBody>();
                    FPCollider  colliderComponent  = other.GetComponent <FPCollider>();
                    FPTransform transformComponent = other.GetComponent <FPTransform>();
                    return(new FPRaycastHit(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);
                    FPRigidBody bodyComponent      = other.GetComponent <FPRigidBody>();
                    FPCollider  colliderComponent  = other.GetComponent <FPCollider>();
                    FPTransform transformComponent = other.GetComponent <FPTransform>();
                    return(new FPRaycastHit(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 <FPCollider>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <FPTransform>();
            }

            if (!_serialized)
            {
                UpdateEditMode();
            }

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

            initialized = true;
        }
Beispiel #4
0
        /**
         * @brief Destroys a GameObject in a deterministic way.
         *
         * The method {@link #DestroyFPRigidBody} is called and attached FrameSyncBehaviors are disabled.
         *
         * @param rigidBody Instance of a {@link FPRigidBody}
         **/
        public static void SyncedDestroy(GameObject gameObject)
        {
            if (instance != null && instance.lockstep != null)
            {
                SyncedDisableBehaviour(gameObject);

                FPCollider[] tsColliders = gameObject.GetComponentsInChildren <FPCollider>();
                if (tsColliders != null)
                {
                    for (int index = 0, length = tsColliders.Length; index < length; index++)
                    {
                        FPCollider tsCollider = tsColliders[index];
                        DestroyFPRigidBody(tsCollider.gameObject, tsCollider.Body);
                    }
                }

                FPCollider2D[] tsColliders2D = gameObject.GetComponentsInChildren <FPCollider2D>();
                if (tsColliders2D != null)
                {
                    for (int index = 0, length = tsColliders2D.Length; index < length; index++)
                    {
                        FPCollider2D tsCollider2D = tsColliders2D[index];
                        DestroyFPRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
                    }
                }
            }
        }
Beispiel #5
0
 public FPRaycastHit(FPRigidBody rigidbody, FPCollider collider, FPTransform transform, FPVector normal, FPVector origin, FPVector 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;
 }
Beispiel #6
0
        /**
         * @brief Removes objets related to a provided player.
         *
         * @param playerId Target player's id.
         **/
        public static void RemovePlayer(int playerId)
        {
            if (instance != null && instance.lockstep != null)
            {
                List <FrameSyncManagedBehaviour> behaviorsList = instance.behaviorsByPlayer[(byte)playerId];

                for (int index = 0, length = behaviorsList.Count; index < length; index++)
                {
                    FrameSyncManagedBehaviour tsmb = behaviorsList[index];
                    tsmb.disabled = true;

                    FPCollider[] tsColliders = ((FrameSyncBehaviour)tsmb.FrameSyncBehavior).gameObject.GetComponentsInChildren <FPCollider>();
                    if (tsColliders != null)
                    {
                        for (int index2 = 0, length2 = tsColliders.Length; index2 < length2; index2++)
                        {
                            FPCollider tsCollider = tsColliders[index2];

                            if (!tsCollider.Body.TSDisabled)
                            {
                                DestroyFPRigidBody(tsCollider.gameObject, tsCollider.Body);
                            }
                        }
                    }

                    FPCollider2D[] tsCollider2Ds = ((FrameSyncBehaviour)tsmb.FrameSyncBehavior).gameObject.GetComponentsInChildren <FPCollider2D>();
                    if (tsCollider2Ds != null)
                    {
                        for (int index2 = 0, length2 = tsCollider2Ds.Length; index2 < length2; index2++)
                        {
                            FPCollider2D tsCollider2D = tsCollider2Ds[index2];

                            if (!tsCollider2D.Body.TSDisabled)
                            {
                                DestroyFPRigidBody(tsCollider2D.gameObject, tsCollider2D.Body);
                            }
                        }
                    }
                }
            }
        }
Beispiel #7
0
        internal void Update(GameObject otherGO, Contact c)
        {
            if (this.gameObject == null)
            {
                this.gameObject = otherGO;
                this.collider   = this.gameObject.GetComponent <FPCollider>();
                this.rigidbody  = this.gameObject.GetComponent <FPRigidBody>();
                this.transform  = this.collider.FPTransform;
            }

            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;
            }
        }