How to make crash physics?

How could I go about making crash physics?

To be more specific, I want to detect how hard the hit is, and depending on how hard the hit was either make something explode, go on fire or break welds.

Let me know if I should be more specific.

Any help is appreciated!

2 Likes

You can see the speed of the bodies in the direction of both and add their magnitudes to calculate the size of the explosion.

1 Like

Could you elaborate on this?

need more character in post

Here I leave you the main part and I attach an example.

part.Touched:Connect(function(hit)
	local p = (part.Position + hit.Position) / 2
	local int = (part.Velocity - hit.Velocity).Magnitude
	local exp = Instance.new("Explosion")
	exp.Position = p
	exp.BlastRadius = int
	exp.BlastPressure = int
	exp.Parent = game.Workspace
end)

ExplosionSegunVelocidad.rbxl (36,0 KB)

1 Like

The BlastPressure must be large so you can multiply it by a fixed value.

exp.BlastPressure = int * 5000
1 Like

I attach again the example changing the velocity of the bodies.

local part = game.Workspace.P1
local vel = 10
local fact_radius = 2
local fact_pressure = 10

part.Touched:Connect(function(hit)
	local p = (part.Position + hit.Position) / 2
	local int = (part.Velocity - hit.Velocity).Magnitude
	local exp = Instance.new("Explosion")
	print(int)
	exp.Position = p
	exp.BlastRadius = int * fact_radius
	exp.BlastPressure = int * fact_pressure
	exp.Parent = game.Workspace
end)

wait(20)

game.Workspace.P1.Velocity = Vector3.new(vel, 0, 0)
game.Workspace.P2.Velocity = -Vector3.new(vel, 0, 0)

ExplosionSegunVelocidad.rbxl (36,1 KB)

if you use RigidConstraint

there are some Destruction properties to make it break depending on force

2 Likes

There isn’t many tutorials on this, could you give some use case examples?

The formula for calculating force is Force = Mass * Change in Velocity . You can get the velocity of a part using the Velocity property.

Here’s a bit of example code that should help get you started in the right direction:

local function onCollision(otherPart)
    local impactThreshold = 1000 -- Adjust this value based on your needs

    -- Calculate impact force
    local velocity = vehiclePart.Velocity
    local mass = vehiclePart:GetMass()
    local impactForce = mass * velocity.Magnitude

    -- Check if impact force is above the threshold
    if impactForce >= impactThreshold then
        -- Perform actions based on impact force
        if impactForce >= explosionThreshold then
            -- Create an explosion
            local explosion = Instance.new("Explosion")
            explosion.Position = vehiclePart.Position
            explosion.BlastRadius = 10
            explosion.BlastPressure = impactForce / 1000 -- Adjust as needed
            explosion.Parent = game.Workspace
            explosion:Fire()
        elseif impactForce >= fireThreshold then
            -- Insert code to start a fire effect here
        else
            -- Insert code to break welds or other actions here
        end
    end
end

-- Connect the collision event
vehiclePart.Touched:Connect(onCollision)

Hope this helps!1!!!

1 Like

How could I detect if a different car hit my car at high speed and to break the parts on my car?

Also with the code you provided I’m not able to replicate the result onto a moving vehicle controlled by the player, it seems to work though on a free falling part in the workspace.

This is the part where you exercise creative liberty as a programmer, as there are several ways to detect a different car hitting yours. The code provided, as mentioned, is just some sample code to get you started in the right direction.

As stated, it is sample code that is best used for instruction and learning, and may need tweaking to fit your needs. You did not provide any sample code of your exact scenario, so I figured this was the best I could do.

1 Like

Nothing I tweak fixes it.

It may be that my code doesn’t work or that the functionalities aren’t being properly implemented, as the code is very minimal and may not have properly accounted for several variables I didn’t know about. I only code a little bit, and to my knowledge the code should be functional if not rudimentary, but my knowledge is limited because I mostly 3d model, so I do apologize for that.

1 Like

I found the problem, for some reason the code doesn’t work on parts which are grouped together, I’m not sure why this is happening.

I think you cant get the velocity of a grouped part?

1 Like

I got the solution from a different post:

It seems getting the velocity and mass of grouped parts uses a different property.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.