Example #1
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);
        }
Example #2
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 FPCollider = (FPCollider)iCollider;

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

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

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

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

            FPCollider._body.FreezeConstraints = constraints;
        }
Example #3
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;
 }
        /**
         *  @brief Initializes internal properties based on whether there is a {@link FPCollider} attached.
         **/
        public void Initialize()
        {
            if (initialized)
            {
                return;
            }

            FPCollider = GetComponent <FPCollider>();
            if (transform.parent != null)
            {
                tsParent = transform.parent.GetComponent <FPTransform>();
            }

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

            if (!_serialized)
            {
                UpdateEditMode();
            }

            if (FPCollider != null)
            {
                if (FPCollider.IsBodyInitialized)
                {
                    FPCollider.Body.FPPosition    = _position + scaledCenter;
                    FPCollider.Body.FPOrientation = FPMatrix.CreateFromQuaternion(_rotation);
                }
            }
            else
            {
                // StateTracker.AddTracking(this);
            }

            initialized = true;
        }
Example #5
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 FPContactPoint();
                }

                this.relativeVelocity = c.CalculateRelativeVelocity();

                contacts[0].normal = c.Normal;
                contacts[0].point  = c.p1;
            }
        }