示例#1
0
	public void CopyToStaticBody(JelloBody oldBody, JelloBody newBody) //TODO make sure these all are up to date with any new variables that need assigning
	{
		newBody.affectedByGravity = oldBody.affectedByGravity;
		newBody.disabled = oldBody.disabled;
		newBody.gravity = oldBody.gravity;
		newBody.IsAwake = oldBody.IsAwake;
		newBody.IsKinematic = oldBody.IsKinematic;
		newBody.IsStatic = tar.IsStatic;
		newBody.IsStatic = true;
		newBody.IsTrigger = oldBody.IsTrigger;
		newBody.overrideWorldGravity = oldBody.overrideWorldGravity;
		newBody.pivotOffset = oldBody.pivotOffset;
		newBody.setComponentReferences();
		newBody.polyCollider = oldBody.polyCollider;
		
		if(oldBody.meshLink != null)
		{
			newBody.meshLink = oldBody.meshLink;
			newBody.meshLink.body = newBody;
		}
		
		if(oldBody.Shape != null)
		{
			newBody.setShape(oldBody.Shape, shapeSettingOptions);
			newBody.Mass = Mathf.Infinity;
		}
		
		for(int i = 0; i < oldBody.EdgePointMassCount; i++)
		{
			newBody.setEdgePointMass(oldBody.getEdgePointMass(i), i);
			newBody.getEdgePointMass(i).Mass = Mathf.Infinity;
			newBody.getEdgePointMass(i).body = newBody;
		}
		for(int i = 0; i < oldBody.InternalPointMassCount; i++)
		{
			newBody.setInternalPointMass(oldBody.getInternalPointMass(i), i);
			newBody.getInternalPointMass(i).Mass = Mathf.Infinity;
			newBody.getInternalPointMass(i).body = newBody;
		}
		for(int i = 0; i < oldBody.AttachPointCount; i++)
		{
			newBody.AddAttachPoint(oldBody.GetAttachPoint(i));
			newBody.GetAttachPoint(i).body = newBody;
		}
		for(int i = 0; i < oldBody.JointCount; i++)
		{
			newBody.AddJoint(oldBody.GetJoint(i));
			newBody.GetJoint(i).bodyA = newBody;
		}
		
		EditorUtility.SetDirty(newBody);
	}
