internal P2DDistanceJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB) : base(bodyA, bodyB) { _jointType = JointType.Distance; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; }
internal P2DPulleyJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB, Transform groundAnchorA, Transform groundAnchorB, float ratio) : base(bodyA, bodyB) { _jointType = JointType.Pulley; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; this.groundAnchorA = groundAnchorA; this.groundAnchorB = groundAnchorB; if (m_GroundPositionA != (Vector2)groundAnchorA.position) { m_GroundPositionA = groundAnchorA.position; var dA = _bodyA.GetRelativePoint(_localAnchorA) - m_GroundPositionA; m_LengthA = dA.magnitude; } if (m_GroundPositionB != (Vector2)groundAnchorB.position) { m_GroundPositionB = groundAnchorB.position; var dB = _bodyB.GetRelativePoint(_localAnchorB) - m_GroundPositionB; m_LengthB = dB.magnitude; } this.ratio = ratio; }
internal P2DWheelJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB, Vector2 axis) : base(bodyA, bodyB) { _jointType = JointType.Wheel; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; SetAxis(axis); }
internal P2DAngleJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB) : base(bodyA, bodyB) { _jointType = JointType.Angle; biasFactor = 0.2f; softness = 0.1f; maxImpulse = 1000.0f; }
protected P2DJoint(Rigidbody2DExt body, Rigidbody2DExt bodyB) { _bodyA = body; _bodyB = bodyB; //Connected bodies should not collide by default collideConnected = false; }
internal P2DFrictionJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB) : base(bodyA, bodyB) { _jointType = JointType.Friction; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; maxForce = 100f; maxTorque = 100f; }
/// <summary> /// You need to specify a local anchor point /// where they are attached and the relative body angle. The position /// of the anchor point is important for computing the reaction torque. /// You can change the anchor points relative to bodyA or bodyB by changing LocalAnchorA /// and/or LocalAnchorB. /// </summary> /// <param name="bodyA">The first body</param> /// <param name="bodyB">The second body</param> /// <param name="localAnchorA">The first body anchor.</param> /// <param name="localAnchorB">The second body anchor.</param> internal P2DWeldJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB) : base(bodyA, bodyB) { _jointType = JointType.Weld; this._localAnchorA = localAnchorA; this._localAnchorB = localAnchorB; referenceAngle = Mathf.DeltaAngle(bodyB.rotation, bodyA.rotation) * Mathf.Deg2Rad; }
internal P2DHingeJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB) : base(bodyA, bodyB) { _jointType = JointType.Hinge; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; referenceAngle = Mathf.DeltaAngle(bodyB.rotation, bodyA.rotation) * Mathf.Deg2Rad; m_Impulse = Vector3.zero; limitState = JointLimitState2D.Inactive; }
protected virtual void OnTriggerEnter2D(Collider2D other) { GameObject go = other.gameObject; if (noHit != (noHit | (1 << go.layer))) { /*if(go.layer == 15) * { * Debug.Log("Ricoche dans trigger"); * return; * }*/ //if (statAttaque.ePower == 0 || statAttaque.eRadius == 0) { if (dommageHit == (dommageHit | (1 << go.gameObject.layer))) { Personnages en = go.GetComponent <Personnages>() as Personnages; en.DommagePerso(dmg); } //}else if if (myTransform != null) { Collider2D[] colliders = Physics2D.OverlapCircleAll(myTransform.position, statAttaque.eRadius, dommageHit); foreach (Collider2D nerbyObject in colliders) { if (dommageHit == (dommageHit | (1 << nerbyObject.gameObject.layer))) { if (Rigidbody2DExt.AddExplosionForce(nerbyObject.GetComponent <Rigidbody2D>(), statAttaque.ePower, myTransform.position, statAttaque.eRadius, statAttaque.upwardsModifier)) { // if (nerbyObject == other) // continue; Personnages en = nerbyObject.GetComponent <Personnages>() as Personnages; en.DommagePerso(dmg); } } } if (effetExplosion != null && statAttaque.eRadius != 0) { Transform clone = Instantiate(effetExplosion, myTransform.position, myTransform.rotation) as Transform; ShockWaveForce wave = clone.GetComponent <ShockWaveForce>(); wave.radius = statAttaque.eRadius * 1.3f; Destroy(clone.gameObject, 1f); } } if (effetContact != null && myTransform != null) { Transform clone = Instantiate(effetContact, myTransform.position, myTransform.rotation) as Transform; Destroy(clone.gameObject, 1f); } Destroy(gameObject); } }
internal P2DSliderJoint(Rigidbody2DExt bodyA, Rigidbody2DExt bodyB, Vector2 localAnchorA, Vector2 localAnchorB, Vector2 axis) : base(bodyA, bodyB) { _jointType = JointType.Slider; _localAnchorA = localAnchorA; _localAnchorB = localAnchorB; SetAxis(axis); m_LocalYAxisA = MathUtils.Cross(1.0f, localXAxisA); referenceAngle = -Mathf.DeltaAngle(bodyB.rotation, bodyA.rotation) * Mathf.Deg2Rad; m_LimitState = JointLimitState2D.Inactive; }
public void Explosion(int dmg, PlayerCharacter2D pl, float fireRate) { if (CanAttack) { if (effetAttaquePrefab != null) { Transform clone = Instantiate(effetAttaquePrefab, firePoint.position, firePoint.rotation) as Transform; ShockWaveForce wave = clone.GetComponent <ShockWaveForce>(); wave.radius = statAttaque.eRadius * 1.3f; Destroy(clone.gameObject, 1f); } attaqueCooldown = fireRate; if (Rigidbody2DExt.AddExplosionForce(pl.GetComponent <Rigidbody2D>(), statAttaque.ePower, firePoint.position, statAttaque.eRadius, statAttaque.upwardsModifier)) { pl.DommagePerso(dmg); } } }
void UpdateBody(Rigidbody2DExt body, int index) { if (!body) { return; } _bodyCache[index] = body; MassData[] massData; body.CalculateMassData(out massData); if (_bodyMap.ContainsKey(body)) { _bodyMap[body] = massData; } else { _bodyMap.Add(body, massData); } }
internal override void InitVelocityConstraints() { _bodyA = jointA._internalJoint._bodyB; _bodyB = jointB._internalJoint._bodyB; _bodyC = jointA._internalJoint._bodyA; _bodyD = jointB._internalJoint._bodyA; m_LocalCenterA = _bodyA.centerOfMass; m_LocalCenterB = _bodyB.centerOfMass; m_LocalCenterC = _bodyC.centerOfMass; m_LocalCenterD = _bodyD.centerOfMass; m_InvMassA = _bodyA._invMass; m_InvMassB = _bodyB._invMass; m_InvMassC = _bodyC._invMass; m_InvMassD = _bodyD._invMass; m_InvIA = _bodyA._invI; m_InvIB = _bodyB._invI; m_InvIC = _bodyC._invI; m_InvID = _bodyD._invI; //Vector2 cA = _bodyA.GetRelativePoint(m_LocalCenterA); float aA = _bodyA.rotation * Mathf.Deg2Rad; Vector2 vA = _bodyA.velocity; float wA = _bodyA.angularVelocity * Mathf.Deg2Rad; //Vector2 cB = _bodyB.GetRelativePoint(m_LocalCenterB); float aB = _bodyB.rotation * Mathf.Deg2Rad; Vector2 vB = _bodyB.velocity; float wB = _bodyB.angularVelocity * Mathf.Deg2Rad; //Vector2 cC = _bodyB.GetRelativePoint(m_LocalCenterC); float aC = _bodyC.rotation * Mathf.Deg2Rad; Vector2 vC = _bodyC.velocity; float wC = _bodyC.angularVelocity * Mathf.Deg2Rad; //Vector2 cD = _bodyB.GetRelativePoint(m_LocalCenterD); float aD = _bodyD.rotation * Mathf.Deg2Rad; Vector2 vD = _bodyD.velocity; float wD = _bodyD.angularVelocity * Mathf.Deg2Rad; Rot qA = new Rot(aA), qB = new Rot(aB), qC = new Rot(aC), qD = new Rot(aD); m_Mass = 0.0f; float coordinateA = 0.0f, coordinateB = 0.0f; if (jointA._internalJoint._jointType == JointType.Hinge) { var hinge = (P2DHingeJoint)jointA._internalJoint; _localAnchorC = hinge._localAnchorA; _localAnchorA = hinge._localAnchorB; m_LocalAxisC = Vector2.zero; m_ReferenceAngleA = hinge.referenceAngle; coordinateA = aA - aC - m_ReferenceAngleA; m_JvAC = Vector2.zero; m_JwA = 1.0f; m_JwC = 1.0f; m_Mass += m_InvIA + m_InvIC; } else { var slider = (P2DSliderJoint)jointA._internalJoint; _localAnchorC = slider._localAnchorA; _localAnchorA = slider._localAnchorB; m_LocalAxisC = slider.localXAxisA; var bpA = _bodyA.position; var bpC = _bodyC.position; Vector2 pC = _localAnchorC; Vector2 pA = MathUtils.MulT(qC, MathUtils.Mul(qA, _localAnchorA) + (bpA - bpC)); coordinateA = Vector2.Dot(pA - pC, m_LocalAxisC); Vector2 u = MathUtils.Mul(qC, m_LocalAxisC); Vector2 rC = MathUtils.Mul(qC, _localAnchorC - m_LocalCenterC); Vector2 rA = MathUtils.Mul(qA, _localAnchorA - m_LocalCenterA); m_JvAC = u; m_JwC = MathUtils.Cross(rC, u); m_JwA = MathUtils.Cross(rA, u); m_Mass += m_InvMassC + m_InvMassA + m_InvIC * m_JwC * m_JwC + m_InvIA * m_JwA * m_JwA; } if (jointB._internalJoint._jointType == JointType.Hinge) { var hinge = (P2DHingeJoint)jointB._internalJoint; _localAnchorD = hinge._localAnchorA; _localAnchorB = hinge._localAnchorB; m_LocalAxisD = Vector2.zero; m_ReferenceAngleB = hinge.referenceAngle; coordinateB = aB - aD - m_ReferenceAngleB; m_JvBD = Vector2.zero; m_JwB = ratio; m_JwD = ratio; m_Mass += ratio * ratio * (m_InvIB + m_InvID); } else { var slider = (P2DSliderJoint)jointB._internalJoint; _localAnchorD = slider._localAnchorA; _localAnchorB = slider._localAnchorB; m_LocalAxisD = slider.localXAxisA; var bPB = _bodyB.position; var bPD = _bodyD.position; Vector2 pD = _localAnchorD; Vector2 pB = MathUtils.MulT(qD, MathUtils.Mul(qB, _localAnchorB) + (bPB - bPD)); coordinateB = Vector2.Dot(pB - pD, m_LocalAxisD); Vector2 u = MathUtils.Mul(qD, m_LocalAxisD); Vector2 rD = MathUtils.Mul(qD, _localAnchorD - m_LocalCenterD); Vector2 rB = MathUtils.Mul(qB, _localAnchorB - m_LocalCenterB); m_JvBD = ratio * u; m_JwD = ratio * MathUtils.Cross(rD, u); m_JwB = ratio * MathUtils.Cross(rB, u); m_Mass += ratio * ratio * (m_InvMassD + m_InvMassB) + m_InvID * m_JwD * m_JwD + m_InvIB * m_JwB * m_JwB; } m_Constant = coordinateA + ratio * coordinateB; // Compute effective mass. m_Mass = m_Mass > 0.0f ? 1.0f / m_Mass : 0.0f; // TODO: Scale impulse to support variable timestep vA += (m_InvMassA * m_Impulse) * m_JvAC; wA += m_InvIA * m_Impulse * m_JwA; vB += (m_InvMassB * m_Impulse) * m_JvBD; wB += m_InvIB * m_Impulse * m_JwB; vC -= (m_InvMassC * m_Impulse) * m_JvAC; wC -= m_InvIC * m_Impulse * m_JwC; vD -= (m_InvMassD * m_Impulse) * m_JvBD; wD -= m_InvID * m_Impulse * m_JwD; if (!_bodyA.isKinematic) { _bodyA.velocity = vA; if (!_bodyA.fixedAngle) { _bodyA.angularVelocity = wA * Mathf.Rad2Deg; } } if (!_bodyB.isKinematic) { _bodyB.velocity = vB; if (!_bodyB.fixedAngle) { _bodyB.angularVelocity = wB * Mathf.Rad2Deg; } } if (!_bodyC.isKinematic) { _bodyC.velocity = vC; if (!_bodyC.fixedAngle) { _bodyC.angularVelocity = wC * Mathf.Rad2Deg; } } if (!_bodyD.isKinematic) { _bodyD.velocity = vD; if (!_bodyD.fixedAngle) { _bodyD.angularVelocity = wD * Mathf.Rad2Deg; } } }
public static Vector2 GetLinearVelocityFromWorldPoint(this Rigidbody2DExt body, Vector2 worldPoint) { return(body.velocity + new Vector2(-body.angularVelocity * Mathf.Deg2Rad * (worldPoint.y - body.position.y), body.angularVelocity * Mathf.Deg2Rad * (worldPoint.x - body.position.x))); }