/// <summary> /// Processes the collision event. /// </summary> /// <param name="jelloCollision">Jello collision.</param> private void ProcessCollisionEvent(JelloCollision jelloCollision) { //only process events if we are pulling and adjacent points need to ignore collisions temporarily // -OR- //we need to calculate if the body is grounded. if ((!adjacentPointsIgnoreCollision || !pulling) && (!requireGrounded || grounded)) { return; } //loop throug each contact in the collision. for (int i = 0; i < jelloCollision.contacts.Length; i++) { //adjacent ignore collisions if (adjacentPointsIgnoreCollision && pulling) { if (body == jelloCollision.contacts[i].bodyA) { //if the penetrating point of this contact is one of our adjacent points, ignore this contact! if (adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyA.getPointMass(jelloCollision.contacts[i].bodyApm))) { jelloCollision.contacts[i].ignore = true; } } else { //if either point of the penetrated edge of htis contact is one of our adjacent points, ignore this contact! if (adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyB.getPointMass(jelloCollision.contacts[i].bodyBpmA)) || adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyB.getPointMass(jelloCollision.contacts[i].bodyBpmB))) { jelloCollision.contacts[i].ignore = true; } } } //grounded logic if (!grounded && !jelloCollision.contacts[i].ignore) { //if the hit point of this contact is below the center of the body, count as grounded. //This works well for the JelloCharacter in the SpiffyDemoScene but may not be the best for all cases. if (jelloCollision.contacts[i].hitPoint.y < transform.position.y) { //clear the number of air jumps performed and set grounded to true. numAirJumpsPreformed = 0; grounded = true; //if we dont need to look for collisions to ignore, then we could break because we now know that at least one point of our body is grounde. if (!adjacentPointsIgnoreCollision || !pulling) { break; } } } } }
/// <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); } } }
/// <summary> /// Processes the collision event. /// </summary> /// <param name="jelloCollision">Jello collision.</param> private void ProcessCollisionEvent(JelloCollision jelloCollision) { //only process events if we are pulling and adjacent points need to ignore collisions temporarily // -OR- //we need to calculate if the body is grounded. if((!adjacentPointsIgnoreCollision || !pulling ) && (!requireGrounded || grounded)) return; //loop throug each contact in the collision. for(int i = 0; i < jelloCollision.contacts.Length; i++) { //adjacent ignore collisions if(adjacentPointsIgnoreCollision && pulling) { if(body == jelloCollision.contacts[i].bodyA) { //if the penetrating point of this contact is one of our adjacent points, ignore this contact! if(adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyA.getPointMass(jelloCollision.contacts[i].bodyApm))) jelloCollision.contacts[i].ignore = true; } else { //if either point of the penetrated edge of htis contact is one of our adjacent points, ignore this contact! if(adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyB.getPointMass(jelloCollision.contacts[i].bodyBpmA)) || adjacentPointMasses.Contains(jelloCollision.contacts[i].bodyB.getPointMass(jelloCollision.contacts[i].bodyBpmB))) { jelloCollision.contacts[i].ignore = true; } } } //grounded logic if(!grounded && !jelloCollision.contacts[i].ignore) { //if the hit point of this contact is below the center of the body, count as grounded. //This works well for the JelloCharacter in the SpiffyDemoScene but may not be the best for all cases. if(jelloCollision.contacts[i].hitPoint.y < transform.position.y) { //clear the number of air jumps performed and set grounded to true. numAirJumpsPreformed = 0; grounded = true; //if we dont need to look for collisions to ignore, then we could break because we now know that at least one point of our body is grounde. if(!adjacentPointsIgnoreCollision || !pulling) break; } } } }
/// <summary> /// Raises the collision event. /// </summary> /// <param name="jelloCollision">The JelloCollision.</param> public virtual void OnJelloCollision(JelloCollision jelloCollision) { //check if there are any subscribers if(JelloCollisionEvent != null) { //call the event JelloCollisionEvent(jelloCollision); } }
/// <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); } } }