示例#2
0
    /// <summary>
    /// Processes the collision event.
    /// </summary>
    /// <param name="jelloCollision">Jello collision.</param>
    private void ProcessCollisionEvent(JelloCollision jelloCollision)
    {
        if (!stickAtPoints && !stickAtEdges)
        {
            return;
        }

        //loop through each contact and see if a joint exists.
        //if not, create the joint.
        for (int i = 0; i < jelloCollision.contacts.Length; i++)
        {
            JelloContact contact = jelloCollision.contacts[i];

            bool skip = true;
            if (stickAtPoints && body == contact.bodyA)
            {
                skip = false;
            }
            if (stickAtEdges && body == contact.bodyB)
            {
                skip = false;
            }
            if (skip)
            {
                continue;
            }

            bool found = false;

            //see if this joint already exists
            for (int c = joints.Count - 1; c >= 0; c--)
            {
                JelloJoint joint = joints[c];

                //remove joints that have been destroyed
                if (joint.destroyed)
                {
                    joints.Remove(joint);
                    continue;
                }

                //i only want to know if there is a joint between the two bodies and if the point or edge is already taken.
                //should only need to use transform...?
                if (joint.TransformA != contact.transformA && joint.TransformB != contact.transformA)
                {
                    continue;
                }
                if (joint.TransformA != contact.transformB && joint.TransformB != contact.transformB)
                {
                    continue;
                }

                //at this point we know that they share the same transforms.

                //we also know that one of the bodies is the body that this script is attached to.


                if (contact.bodyA != null)
                {
                    if (stickAtPoints && joint.bodyA == contact.bodyA)                 //this matters if i am alowing point joints
                    {
                        if (joint.affectedIndicesA.Length != 1)                        //joint bodya has multiple anchors
                        {
                            continue;
                        }
                        else if (joint.affectedIndicesA[0] != contact.bodyApm)                        //joint bodyA has a single anchor and is not the same
                        {
                            continue;
                        }
                    }

                    //where am i handling rigidbodies or even static bodies??

                    if (stickAtEdges && joint.bodyB != null)
                    {
                        if (joint.bodyB == contact.bodyA)                       //this matters if i am allowing edge joints
                        {
                            if (joint.affectedIndicesB.Length != 1)
                            {
                                continue;
                            }
                            else if (joint.affectedIndicesB[0] != contact.bodyApm)
                            {
                                continue;
                            }
                        }
                    }
                }

                if (contact.bodyB != null)
                {
                    if (stickAtEdges && joint.bodyA == contact.bodyB)
                    {
                        if (joint.affectedIndicesA.Length != 2)
                        {
                            continue;
                        }
                        else
                        {
                            if (joint.affectedIndicesA[0] != contact.bodyBpmA && joint.affectedIndicesA[0] != contact.bodyBpmB)
                            {
                                continue;
                            }
                            if (joint.affectedIndicesA[1] != contact.bodyBpmA && joint.affectedIndicesA[1] != contact.bodyBpmB)
                            {
                                continue;
                            }
                            if (joint.bodyB != null)
                            {
                                if (joint.affectedIndicesB.Length != 1)
                                {
                                    continue;
                                }
                                else if (joint.affectedIndicesB[0] != contact.bodyApm)
                                {
                                    continue;
                                }
                            }
                        }
                    }


                    if (stickAtPoints && joint.bodyB != null)
                    {
                        if (joint.bodyB == contact.bodyB)
                        {
                            if (joint.affectedIndicesB.Length != 2)
                            {
                                continue;
                            }
                            else
                            {
                                if (joint.affectedIndicesB[0] != contact.bodyBpmA && joint.affectedIndicesB[0] != contact.bodyBpmB)
                                {
                                    continue;
                                }
                                if (joint.affectedIndicesB[1] != contact.bodyBpmA && joint.affectedIndicesB[1] != contact.bodyBpmB)
                                {
                                    continue;
                                }
                                if (joint.bodyA != null)
                                {
                                    if (joint.affectedIndicesA.Length != 1)
                                    {
                                        continue;
                                    }
                                    else if (joint.affectedIndicesA[0] != contact.bodyApm)
                                    {
                                        continue;
                                    }
                                }
                                //else check if ra is the same?
                            }
                        }
                    }
                }


                //must be the same
                found = true;
                break;
            }

            if (!found)           //joint Doesn't exist, so create it!
            {
                if (!stickAtPoints && contact.transformA == transform)
                {
                    continue;
                }
                if (!stickAtEdges && contact.transformB == transform)
                {
                    continue;
                }

                JelloJoint joint = new JelloJoint();

                joint.TransformA   = contact.transformA;
                joint.localAnchorA = joint.TransformA.InverseTransformPoint(contact.hitPoint);
                if (joint.bodyA != null)
                {
                    int[]     indices  = new int[1];
                    Vector2[] vertices = new Vector2[1];

                    indices[0]  = contact.bodyApm;
                    vertices[0] = contact.bodyA.getPointMass(indices[0]).LocalPosition;
                    joint.RebuildAnchor(contact.transformA.InverseTransformPoint(contact.hitPoint), true, false, indices, vertices);
                }

                joint.TransformB   = contact.transformB;
                joint.localAnchorB = joint.TransformB.InverseTransformPoint(contact.hitPoint);
                if (contact.bodyB != null)
                {
                    int[]     indices  = new int[2];
                    Vector2[] vertices = new Vector2[2];

                    indices[0] = contact.bodyBpmA;
                    indices[1] = contact.bodyBpmB;

                    vertices[0] = contact.bodyB.getPointMass(indices[0]).LocalPosition;
                    vertices[1] = contact.bodyB.getPointMass(indices[1]).LocalPosition;
                    joint.RebuildAnchor(joint.localAnchorB, false, false, indices, vertices);
                }

                joint.breakable     = true;
                joint.breakVelocity = breakVelocity;
                body.AddJoint(joint);
                joints.Add(joint);
            }
        }
    }
示例#3
0
    //TODO make sure these all are up to date with any new variables that need assigning
    public void CopyToStaticBody(JelloBody oldBody, JelloBody newBody)
    {
        newBody.affectedByGravity = oldBody.affectedByGravity;
        newBody.disabled = oldBody.disabled;
        newBody.gravity = oldBody.gravity;
        newBody.IsAwake = oldBody.IsAwake;
        newBody.IsKinematic = oldBody.IsKinematic;
        newBody.IsStatic = tar.IsStatic;
        newBody.IsStatic = true;
        newBody.IsTrigger = oldBody.IsTrigger;
        newBody.overrideWorldGravity = oldBody.overrideWorldGravity;
        newBody.pivotOffset = oldBody.pivotOffset;
        newBody.setComponentReferences();
        newBody.polyCollider = oldBody.polyCollider;

        if(oldBody.meshLink != null)
        {
            newBody.meshLink = oldBody.meshLink;
            newBody.meshLink.body = newBody;
        }

        if(oldBody.Shape != null)
        {
            newBody.setShape(oldBody.Shape, shapeSettingOptions);
            newBody.Mass = Mathf.Infinity;
        }

        for(int i = 0; i < oldBody.EdgePointMassCount; i++)
        {
            newBody.setEdgePointMass(oldBody.getEdgePointMass(i), i);
            newBody.getEdgePointMass(i).Mass = Mathf.Infinity;
            newBody.getEdgePointMass(i).body = newBody;
        }
        for(int i = 0; i < oldBody.InternalPointMassCount; i++)
        {
            newBody.setInternalPointMass(oldBody.getInternalPointMass(i), i);
            newBody.getInternalPointMass(i).Mass = Mathf.Infinity;
            newBody.getInternalPointMass(i).body = newBody;
        }
        for(int i = 0; i < oldBody.AttachPointCount; i++)
        {
            newBody.AddAttachPoint(oldBody.GetAttachPoint(i));
            newBody.GetAttachPoint(i).body = newBody;
        }
        for(int i = 0; i < oldBody.JointCount; i++)
        {
            newBody.AddJoint(oldBody.GetJoint(i));
            newBody.GetJoint(i).bodyA = newBody;
        }

        EditorUtility.SetDirty(newBody);
    }