Пример #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;
        }
        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);
            }
        }
        public void Clone(RigidBody rb)
        {
            this.marker            = rb.marker;
            this.affectedByGravity = rb.affectedByGravity;
            this.boundingBox       = rb.boundingBox;
            this.internalIndex     = rb.internalIndex;
            this.inverseMass       = rb.inverseMass;
            this.isColliderOnly    = rb.isColliderOnly;
            this.isStatic          = rb.isStatic;
            this.isKinematic       = rb.isKinematic;
            this.sweptDirection    = rb.sweptDirection;

            this.position        = rb.Position;
            this.orientation     = rb.Orientation;
            this.linearVelocity  = rb.LinearVelocity;
            this.angularVelocity = rb.AngularVelocity;
            this.inertia         = rb.Inertia;
            this.invInertia      = rb.InverseInertia;
            this.invInertiaWorld = rb.InverseInertiaWorld;
            this.invOrientation  = rb.invOrientation;
            this.force           = rb.Force;
            this.torque          = rb.Torque;

            this.shapeClone = poolGenericShapeClone.GetNew();
            this.shapeClone.Clone(rb.Shape);

            this.connections.Clear();
            for (index = 0, length = rb.connections.Count; index < length; index++)
            {
                this.connections.Add(rb.connections[index]);
            }
            this.constraints.Clear();
            for (index = 0, length = rb.constraints.Count; index < length; index++)
            {
                this.constraints.Add(rb.constraints[index]);
            }

            this.isActive = rb.IsActive;

            this.inactiveTime = rb.inactiveTime;

            this.marker = rb.marker;

            this.disabled = rb.disabled;

            this.freezeConstraint     = rb._freezeConstraints;
            this._freezePosition      = rb._freezePosition;
            this._freezeRotation      = rb._freezeRotation;
            this._freezeRotationQuat  = rb._freezeRotationQuat;
            this.prevKinematicGravity = rb.prevKinematicGravity;
            this.linearDrag           = rb.linearDrag;
            this.angularDrag          = rb.angularDrag;
            this.staticFriction       = rb.staticFriction;
            this.restitution          = rb.restitution;
        }
        private static void DrawFreezePanel(ref FPRigidBodyConstraints freezeConstraint, bool position)
        {
            FPRigidBodyConstraints axisX = position ? FPRigidBodyConstraints.FreezePositionX : FPRigidBodyConstraints.FreezeRotationX;
            FPRigidBodyConstraints axisY = position ? FPRigidBodyConstraints.FreezePositionY : FPRigidBodyConstraints.FreezeRotationY;
            FPRigidBodyConstraints axisZ = position ? FPRigidBodyConstraints.FreezePositionZ : FPRigidBodyConstraints.FreezeRotationZ;

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel(position ? "Freeze Position" : "Freeze Rotation");

            Rect controlRect = GUILayoutUtility.GetLastRect();

            controlRect.width = 30;
            controlRect.x    += EditorGUIUtility.labelWidth;

            bool fX = GUI.Toggle(controlRect, CheckAxis(freezeConstraint, axisX), "X");

            controlRect.x += 30;
            bool fY = GUI.Toggle(controlRect, CheckAxis(freezeConstraint, axisY), "Y");

            controlRect.x += 30;
            bool fZ = GUI.Toggle(controlRect, CheckAxis(freezeConstraint, axisZ), "Z");

            freezeConstraint = FPRigidBodyConstraints.None;

            if (fX)
            {
                freezeConstraint |= axisX;
            }

            if (fY)
            {
                freezeConstraint |= axisY;
            }

            if (fZ)
            {
                freezeConstraint |= axisZ;
            }

            EditorGUILayout.EndHorizontal();
        }
 private static bool CheckAxis(FPRigidBodyConstraints toCheck, FPRigidBodyConstraints axis)
 {
     return((toCheck & axis) == axis);
 }