Get part intersection length (Maths)

Hello devs !

I am currently implementing piercing to my gun engine, but got into some math problem,
I want to get the length of the intersection and get A into B (A is origin, B is the point were it gets out of the part) . More details there :

I need some help with the maths, I have the Normal Surface, Position (A), the axis hit (X, Y, Z) and CFrame. Adding C to origin A would get me to goal B.
Thanks !

1 Like

Can’t you apply pythagorean on the C side and wall depth?

Edit: I think you can get the length by doing (A-B).Magnitude assuming both A and B are Vector3 coordinates.

Problem is I don’t have B, only A

Which values are known to you?

I think you’d need the width of the wall. in which case it’s:

local wallVec = -norm
local p = A.Unit:Dot(wallVec)
local coeff = 1 / p
local C = A.Unit * coeff * wall.Width
local B = A + C

p is the percentage of the wall that the unit vector goes through. therefore you need to divide one by the percentage to get the coefficient for the full distance.

1 Like

my bad. if you’re wondering why it doesn’t work, I multiplied width with the wrong thing. edited it.

Thanks its working fine. Could I get an explanation on that maths tho ? (Sorry for late reply lol)

first, it gets the normal of wall backwards (so that it goes in the same direction as the bullet). the normal is already a unit vector (length of 1).

the dot product gives you the distance that one vector goes in the direction of another (|A| cos theta).

if you have two unit vectors A and B it actually becomes the percentage that A covers B. think about it: the percentage covered is usually p = (A dot B) / |B| but since |B| is just 1, it’s simply p = A dot B. in this case, A is the direction of the bullet and B is the reversed normal of the wall.

now to find the coefficient of A.Unit that would become C. we can invert a percentage by using it to divide 1. for example, if x is 50% of y, then 1 / 0.5 = 2; you have to multiply x by 2 to equal y. the same rule applies to vectors.

then we have C = A.Unit * (1 / p). however, this would only work on a wall that was one stud long since wallVec is one stud long. we have to multiply it by the width of the wall. so finally:

C = A.Unit * (1 / (A.Unit dot (-normal))) * width