How do I detect the direction of a vehicle collision?

Hello. I have been trying to detect if the part a vehicle collided with is on the front, rear, right or left of the vehicle. I tried to achieve this through Vector distance and then through dot, but both attempts failed. I don’t know how I can achieve that properly.
Below is my latest attempt, using dot. The dot results are strange and I don’t think I work with them.

hitbox.Touched:Connect(function(hit)
	if debounce == true then
		debounce = false
		print("rammed " .. hit.Name)
		local distance = pivot.Position - hit.Position -- this is from when i tried vector distance instead of dot
		local frontdot = hitboxes.Body.CFrame.LookVector.Unit:Dot(hit.Position.Unit)
		print(frontdot)
		-- i was about to make the rightdot with RightVector until i saw that the frontdot wasn't giving good results
		task.wait(1)
		debounce = true
	end
end)

By the way, I only wanted one result; front, rear, left or right. The vector distance method was kind of working, but it was giving out two directions instead of just one.

Another note, I’m worried about Dot giving undesired results if the part the vehicle collided with is facing a different direction. I want to detect a front collision if the vehicle is facing the general position of the part, and I want to detect a left collision if the vehicle has its left turned to the position of the part. This is another problem I’m having with Dot, I don’t care what direction the part is facing, only the vehicle’s direction.

2 Likes

I still can’t get consistent and proper results out of Dot().
If I set it like this:

local frontdot = hitboxes.Body.CFrame.LookVector.Unit:Dot(hit.CFrame.LookVector.Unit)

I get results based on whether both the vehicle and the part are facing each other, but as I noted, I don’t care about what direction the part is facing. All I care about is what direction of the vehicle is facing the part, and that’s it. I just don’t know how to do it.

1 Like

I guess I’ll just leave collisions on hold for now and work on something else.

1 Like

You probably want this:
local frontdot = hitboxes.Body.CFrame.LookVector.Unit:Dot((hit.Position- hitboxes.Body.Position).Unit)

The dot product of vehicle pointing direction and the hit position’s direction from the world origin is not useful, you want the hit object’s position relative to the vehicle. When that works, do your RightVector check the same way.

1 Like

This worked perfectly. However, now I need to determine whether the hit was leaning more to the front / rear, or left / right. I did this:

		if frontdot < 0 then
			print("front")
		elseif front> 0 then
			print("rear")
		end
		if sidedot > 0 then
			print("right")
		elseif sidedot < 0 then
			print("left")
		end

And it prints out exactly where it hit the object from, but I need one direction.

To get which side the collision is on, you need to compare the front and side dot products to the dot product of the center of your car to the corners. For example, if math.abs(frontdot) is greater than the magnitude of the dot product of the car LookVector with the direction to the front right corner of the car. You can just hardcode the dot product of a front and rear corner of the car with the car look vector, or you can put attachments or something at the corners and do the math at runtime.

If the car were perfectly square, you could just check math.abs(frontdot) >= math.abs(rightdot), but that doesn’t work if the car is not square. Really, you care about which of 4 regions the hit position is in, where the regions are the wedges between the car’s diagonals.