How to create realistic sonic booms?

Hey, I am developing a weapon system for my game. I’m currently trying to simulate a sonic boom
or a cracking noise produced by a bullet. I know the players’ position, the bullet velocity, and the bullet position.

image
This image illustrates the effect I want to achieve, but I have no idea how to replicate this effect in Roblox.

Any help is greatly appreciated :smiley:

The way to achieve these Doppler shifts, you will play the sounds from the client and not server, so everyone will hear differently.

Hint: Expanding spheres’ central point moves forward.

1 Like

Visually, it’s a cylinder that expands and fades into transparency. You could use beams, particle emitters, really there’s quite a few options depending on how you’re handling bullets.

Hey, thank you for responding :smiley:
I’m handling the bullet on the client so that wouldn’t be a problem.

I don’t think actually creating sphere parts would be the best solution, but is there any way to do this with just code and math? I’m trying to figure out when the observer should hear the supersonic boom :thinking:
boom

You can use the speed of sound and the position of the plane to calculate when the noise will reach a position (in this case the position of a player)

EDIT:

Here’s how:

The speed of sound is 340 m/s.

You can calculate the time by using this equation:

image

Also, we calculate the distance by using the difference between each position.

Quick script to calculate it and print it:


local speedOfSound = 340 -- Speed of sound
local position = Vector3.new(0,5,7) -- Position of plane
local playerPosition = Vector3.new(7,5,7) -- Position of player

while wait() do
	position += Vector3.new(3,0,0) -- Movement of plane
	local Time = Vector3.new(math.abs((position.X-playerPosition.X))/speedOfSound,math.abs((position.Y-playerPosition.Y))/speedOfSound,math.abs((position.Z-playerPosition.Z))/speedOfSound) -- Calculating time in 3 dimensions
	print(Time) -- printing
end

1 Like

How would I do this when the source is moving and exceeding the speed of sound?

1 Like

I just edited it. Not sure if it’s what you wanted.

1 Like

Thank you :smiley:
I’m not sure what the Time variable does. Also, does this take into account that the bullet is moving faster than 340 m/s?

It’s pretty much how sound works (not talking about frequency) so, yes.

1 Like

The time variable is how long it takes until it reaches the player in 3 dimensions.

1 Like

So would I just get the magnitude and play the sound when it reaches 0?

Kind of. You would use the distance magnitude and delay the sound.

Let me explain with a script:

local speedOfSound = 340 -- Speed of sound
local position = Vector3.new(0,5,7) -- Position of plane
local playerPosition = Vector3.new(7,5,7) -- Position of player
local RunService = game:GetService("RunService")


local function delay(n) -- more precise delay
	n = n or 0.05
	local startTime = os.clock()

	while os.clock() - startTime < n do 
		RunService.Stepped:Wait()
	end
end

while wait() do
	position += Vector3.new(3,0,0) -- Movement of plane
	local Time = math.abs(position.Magnitude-playerPosition.Magnitude)/speedOfSound -- Calculation of how long it should take until you hear the noise
	delay(Time) -- Delay
	-- Turn on sound
end

Modify it for your case.

2 Likes

Hey, sadly I didn’t get your script to work.

I tried following this video and managed to recreate the effect I want in GeoGebra. However, I don’t know how I would do the same in Roblox’s 3D environment.

Here’s what I did in GeoGebra:


image

I don’t know if this is important, but my bullets follow a curve, not a straight line :slightly_smiling_face:

I see. I used this and it worked in my case.

It calculates the distance between the Player (workspace.Player.Position) and the Plane (workspace.Plane.Position) then calculates the time it should take (Distance/Velocity), waits the amount of time it should take then plays the Sonic Boom sound (workspace.SonicBoom).

Hope this works.

local distance = math.abs((workspace.Player.Position-workspace.Plane.Position).Magnitude) -- Distance between player and plane.
local velocity = 510 -- Mach 1.5 (1.5*340 = 510)
local Time = distance/velocity -- Time it should take until you hear the sound.

wait(Time) -- Waits the amount of time it should take.
workspace.SonicBoom:Play() -- Plays the sound.