This thread is designed to provoke discussion and contribution, so that we can collectively design an optimal solution. No promises, but I’ll likely end up open sourcing a module that uses what we come up with.
I want to create a system to add dynamic sound effects when objects collide. It should preferably take relative velocity, material, and mass into account when determining the sound being played.
First, let’s break the problem down into chunks.
- Detect collision
- Calculate various parameters
- Play determined sound
Now that we know exactly what we need to do, let’s discuss how we could do it.
I’ve come up with multiple possible methods, but I’m uncertain which is best, and I’ve run into some annoyances as well. Some are clearly worse than others, but I’m putting them here for sake of completion. Note that I have not attempted to program any of these yet, they are simply ideas for now.
Method A:
Detecting sudden changes in current velocity could indicate when a collision has occurred. Sudden stops and starts, etc. The amount changed would tell us the strength of the hit, so we could alter the volume of the SFX accordingly, giving a more dynamic feel.
Problem is, it would likely lead to false positives, doesn’t give us material data, and requires us to track and store velocity in order to know when collisions have occurred.
Method B:
Use :GetTouchingParts() and detect new entries as collisions. Calculate the relative velocity and total mass of the two objects for volume. Have sounds for MaterialA+MaterialB and play the sound that matches the two materials. This one would require constant polling, but it would give material data that the previous method could not. (Although the materials are a problem discussed further down)
Method C:
Using the .Touched event. When touched, calculate the relative velocity between the hit object and the main object, and use that to determine volume. You can also weigh in the combined mass of the objects in your volume calculation. Have sounds for MaterialA+MaterialB and play the sound that matches the two materials.
I’m sure most of you instantly thought of this, as did I, but the context of my game brings up a separate issue, and this issue is what led me to even start this post in the first place.
My game uses Textures, rather than the default Roblox materials. They’re often just set to Fabric to remove shine and highlights. Therefore, calculating the parameters becomes more complex. I need to mark the objects with custom metadata, and I’m uncertain what the best way to go about this is.
We could use Attributes (jk, they’re coming soon™), CollectionService Tags, ValueObjects, or a part naming system which we do some string manipulation on.
All of these require me to manually set up hundreds upon hundreds of parts in my game by hand, which I want to avoid. Also, using part names to indicate properties would lead to a messy workspace.
What do you guys suggest?