public void CollisionStay(PhysXCollision collision) { // float penCoefficientA = (penForMaxImpulseScale - penForUnitImpulseScale * maxImpulseScale) / (penForUnitImpulseScale * penForUnitImpulseScale * penForMaxImpulseScale - penForUnitImpulseScale * penForMaxImpulseScale * penForMaxImpulseScale); // float penCoefficientB = maxImpulseScale / penForMaxImpulseScale - penCoefficientA * penForMaxImpulseScale; if (collision.rigidBody != null) { velocity = (rigidBody.velocity - collision.rigidBody.velocity).magnitude; } else { velocity = rigidBody.velocity.magnitude; } velocity -= velocityOffset; if (velocity < 0) { velocity = 0; } float penExp = velocityScale / velocity; if (penExp > maxPenExp) { penExp = maxPenExp; } float penCoefficient = 1 / Mathf.Pow(penForUnitImpulseScale, penExp); for (int i = 0; i < collision.contactCount; i++) { PhysXContactPoint contactPoint = collision.GetContact(i); if (contactPoint.ownShape == collider.shape) { float penetration = -contactPoint.separation; if (penetration < 0) { penetration = 0; } //Debug.Log("penetration: " + penetration); Vector3 impulse = contactPoint.impulse; // float impulseScale = penCoefficientA * penetration * penetration + penCoefficientB * penetration; float impulseScale = penCoefficient * Mathf.Pow(penetration, penExp); if (impulseScale > maxImpulseScale) { impulseScale = maxImpulseScale; } impulse *= impulseScale; rigidBody.AddGhostImpulse(impulse, contactPoint.point); } } }
public void CollisionEnter(PhysXCollision collision) { // Debug.LogError(collision.gameObject); Vector3 impactNormal = collision.GetContact(0).normal; // if we are the true projectile, then deal with game altering stuff like damage n that if (explosionForce > 0) { Squishing hitMeshSquisher = collision.gameObject.GetComponentInParent <Squishing>(); if (hitMeshSquisher != null) { Vector3 explosionPoint = collision.GetContact(0).point + impactNormal * explosionOffset; hitMeshSquisher.ExplodeMeshAt(explosionPoint, explosionForce); } } Vector3 hitPoint = collision.GetContact(0).point; VehicleHealthManager hitVm = collision.gameObject.GetComponentInParent <VehicleHealthManager>(); if (hitVm != null) { if (hitVm.teamId == weaponDamageDetails.sourceTeamId) { Destroy(gameObject); } } if (isTrueProjectile) { DamageCollisionHandler(hitVm, hitPoint); } VisualCollisionHandler(impactNormal, hitVm != null); // pooledObject.Finish(); Destroy(gameObject); }
public void CollisionEnter(PhysXCollision collision) { // Debug.Log("crashed into: " + collision.gameObject); float dSpeed = myRb.velocity.magnitude; float impulse = collision.impulse.magnitude; if (collision.rigidBody != null) { dSpeed = (myRb.velocity - collision.rigidBody.velocity).magnitude; } if (myPhotonView.IsMine && collision.contactCount > 0 && dSpeed > 1.5 && collisionTimer < 0) { collisionTimer = maxCollisionRate; Vector3 collisionNormal = collision.GetContact(0).normal; Vector3 collisionForce = collision.impulse; if (Vector3.Dot(collisionForce, collisionNormal) < 0) { collisionForce = -collisionForce; } collisionForce /= Time.fixedDeltaTime; collisionForce = transform.InverseTransformDirection(collisionForce); VehicleHealthManager otherVehicleManager = collision.gameObject.GetComponent <VehicleHealthManager>(); Vector3 collisionPoint = Vector3.zero; for (int i = 0; i < collision.contactCount; i++) { collisionPoint += collision.GetContact(i).point; } collisionPoint /= collision.contactCount; Vector3 contactDirection = transform.InverseTransformPoint(collisionPoint); float damage = CalculateCollisionDamage(collisionForce, contactDirection, otherVehicleManager != null); // instantiate damage sound over network if ((damage > crashSoundsSmallDamageThreshold || (otherVehicleManager != null)) && timeSinceLastRam > 0.25f) { PlayDamageSoundNetwork(damage); } damage = damage / rammingDamageResistance; if (myPhotonView.IsMine && hasHotPotatoManager && otherVehicleManager != null && timeSinceLastRam > 0.25f) { if (collisionNpv.GetDriverID() == PhotonNetwork.LocalPlayer.ActorNumber || collisionNpv.GetGunnerID() == PhotonNetwork.LocalPlayer.ActorNumber) { // Debug.LogError("Slow down should happen"); hotPotatoManager.SlowedCollision(); } if (collisionSparks != null) { GameObject a = Instantiate(collisionSparks, collisionPoint, Quaternion.identity); a.transform.parent = transform; } if (collisionNpv != null && !collisionNpv.botDriver) { if (damage > 4) { driverCrashDetector.CrashCollisionCamera(collision, false); } else { driverCrashDetector.CrashCollisionCamera(collision, true); } } } if (damage > 5) { if (otherVehicleManager != null) { if (otherVehicleManager != null) { damage *= otherVehicleManager.rammingDamageMultiplier; } Weapon.WeaponDamageDetails rammingDetails = otherVehicleManager.rammingDetails; rammingDetails.damage = damage; if (markedByTeams.Contains(rammingDetails.sourceTeamId)) { rammingDetails.damage *= markedTeamDamageIncrease; } TakeDamage(rammingDetails); } else { TakeDamage(damage); } } } if (collision.rigidBody != null) { timeSinceLastRam = 0f; } }
public void CollisionStay(PhysXCollision collision) { if (collision.contactCount > 0 && collision.GetContact(0).separation < -minPenetration) { if ((collision.rigidBody == null || collision.rigidBody.mass >= minDeformationMass) && !collision.collider.gameObject.CompareTag("DustGround")) { meshDeformationMarker.Begin(); bool isInconvenient = collision.collider is PhysXMeshCollider && !((PhysXMeshCollider)collision.collider).convex; Vector3 collisionSurfaceNormal = Vector3.zero; Vector3 collisionSurfacePoint = Vector3.zero; for (int i = 0; i < collision.contactCount; i++) { PhysXContactPoint contactPoint = collision.GetContact(i); float impulseMagnitude = contactPoint.impulse.magnitude; collisionSurfaceNormal += contactPoint.normal; collisionSurfacePoint += contactPoint.point; } collisionSurfaceNormal /= collision.contactCount; collisionSurfacePoint /= collision.contactCount; collisionSurfaceNormal = transform.InverseTransformDirection(collisionSurfaceNormal); collisionSurfacePoint = transform.InverseTransformPoint(collisionSurfacePoint); gizmoSurfaceNormal = collisionSurfaceNormal; gizmoSurfacePoint = collisionSurfacePoint; Vector3 oldPosition = collision.body.position; Quaternion oldRotation = collision.body.rotation; collision.body.position = transform.InverseTransformPoint(collision.body.position); collision.body.rotation = Quaternion.Inverse(transform.rotation) * collision.body.rotation; VertexGroup[] groups = meshGraph.groups; for (int i = 0; i < groups.Length; i++) { VertexGroup current = groups[i]; Vector3 vertex = current.pos; if (IsBeyondCollisionSurface(collisionSurfaceNormal, collisionSurfacePoint, vertex)) { if (isInconvenient || collision.collider.ClosestPoint(vertex) == vertex) { Vector3 deformation = DeformationFromCollisionSurface(collisionSurfaceNormal, collisionSurfacePoint, vertex); // if (addNoise) deformation *= Random.value * multiplier + addition; current.MoveBy(vertices, deformation, false); current.SetWasMoved(teamId, true); if (!current.GetEnqueued(teamId)) { vertexQueue.Enqueue(current); current.SetEnqueued(teamId, true); // Debug.Log("Vertex group " + current.vertexIndices[0] + " enqueued due to collision"); } } } } collision.body.position = oldPosition; collision.body.rotation = oldRotation; dissipationNeeded = true; meshDeformationMarker.End(); } } }