Im trying to make an accurate collision so that if a spaceship collided each other or collided something else and is very fast, it will blow up
So far i tried this :
local diffspeed = (script.Parent.AssemblyLinearVelocity.Magnitude - Part.AssemblyLinearVelocity.Magnitude)
if math.abs(diffspeed) >= 500 then
print("blow up")
end
But i do realized that if the spaceship is facing the opposite way with what the spaceship is going to collide on and both the spaceship and what it’s gonna collide on has the same velocity, it wont print the “blow up” string
I do know that I need to include orientation for the diffspeed variable but I dont know how how to include it and what math is necessary. Thx
but the problem is that if the ship is facing the opposite orientation with the thing its going to collide, and both got the same velocity, it wont blow up
To take into account orientation, it’s going to be difficult within Roblox as we don’t know the surface normal and point where the objects collide as there is no API for that Unity does though . This is necessary for finding the impulse which is force multiplied by time.
Example google search:
Consequently, I’m going to make an assumption that the objects are spherical as the surface normal is easy to find from position of the objects
In order to find the momentum that is going to contribute towards the reaction force, vector projection will be utilized:
Where p1 = position, v1 = velocity, R1 is the surface normal.
Additionally, the smallest momentum will be given, ex the function will return R1 as momentum as that is the portion of the momentum that goes into collision and not translation movement.
--Projects vector a onto vector b, returns the magnitude
local function vectorProjectionAOntoBMagnitude(a : Vector3, b : Vector3)
local numeratorTerm = a:Dot(b)
local denominatorTerm = b:Dot(b)
return numeratorTerm/denominatorTerm
end
local function findCollisionMomentum(part1 : BasePart, part2 : BasePart)
local p1 = part1.Position
local p2 = part2.Position
local v1 = part1.AssemblyLinearVelocity
local v2 = part2.AssemblyLinearVelocity
local mass1 = part1:GetMass()
local mass2 = part2:GetMass()
local r1Direction = (p2-p1).Unit
local r2Direction = -r1Direction
local r1VelocityProjection = vectorProjectionAOntoBMagnitude(v1, r1Direction)
local r2VelocityProjection = vectorProjectionAOntoBMagnitude(v2, r2Direction)
--We only care about the + velocity going in the same direction as the reaction force, remove negative values
--Also converts it into a momentum, because mass matters
local r1NonZero = math.max(0, r1VelocityProjection)*mass1
local r2NonZero = math.max(0, r2VelocityProjection)*mass2
--Find the Minimum
local netVelocityDifference = math.min(r1NonZero, r2NonZero)
return netVelocityDifference
end
Also I just played with 2 parts for a few hours and just found a way to get the velocity between the 2 ships including when they are on a opposite orientation :
local part1 = game.Workspace.TestPartA
local part2 = game.Workspace.TestPartB
local v1 = part1.AssemblyLinearVelocity
local v2 = part2.AssemblyLinearVelocity
local diffspeed = (v1 - v2).Magnitude
print(diffspeed)
local batonName = “Baton” – Name of the hitting object
local forceMultiplier = 50 – Adjust force strength as needed
script.Parent.Touched:Connect(function(hit)
if hit.Parent and hit.Parent.Name == batonName then
local ball = script.Parent – The object being affected (assumes this script is on the ball)
-- Create a VectorForce
local vectorForce = Instance.new("VectorForce")
vectorForce.Name = "ShockForce"
-- Create an attachment for the ball
local attachment = Instance.new("Attachment")
attachment.Parent = ball
vectorForce.Attachment0 = attachment
-- Set VectorForce properties
vectorForce.RelativeTo = Enum.ActuatorRelativeTo.World
vectorForce.Parent = ball
-- Calculate the direction
local direction = (ball.Position - hit.Position).Unit -- From hit point to ball
local flatDirection = Vector3.new(direction.X, 0, direction.Z) -- Flatten direction (Y = 0)
vectorForce.Force = flatDirection * forceMultiplier
-- Optional: Remove the force after a short time
task.wait(0.5) -- Adjust duration as needed
vectorForce:Destroy()
attachment:Destroy()
end