Calculating an objects velocity after hitting it with a raycast?

I am working on a radar system for a game I am working on and was just wondering what the easiest way for me to get an hit objects position and to the calculate its velocity after the initial hit.

The measurement we’ll use is going to be studs and the framework for casting these rays will be done via FastCast

A video example of how the system will cast its rays/function:

Any tools/frameworks or anything else you could recommend would help a lot

Have you considered recording the position of the hit object, then grabbing its position again after a period of time and substituting the values into the velocity formula: Velocity = Displacement/Time, or would that not work in your particular situation?

You can create a new thread with task.spawn after hitting an object to prevent the entire program from yielding when waiting to get the second position, if need be.

1 Like

Ive tried that but I feel like the math I have is off. Code and output below:

The code supplied is for testing.

local targetsF = game:GetService('Workspace'):FindFirstChild('targets')

local config = {
	Range = 15000, -- in studs
	MinSize = 5, -- size of targets
	Debug = true, -- debugging
	minVelocity = 10,
}

function getTargets()
	
	-- later to be used after initial testing
	
end

function CalculateVelocity(Before, After, deltaTime): Vector3
	
	local Displacement = (After - Before)
	local Velocity = Displacement / deltaTime

	return Velocity
end

game:GetService('RunService').Heartbeat:Connect(function(delta)
	
	for i,v: Part in pairs(targetsF:GetChildren()) do

		local last = v.Position
		task.wait(1.5)
		if last ~= v.Position then

			local newest = v.Position
			
			local velo = CalculateVelocity(last, newest, delta)
			
			print('Velocity: ', velo.Unit.Magnitude)
			
			if velo.Unit.Magnitude >= config.minVelocity then
				
				warn('Enemy craft detected: ', velo, 'Speed: '..velo.Unit.Magnitude)
				
			end
			
		else
			print(v.Position)
		end

	end
	
end)

Output:

  18:02:39.646  Velocity:  0.9999999403953552  -  Server
  18:02:39.662  Velocity:  1  -  Server
  18:02:39.712  Velocity:  0.9999999403953552  -  Server

Part move script:

local ti = TweenInfo.new(2, Enum.EasingStyle.Linear, Enum.EasingDirection.InOut)
local p = script.Parent

local studs = 50

game:GetService('RunService').Heartbeat:Connect(function(delta)
	
	local t = game:GetService('TweenService'):Create(script.Parent, ti, { Position = Vector3.new(p.Position.X + studs, p.Position.Y, p.Position.Z) })
	
	t:Play()
	
	t.Completed:Once(function(pb)
		
		if t.PlaybackState.EnumType == Enum.PlaybackState.Completed then
			
			t:Play()
			
		end
		
	end)
	
end)

I’m no mathematician, however, I believe the cause of your problems is that you are dividing the displacement position by deltaTime, as opposed to dividing displacement magnitude by deltaTime, so converting the resultant displacement position into a magnitude value first before dividing should give you the desired output.

1 Like

I suppose this worked but it seems values seem to be fairly jittery but I think it might be because of the tweening/generic behaviour.

one thing I have noticed is that when lowering the studs to 10 it gives an output of 50+ - 60+.
However looking through the print logs it seems to start at <1 then shoots up to a consistant 60 but again I believe this is due to the tweening/generic behaviour however but I am not sure of that.


  18:56:31.414  Velocity:  0.5038237801642473  -  Server - Script:31
  18:56:31.731  Velocity:  3.209465783961754  -  Server - Script:31
  18:56:31.897  Velocity:  6.102898557578108  -  Server - Script:31
  18:56:31.965  Velocity:  12.6531579836019  -  Server - Script:31
  18:56:31.965  Enemy craft detected:  12.6531579836019 Speed: 12.6531579836019  -  Server - Script:35

Code:

function CalculateVelocity(Before: Vector3, After: Vector3, deltaTime: number): number
	
	local Displacement = (After - Before)
	local Velocity = Displacement.Unit.Magnitude / deltaTime

	return Velocity
end

It is to be expected that it shoots up instantly to a constant value. You’re not including any acceleration when tweening objects, and TweenService works on calculating a constant velocity to move at to get to the destination within the exact specified time frame.

1 Like