Bullets through objects without rays?

the basic info known would be the depth of the surface it hit, where on the surface it hit, and the angle it entered the surface at. as shown in the screenshot below for referance and some sloppy sketch.

what im trying to find out is where it would exit with said angle of entry needing it for a projectile system. as seen in the picture below this is what im trying to find out.

what i thought may would was a hypotenuse solution but that would be for a 2d surface like this.

and so my other idea is possibly pythagorean theorem with 3d shapes although its proving challenging and i’m not even sure if it would work as a solution to my issue reference below.

hopefully someone on here would be willing to help with this and provide a simplified solution thank you.
-edited to make title more accurate to what i’m after


You can get the angle using the dot product of the ray direction and the surface normal (both as a unit vector).

why not use rays because your maths will not work for other shapes other then blocks and even the corners of the block would be very complex

If you raycast inside of an object, it’ll ignore whatever it is currently inside of.

If you’d like to know the distance traveled while inside of the block, what you can do is follow the original raycast out and then raycast in reverse, then comparing the hit points from the normal ray and the reversed ray.

i should have been more clean on this part but i don’t mind using rays in all but they become rather tricky when going through objects and surfaces so i wouldn’t mind using a ray for like getting the angle and where it hit on what surface but past that point it would get over complicated

thats actually a pretty good idea tbh and to make it performance friendly and fast it wouldn’t be to bad would you also happen to know that speed that could happen at because I’m needing it to be within less then 1/60th of a second or like on update frames

if you wouldn’t mind elaborating on this it would be quite nice I’m not heavily experienced when it comes to math like that

Lol. This is exactly what I’m testing right now. I love problems that make you think.

1 Like

As long as you create new RaycastParams that have the block as the only whitelisted object, it’ll be very performance friendly. Unfortunately I don’t believe that there’s any other way if you want to calculate the distance inside of unions or meshes, so this would really be your only option.

i had no intentions of it working with different shape surfaces but it would be quite useful to have as well and as for the speed it could happen at and any tips for working with rays would be nice i have not used them a ton but i do know how to use them non the less so like how to take care of them after they are used to keep away from memory leaks and such

also would it be possibly to have a math solution just out of curiosity im more leaning to rays now but just out of my own interest yk

rays would be a lot more simple let me show you how

-- setup the first raycast
local raycastParams1 = RaycastParams.new()
local rayStartPosition1 = vector3.new(0, 0, 0)
local rayDirection1 = Vector3.new(0, 0, -1000)
local raycastResult1 = workspace:Raycast(rayStartPosition1, rayDirection1, raycastParams1)
if raycastResult1 == nil then return end

-- work out how far we need to move to get to the other side of the part
local maxSize = math.max(raycastResult1.Instance.Size.X, raycastResult1.Instance.Size.Y, raycastResult1.Instance.Size.Z)

-- setup the second raycast
local raycastParams2 = RaycastParams.new()
raycastParams2.FilterDescendantsInstances = {raycastResult1.Instance}
raycastParams2.FilterType = Enum.RaycastFilterType.Whitelist
local rayStartPosition2 = raycastResult1.Position + rayDirection1.Unit * maxSize * 2
local rayDirection2 = -rayDirection1.Unit * maxSize * 2
local raycastResult2 = workspace:Raycast(rayStartPosition2, rayDirection2, raycastParams2)

-- print the enter and exit positions
print(raycastResult1.Position, raycastResult2.Position)

Raycasts don’t help too much with memory leaks. But I would generally try to let the built-in functions do most of the heavy lifting as they’re usually very fast. If you don’t have a whitelist of allowed objects to raycast, the raycast will have to consider more and more parts as the raycast gets longer. Having lots of objects to consider will slow down the raycast function.

As for a math solution I don’t believe I can do that right now due to having to consider an objects rotation and such. Even though I don’t have the math solution, I would generally try to keep the underlying engine code do most of the heavy lifting until there’s a clear problem occurring.

ight thanks for you insightful answers on this ill be sure to give your idea a try and let you know how it goes

1 Like

interesting and simplified ill give this a try quickly thank you

alright i tried that and thats pretty much what i was after for the most part and can be kept quite clean so thank you so much for that 5uphi the solution is clean, simple and easy so thank you for you insightful knowledge and help with this

no problem :slight_smile: i happy it works