Need help finding edges on a part

Hi, just want to say that I have searched the forum and even via other search engines, and either didn’t understand or wasn’t able to figure out how to implement it.
So all I want to do is check if a raycast hit position is close to either right or left edge on a surface as shown in the picture below…
image
-Sorry I suck at mouse drawing-
Any sort of help would be appreciated…

1 Like

You’d have to hard-code some position in and check their proximity with Vector3.FuzzyEq. ( Since you did not include the “back” of the part’s edge), otherwise, the edges of a part is given by (omitting the y-axis as it is unimportant)

(Assuming you didn’t rotate the z-axis; if you did you have to do some maths using trigonometry)

(pos.x-size.x/2, pos.z-size.z/2)
(pos.x-size.x/2, pos.z+size.z/2)
(pos.x+size.x/2, pos.z-size.z/2)
(pos.x+size.x/2, pos.z+size.z/2)

Just do if (x2-x1)^2 + (z2-z1)^2 < epilson^2 then where x1, z1 is the raycast intersection’s x and z position and x2, z2 is the edges’s x and z position

I might have an idea, do you know how to get the vertices of a part?
If so can’t I do something like this,
image
Depending on the x and z coordinates create a bound in which the hit position must be in?

So roblox doesn’t really have a way to get the vertices of a part. You can use the properties to calculate them yourself though. For a cube the simplest way being to add CFrame.LookVector*Size.Z/2 to the other vectors on that CFrame until you build what you need.

The way I would go about solving this problem is by taking the normal of the surface you hit
Then I would find which CFrame vector (Right, Up, Look) matches the normal direction. Making sure to also check if you need to negate those.

Then after I get which vector of the part I’m on, you can use the 2 other CFrame vectors that make up that face to determine where all of the edges are.

You take the Position of the part, then you add the CFrame vector * part.Size.CFrameVectorAxis/2 to find the middle of the surface of the part you hit.

Then you can take 1/2 of another CFrame Vector and add that to your center of the face to get the center of one of the edges. You can repeat that to find all four edges. Though since you only care about the sides you can determine which of the CFrames is the closest to being what you want and only calculate that.

This works fine when it comes to cubes, and you can get away with something similar on a couple of other things, but if you wanted this to work for something like meshes it would be a lot more difficult since as far as I’m aware you can’t easily get the geometry of a model through roblox apis.

I’m a little lost, could you provide some sort of example?
I’m not that great when it comes to geometry or trigonometry…
It would be helpful

I don’t get how CFrame.UpVector or CFrame.RightVector work, It would be nice if you can help the documentation is not really helping. Just a summary, I can get an understanding quickly that way.

So CFrame represents both the position and orientation of a part.

CFrame has a few properties you can access that are nice.
What CFrame.LookVector returns is a Vector3 that represents the direction you would travel to move in the direction the part is facing.
RightVector is the direction to the side of it and UpVector is the direction to go up relative to the part.

For example if I took a part and put a decal on the front surface, then randomly rotated it, but afterwards wanted to spawn a ball just in front of that decal on the part, I could use CFrame.LookVector to determine which direction I would have to travel from the center of that part to end up in front of that decal.

ball.Position = decalPart.Position + decalPart.CFrame.LookVector * 5 --the reason I'm multiplying is because lookVector gives me a direction to go in, but it is only 1 stud long.  So whatever I multiply it by tells it to move that many studs in that direction

Ah that makes so much more sense than that documentation, thanks!

Since there isn’t really a definitive answer yet I will try and create a function to do as @Feedekaiser and @tlr22 suggested similarly.