Skip to content

Commit

Permalink
#2 ArticulationBody support, samples
Browse files Browse the repository at this point in the history
  • Loading branch information
mitay-walle committed Oct 16, 2022
1 parent 4d2f7b0 commit 952e3a4
Show file tree
Hide file tree
Showing 6 changed files with 1,186 additions and 69 deletions.
6 changes: 3 additions & 3 deletions Runtime/Optional/Physics2D/PhysSoundObject.2D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public void OnCollisionStay2DInternal(Collision2D c)

if (_setPrevVelocity)
{
_prevVelocity = _r2D.velocity;
_prevVelocity = _rigidbody2D.velocity;
_setPrevVelocity = false;
}

Vector3 deltaVel = _r2D.velocity - (Vector2) _prevVelocity;
Vector3 deltaVel = _rigidbody2D.velocity - (Vector2) _prevVelocity;

if (_collisionEvents.PlayOnCollisionStay)
PlayImpactSound(c.collider, deltaVel, c.GetContact(0).normal, c.GetContact(0).point);
Expand All @@ -48,7 +48,7 @@ public void OnCollisionStay2DInternal(Collision2D c)
false);
}

_prevVelocity = _r2D.velocity;
_prevVelocity = _rigidbody2D.velocity;
}

public void OnCollisionExit2DInternal(Collision2D c)
Expand Down
11 changes: 8 additions & 3 deletions Runtime/Optional/Physics3D/PhysSoundObject.3D.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,18 @@ public void OnCollisionStayInternal(Collision collision)
_audioContainersMap == null)
return;

Vector3 velocity = default;

if (_rigidbody) velocity = _rigidbody.velocity;
if (_articulationBody) velocity = _articulationBody.velocity;

if (_setPrevVelocity)
{
_prevVelocity = _r.velocity;
_prevVelocity = velocity;
_setPrevVelocity = false;
}

Vector3 deltaVel = _r.velocity - _prevVelocity;
Vector3 deltaVel = velocity - _prevVelocity;

if (collision.contactCount > 0)
{
Expand All @@ -71,7 +76,7 @@ public void OnCollisionStayInternal(Collision collision)
_contactPoint,
false);

_prevVelocity = _r.velocity;
_prevVelocity = velocity;
}

public void OnCollisionExitInternal(Collision c)
Expand Down
9 changes: 5 additions & 4 deletions Runtime/PhysSoundObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ public partial class PhysSoundObject : PhysSoundObjectBase
override protected void Initialize()
{
#if PHYS_SOUND_3D
_r = GetComponent<Rigidbody>();
if (_r)
_rigidbody = GetComponent<Rigidbody>();
_articulationBody = GetComponent<ArticulationBody>();
if (_rigidbody || _articulationBody)
{
ValidateComponent<PhysSoundObject3DTriggerExit>(_collisionEvents.SlideOnTriggerExit);
ValidateComponent<PhysSoundObject3DTriggerStay>(_collisionEvents.SlideOnTriggerStay);
Expand All @@ -38,8 +39,8 @@ override protected void Initialize()

#endif
#if PHYS_SOUND_2D
_r2D = GetComponent<Rigidbody2D>();
if (_r2D)
_rigidbody2D = GetComponent<Rigidbody2D>();
if (_rigidbody2D)
{
ValidateComponent<PhysSoundObject2DTriggerExit>(_collisionEvents.SlideOnTriggerExit);
ValidateComponent<PhysSoundObject2DTriggerStay>(_collisionEvents.SlideOnTriggerStay);
Expand Down
5 changes: 3 additions & 2 deletions Runtime/PhysSoundObjectBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ private void Start()
protected abstract void Initialize();

#if PHYS_SOUND_3D
protected Rigidbody _r;
protected Rigidbody _rigidbody;
protected ArticulationBody _articulationBody;
protected void PlayImpactSound(Collider other, Vector3 relativeVelocity, Vector3 normal, Vector3 contactPoint)
{
if (!_delay.IsReady() || SoundMaterial == null || !enabled || SoundMaterial.AudioSets.Count == 0 ||
Expand All @@ -61,7 +62,7 @@ protected void PlayImpactSound(Collider other, Vector3 relativeVelocity, Vector3
#endif

#if PHYS_SOUND_2D
protected Rigidbody2D _r2D;
protected Rigidbody2D _rigidbody2D;
protected void PlayImpactSound(Collider2D other, Vector3 relativeVelocity, Vector3 normal, Vector3 contactPoint)
{
if (!_delay.IsReady() || SoundMaterial == null || !enabled || SoundMaterial.AudioSets.Count == 0 ||
Expand Down
4 changes: 2 additions & 2 deletions Runtime/PhysSoundObjectLite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ private void FixedUpdate()
override protected void Initialize()
{
#if PHYS_SOUND_3D
_r = GetComponent<Rigidbody>();
_rigidbody = GetComponent<Rigidbody>();
#endif
#if PHYS_SOUND_2D
_r2D = GetComponent<Rigidbody2D>();
_rigidbody2D = GetComponent<Rigidbody2D>();
#endif

if (_autoCreateSources)
Expand Down
Loading

0 comments on commit 952e3a4

Please sign in to comment.