Make part at ground normal

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve? Keep it simple and clear!

This topic might be super simple for others tho I could not find any sources on how to make this. Making the part’s face perpendicular or oppose to the wall/floor, which the object the mouse is pointing to in the video.

  1. What is the issue? Include screenshots / videos if possible!
  1. What solutions have you tried so far? Did you look for solutions on the Developer Hub?

I know this might use normals using raycasts from mouse but i couldnt figure it how to do it or face the part.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

Thx for those who help

Assuming you already have casted the ray and have raycast.Position and raycast.Normal, you could position the part at CFrame.new(raycast.Position, raycast.Position + raycast.Normal) * CFrame.new(0,0,-CLEARANCE) where clearance is a number of how far off the ground you want your part
this is also assuming that the front face of your part is the side you want to be on top. otherwise, you would rotate the part by multiplying said CFrame by CFrame.Angles(math.rad(-90),0,0).

explanation: a normal value is the lookvector of the face the raycast hit, i.e. it is a vector which is one stud long and points out from the face. We position your part with a CFrame because unlike a vector3 it also contains a orientation component. the first parameter in a CFrame is the position, and the second is a position the cframe should look at, so we set it to the position + one stud out from the face.

2 Likes

Can check out this response from a few wks back.
The “Hacky test code used for video” has mouse raycast code similar to what’s done in the vid you posted. Might provide some ideas for place to start.

1 Like

in the code u provided, I don’t really know what cframe.frommatrix and :cross() methods does and the use of it. I know the cross method is used for finding the missing vector axis although I still don’t get it

Glad you found a solution. For clarity, the fromMatrix is just one of the CFrame constructors. You can think of it like using the version of the new constructor that takes all the individual args x,y,z,R00, R01, R02, etc, but it expects four Vector3 args instead of all the individual numbers.

The last img below shows the vector that corresponds to each column of the rotation matrix. In short, this constructor lets you create a CFrame using vectors for Position, RightVector, UpVector, and (-)LookVector (in that order).

The choice of that constructor was appropriate in the example because the code was using vectors for the facing direction and the surface normal, so it was straight-forward keep everything in terms of vectors and plug them into that constructor to get the new CFrame.

The Cross method is just like you said. The cross product of two vectors finds a third vector that is orthogonal to the first two, so, for example, it gives the RightVector if you know Up and Look.

All together these let you do things like take an object moving “that way” and use surface normals to rotate CFrames such that the obj tilts and rolls as it moves across the surface while maintaining the “that way” direction of travel.

at what scenerios can i use the method cross? It seems useless for me tho

It’s a vector math operator and can be used for calculating torque, among other things. Up above, it’s being used to generate vectors for the CFrame’s rotation matrix. Think of the x,y,z axes for the world. Each of those dimensions is supposed to be orthogonal, or perpendicular, to the others (at 90-deg angles). For example, the x-axis is at a 90-degree angle to the y-axis in the x,y plane.
image
Now suppose X represents the direction of travel and Y is the world up direction. If we want to tilt things so that the new up is parallel to a surface vector (or just something other than the world-up), we need a way to find the vector that represents “forward” but is also 90-deg to our new up.
image
A way to do that is by calc the cross product, twice. When we take the cross of our Forward with our desired up, we get another vector that is perpendicular to plane that the other two are in. This is our RightVector.
image
We can then take the cross product of that new RightVector with our desired up to get a corrected forward vector that is still pointing “forward” but is now perpendicular to our desired up and RightVectors.
image
Those three vectors represent the rotation matrix for a new CFrame, and can be combined with a Position vector in the CFrame.fromMatrix constructor to create a new CFrame.

So, the cross method takes two vectors and gives you another vector that is perpendicular to the plane that the other two are in.

Cross is how the surface normal is calculated in the first place. If the part of the surface you’re interested in is represented by a triangle (three points are co-planar), you can imagine vectors pointing from a common vertex to the other two points. Their cross product is the surface normal. Cross can basically be used wherever you need to find a vector that is perpendicular to a planar surface/two other vectors/three points in space.

There are lots of ways to construct CFrames for a desired rotation. You don’t have to do it this way if another approach does what you need.

1 Like

thanks for your dedication and work man, I appreciate it :grinning:

1 Like

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