VR Development Framework
v 1.0.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events Pages
void AVR.Phys.AVR_Grabbable.OnCollisionEnter ( Collision  collision)
inlineprivate

Definition at line 312 of file AVR_Grabbable.cs.

313  {
314  // Haptic feedback
315  foreach(AVR_BasicGrabProvider h in AttachedHands)
316  {
317  h.controller.HapticPulse(Mathf.Lerp(0, 0.5f, 0.2f * collision.relativeVelocity.magnitude), 0.05f);
318  }
319 
320  //Play collision sound
321  if (source != null && objectType.soundData.collideSound != null && rb != null)
322  {
323  //Calculate a sound multiplier based off the velocity and mass of the collision
324  float soundMultiplier = objectType.soundData.volumeMultiplier;
325 
326  float mass = rb.mass;
327  float speed = rb.velocity.magnitude;
328  float drag = rb.drag == 0.0f ? 0.5f : rb.drag; //If the rigidbody drag is 0, use 0.5. Otherwise, use that drag.
329 
330  const float frontalArea = 0.5f;
331  const float airDrag = 1.1f;
332 
333  //Equation adapted and modified from https://www.grc.nasa.gov/WWW/K-12/rocket/termvr.html
334  float terminalSpeed = Mathf.Sqrt(2.0f * mass * Physics.gravity.magnitude / airDrag * frontalArea * drag);
335 
336  //When you grab an object, it teleports.
337  //This creates these wildly large speed values when grabbed. This will ignore them.
338  if (speed > terminalSpeed)
339  {
340  return;
341  }
342 
343  //Using the adapted terminal velocity, create a ratio from 0 - 1 of speed.
344  //Use this ratio as the sound multiplier. The faster the object moves, the louder the sound.
345  float physicsSoundMultiplier = speed / terminalSpeed;
346 
347  soundMultiplier *= physicsSoundMultiplier;
348 
349  source.PlayOneShot(objectType.soundData.collideSound, soundMultiplier);
350  }
351  }
AudioSource source
Optional AudioSource to play sounds from GrabbableObjectType data
List< AVR_BasicGrabProvider > AttachedHands
List of hands that are currently grabbing this object.
GrabbableObjectType objectType
Type that describes the objects behaviour when grabbed.
Rigidbody rb
Rigidbody of this grabbable object.
GrabbableObjectSoundData soundData