How to correctly calculate part thickness when penetrated by a 'raycast'

Before I start, I want to mention that I am using FastCastRedux to handle projectiles and penetration.

I am trying to determine the following values: ExitPosition: Vector3 & Thickness: number. I am attempting to modify the projectile’s velocity after it penetrates an object, based on its thickness (and material, but that’s not an issue). The issue is I’m not quite sure how I would go about calculating this. My code is as follows:

newCaster.RayPierced:Connect(function(activeCast, raycastResult: RaycastResult, segmentVelocity: Vector3, cosmeticBulletObject: BasePart?)
	local hitInstance: BasePart = raycastResult.Instance
	local hitNormal: Vector3 = raycastResult.Normal
	local hitPosition: Vector3 = raycastResult.Position
			
	local exitPosition: Vector3?, thickness: number?
			
	-- some code to calculate exitPosition, so I can calculate thickness

	thickness = (exitPosition - hitPosition).Magnitude

	--activeCast:SetPosition(hitPosition)
	local readyThickness = thickness * 1.07
			
	activeCast:AddVelocity(Vector3.new(readyThickness, readyThickness, readyThickness))
	RenderVFX.Debug.RenderPoint(hitPosition, Color3.fromRGB(68, 255, 35))
end)

The goal is to have the thickness automatically calculated based on the size of the object, which I know you could do by taking the longest axis, but this isn’t feasible for the projectiles as they can come from different angles and directions.

I did try a few solutions, but all seemed to lead away from what I was actually trying to achieve, any help is much appreciated, thanks.

2 Likes

To calculate the exit position and thickness of the projectile after it penetrates an object, you can use the hitPosition and exitPosition vectors. Here’s a possible approach to calculate these values:

newCaster.RayPierced:Connect(function(activeCast, raycastResult: RaycastResult, segmentVelocity: Vector3, cosmeticBulletObject: BasePart?)
	local hitInstance: BasePart = raycastResult.Instance
	local hitNormal: Vector3 = raycastResult.Normal
	local hitPosition: Vector3 = raycastResult.Position

	local exitPosition: Vector3?
	local thickness: number?

	-- Calculate exitPosition using your desired method

	-- Example: If the projectile is traveling in the direction of hitNormal, you can calculate the exitPosition by adding the hitPosition and a small offset in the hitNormal direction
	local penetrationDepth = 1 -- Adjust this value as needed
	exitPosition = hitPosition + hitNormal * penetrationDepth

	thickness = (exitPosition - hitPosition).Magnitude

	local readyThickness = thickness * 1.07

	activeCast:AddVelocity(Vector3.new(readyThickness, readyThickness, readyThickness))
	RenderVFX.Debug.RenderPoint(hitPosition, Color3.fromRGB(68, 255, 35))
end)

3 Likes

since the raycast could hit things of all different shapes and sizes, I’d fire another raycast to determine the exitPosition (unless you want to deal with tons of trigonometry). The simplest way to check would be to take the longest path possible, and fire another ray back at hitPosition, starting forward of hitposition.

local hitPostion = Raycast.Position

local MaximumTravel = math.sqrt(Raycast.Instance.Size.X^2, Raycast.Instance.Size.Y^2, Raycast.Instance.Size.Z^2)--every BasePart has a size, it the minimum square that incloses the object

local endPositionRayOrigin = hitPostion + (hitPostionDirection.Unit * MaximumTravel)--hitPositionDirection is the direction of the raycast that found hitPosition

local endPosition = workspace:Raycast(endPositionRayOrigin, hitPostion - endPositionRayOrigin)
--endPosition.Position returns the exit hole's position

This code is untested, but it should give you an idea of what to do. hope this helps! :grin:

2 Likes

In my current case, @F0ZYAN’s solution will work, since I can calculate the maximum penetration depth based on the projectile’s velocity, then whitelist raycast back towards the hitPosition, if it hits, I get the exitPosition, if it doesn’t, I don’t have enough velocity to make it through the object, therefor ending the projectile’s life; this works great for bullets in this context, since I’ll be lowering the velocity after each penetration. Though when it comes to other types of projectiles that will interact with more complex 3D items, I will most likely implement your solution.

1 Like

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