public void ShootSphere() { var sphere = GameObject.Instantiate(m_prefab, m_camera.transform.position, m_camera.transform.rotation) as GameObject; var rigidbody = sphere.GetComponent <PSI.Rigidbody>(); var collider = sphere.GetComponent <PSI.Collider>(); rigidbody.mass = m_mass; rigidbody.drag = m_drag; rigidbody.angularDrag = m_angularDrag; var material = new PSI.PhysicsMaterial(); material.kineticFriction = m_kineticFriction; material.staticFriction = m_staticFriction; material.restitution = m_restitution; material.frictionCalculation = m_frictionCombine; material.restituionCalculation = m_restitutionCombine; collider.material = material; var forcePoint = rigidbody.position - sphere.transform.forward + sphere.transform.up; rigidbody.AddForceAtPoint(new Vector3(m_x, m_y, m_z), forcePoint, m_forceMode); }
private void ImpulseResponse(Collider a, Collider b, Vector3 direction, float intersection, Vector3 contactPoint, bool initialResponse) { // Cache the rigidbodies since we'll use them a lot. var rigidbodyA = a.attachedRigidbody; var rigidbodyB = b.attachedRigidbody; // Determine the surface properties that should be applied to both objects. var surfaceA = new PhysicsMaterial.Result(); var surfaceB = new PhysicsMaterial.Result(); PhysicsMaterial.Combine(a.material, b.material, out surfaceA, out surfaceB); // Correct the linear and rotational motion of the object. if (initialResponse) { // Adjust the position. //DynamicPositionCorrection (a.attachedRigidbody, b.attachedRigidbody, direction, intersection); CorrectLinearMotion(a.attachedRigidbody, b.attachedRigidbody, surfaceA, surfaceB, direction); CorrectAngularMotion(a.attachedRigidbody, b.attachedRigidbody, surfaceA, surfaceB, direction); } // Apply friction. ApplyFriction(rigidbodyA, surfaceA.staticFriction, surfaceA.kineticFriction, contactPoint); ApplyFriction(rigidbodyB, surfaceB.staticFriction, surfaceB.kineticFriction, contactPoint); }
private void StaticResponse(Collider dynamic, Collider stationary, Vector3 direction, float intersection, Vector3 contactPoint, bool initialResponse) { // Cache the rigidbody as we'll be using it a lot. var rigidbody = dynamic.attachedRigidbody; // Get the surface properties. var staticFriction = PhysicsMaterial.CalculateStaticFriction(dynamic.material, stationary.material); var kineticFriction = PhysicsMaterial.CalculateKineticFriction(dynamic.material, stationary.material); var restitution = PhysicsMaterial.CalculateRestitution(dynamic.material, stationary.material); if (initialResponse) { // Ensure the object is moved away. //StaticPositionCorrection (rigidbody, direction, intersection); // We assume the static object has a mass so great it can't be moved and just scale by restitution. var momentumDirection = Vector3.Reflect(rigidbody.momentum, direction); rigidbody.momentum = momentumDirection * restitution; // Now do the same for angular momentum. var angularDirection = Vector3.Reflect(rigidbody.angularMomentum, direction).normalized; rigidbody.angularMomentum = -angularDirection * restitution; } // Apply friction. ApplyFriction(rigidbody, staticFriction, kineticFriction, contactPoint); }
public static float CalculateRestitution(PhysicsMaterial lhs, PhysicsMaterial rhs) { if (lhs) { return(lhs.CalculateRestitution(rhs)); } if (rhs) { return(rhs.restitution); } return(1f); }
public static float CalculateStaticFriction(PhysicsMaterial lhs, PhysicsMaterial rhs) { if (lhs) { return(lhs.CalculateStaticFriction(rhs)); } if (rhs) { return(rhs.staticFriction); } return(0f); }
/// <summary> /// Attempts to obtain the PhysicsMaterial for the Collider, attach itself to a Rigidbody and find a Physics /// system that can be registered with. /// </summary> virtual protected void OnEnable() { m_attachedRigidbody = FindAttachableRigidbody(); material = material ?? ObtainMaterial(); UpdateRigidbodyInertiaTensor(); // For some reason Unity is changing the object without destroying it so we have to handle that situation. if (!m_physics) { m_physics = Physics.FindSystem(); } m_physics.Register(this); Assert.IsNotNull(m_physics, "Collider couldn't find a Physics system to register with."); }
/// <summary> /// Combines the values of two surfaces together, outputting the results to Result a and b. /// </summary> /// <param name="lhs">A material to be combined.</param> /// <param name="rhs">A material to be combined./</param> /// <param name="a">The resulting surface properties for "lhs".</param> /// <param name="b">The resulting surface properties for "rhs".</param> public static void Combine(PhysicsMaterial lhs, PhysicsMaterial rhs, out Result a, out Result b) { if (lhs) { a.kineticFriction = lhs.CalculateKineticFriction(rhs); a.staticFriction = lhs.CalculateStaticFriction(rhs); a.restitution = lhs.CalculateRestitution(rhs); if (rhs) { b.kineticFriction = rhs.CalculateKineticFriction(lhs); b.staticFriction = rhs.CalculateStaticFriction(lhs); b.restitution = rhs.CalculateRestitution(lhs); } // Use the surface properties of "a" if "rhs" is null. else { b = a; } } // Swap the parameters if "lhs" is null and "rhs" isn't. else if (rhs) { Combine(rhs, lhs, out b, out a); } else { // Set restitution to 1.0 by default. a = new Result(); a.restitution = 1f; // Have "b" copy "a". b = a; } }
/// <summary> /// Calculates the resitution for the current object based on the other material given. /// </summary> /// <returns>A calculated restitution value.</returns> /// <param name="other">The other surface, if null then no calculation will be performed.</param> public float CalculateRestitution(PhysicsMaterial other) { return(other ? Calculate(restitution, other.restitution, restituionCalculation) : restitution); }
/// <summary> /// Calculates the static friction co-efficient for the current surface based on the other material given. /// </summary> /// <returns>A calculated static friction co-efficient.</returns> /// <param name="other">The other surface, if null then no calculation will be performed.</param> public float CalculateStaticFriction(PhysicsMaterial other) { return(other ? Calculate(staticFriction, other.staticFriction, frictionCalculation) : staticFriction); }
/// <summary> /// Calculates the kinetic friction co-efficient for the current surface based on the other material given. /// </summary> /// <returns>A calculated kinectic friction co-efficient.</returns> /// <param name="other">The other surface, if null then no calculation will be performed.</param> public float CalculateKineticFriction(PhysicsMaterial other) { return(other ? Calculate(kineticFriction, other.kineticFriction, frictionCalculation) : kineticFriction); }