void activateSideSprings(Rigidbody2D collidedRigidbody) { SpringJoint2D springJoint1 = sideRigidBody1.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D springJoint2 = sideRigidBody2.gameObject.GetComponent <SpringJoint2D> (); if (stackingToPlate) { //the pancake is stacking to plate, need to adjust spring attachment parameters (dafaults are for other pancakes). springJoint1.connectedAnchor = new Vector2(-1.0f * sideSpringPlatePlacement, 0); springJoint1.dampingRatio = springPancakeToPlateDamping; springJoint1.frequency = springPancakeToPlateFrequency; springJoint2.connectedAnchor = new Vector2(sideSpringPlatePlacement, 0); springJoint2.dampingRatio = springPancakeToPlateDamping; springJoint2.frequency = springPancakeToPlateFrequency; } if (flippedAttached) { Vector2 v1 = springJoint1.connectedAnchor; springJoint1.connectedAnchor = new Vector2(-1.0f * v1[0], 0); Vector2 v2 = springJoint2.connectedAnchor; springJoint2.connectedAnchor = new Vector2(-1.0f * v2[0], 0); } if (stackingToPlate) { springJoint1.connectedBody = collidedRigidbody; springJoint1.enabled = true; springJoint2.connectedBody = collidedRigidbody; springJoint2.enabled = true; } else { PancakeBehaviourScript script = collidedRigidbody.gameObject.GetComponentInParent <PancakeBehaviourScript> (); bool pancakeBelowIsFlipped = script.isFlippedAttached(); if (flippedAttached && pancakeBelowIsFlipped) { springJoint1.connectedBody = script.getSideRigidBody1(); springJoint2.connectedBody = script.getSideRigidBody2(); } else if (flippedAttached || pancakeBelowIsFlipped) { //one of the pancakes is flipped, I need to attach side 1 to side 2 and vice versa springJoint1.connectedBody = script.getSideRigidBody2(); springJoint2.connectedBody = script.getSideRigidBody1(); } else { //nothing is flipped: springJoint1.connectedBody = script.getSideRigidBody1(); springJoint2.connectedBody = script.getSideRigidBody2(); } springJoint1.enabled = true; springJoint2.enabled = true; } }
void activateSprings(Rigidbody2D collidedRigidbody) { SpringJoint2D[] springJoints = rigidBodyOfInterest.gameObject.GetComponents <SpringJoint2D> (); bool foundFirst = false; foreach (SpringJoint2D springJoint in springJoints) { if (stackingToPlate) { //the pancake is stacking to plate, need to adjust spring attachment parameters (dafaults are for other pancakes). Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(v[0] / springPancakePlacement * springPlatePlacement, 0); foundFirst = true; springJoint.dampingRatio = springPancakeToPlateDamping; springJoint.frequency = springPancakeToPlateFrequency; springJoint.distance = springPancakeToPlateDistance; } else { //attaching to another pancake, if the pancake below is flipper during attachemnt, I should rotate the // attachment (same as if this pancake is flipped). //if this pancake is also flipped, the rotation will be repeated, //briging the connectin to norma n=once again in the below if clause PancakeBehaviourScript script = collidedRigidbody.gameObject.GetComponentInParent <PancakeBehaviourScript> (); if (script.isFlippedAttached()) { Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(-1.0f * v[0], 0); } } float zRot = (rigidBodyOfInterest.transform.rotation.eulerAngles.z % 360); //Debug.Log ("zRot:"); //Debug.Log (zRot); if ((zRot > 90 && zRot < 270.0) || (zRot < -90 && zRot > -270)) { //the colliding PancakeBehaviourScript is upside down, //I will flip the springPlatePlacement connections, such that the springs will still be cross: Vector2 v = springJoint.connectedAnchor; springJoint.connectedAnchor = new Vector2(-1.0f * v[0], 0); flippedAttached = true; } springJoint.connectedBody = collidedRigidbody; springJoint.enabled = true; } activateSideSprings(collidedRigidbody); }
public void reConnectSpringsBasedOnThisPancake(Rigidbody2D reAttachmentBase) { SpringJoint2D[] ownSpringJoints = rigidBodyOfInterest.gameObject.GetComponents <SpringJoint2D> (); SpringJoint2D[] baseSpringJoints = reAttachmentBase.gameObject.GetComponents <SpringJoint2D> (); for (int i = 0; i < 2; ++i) { Vector2 v = baseSpringJoints [i].connectedAnchor; ownSpringJoints [i].connectedAnchor = new Vector2(v [0], v [1]); ownSpringJoints [i].dampingRatio = baseSpringJoints [i].dampingRatio; ownSpringJoints [i].frequency = baseSpringJoints [i].frequency; ownSpringJoints [i].distance = baseSpringJoints [i].distance; ownSpringJoints[i].connectedBody = baseSpringJoints[i].connectedBody; } PancakeBehaviourScript scriptOfBase = reAttachmentBase.gameObject.GetComponentInParent <PancakeBehaviourScript> (); if (scriptOfBase.isFlippedAttached()) { flippedAttached = true; } if (scriptOfBase.isStackingToPlate()) { stackingToPlate = true; } SpringJoint2D ownSideJoint1 = sideRigidBody1.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D ownSideJoint2 = sideRigidBody2.gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D baseSideJoint1 = scriptOfBase.getSideRigidBody1().gameObject.GetComponent <SpringJoint2D> (); SpringJoint2D baseSideJoint2 = scriptOfBase.getSideRigidBody2().gameObject.GetComponent <SpringJoint2D> (); Vector2 v1 = baseSideJoint1.connectedAnchor; ownSideJoint1.connectedAnchor = new Vector2(v1 [0], v1 [1]); Vector2 v2 = baseSideJoint2.connectedAnchor; ownSideJoint2.connectedAnchor = new Vector2(v2 [0], v2 [1]); ownSideJoint1.dampingRatio = baseSideJoint1.dampingRatio; ownSideJoint2.dampingRatio = baseSideJoint2.dampingRatio; ownSideJoint1.frequency = baseSideJoint1.frequency; ownSideJoint2.frequency = baseSideJoint2.frequency; ownSideJoint1.distance = baseSideJoint1.distance; ownSideJoint2.distance = baseSideJoint2.distance; ownSideJoint1.connectedBody = baseSideJoint1.connectedBody; ownSideJoint2.connectedBody = baseSideJoint2.connectedBody; }