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

            DrawDefaultInspector();

            serializedObject.Update();

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

            if (constraintsFoldout)
            {
                EditorGUI.indentLevel++;

                FPRigidBodyConstraints 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);
            }
        }
Exemple #4
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;
 }
        public void Start()
        {
            if (!Application.isPlaying)
            {
                return;
            }

            Initialize();
            rb = GetComponent <FPRigidBody> ();
        }
Exemple #6
0
        /**
         *  @brief Creates a new {@link FPRigidBody} when there is no one attached to this GameObject.
         **/
        public void Awake()
        {
            FPTransform = this.GetComponent <FPTransform>();
            FPRigidBody = this.GetComponent <FPRigidBody>();

            if (lossyScale == FPVector.one)
            {
                lossyScale = FPVector.Abs(transform.localScale.ToFPVector());
            }
        }
Exemple #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;
            }
        }