# Converting direction to a orientation

Im using raycast to detect a surface direction of a part how would I use that direction and make it to a rotation for a other part any help is appreciated

1 Like

I think using the returned surface normal of the raycast could help you

Im not at home right now but correct me if im wrong but isnâ€™t surface normal a unit vector and if Iâ€™m right how would I convert it to a orientation which is not a unit vector I believe

Wait would the part your trying to rotate be on one of the surface directions?

Also have you found the surface direction yet?

1 Like

I maybe have misunderstood you but if im wrong correct me but you said surface direction and you can find the direction of a surface without finding the normal of that surface

Unless you already know the surface direction of course

1 Like

Yes the part would be on the surface and yeah finding the surface direction is the easy part

Why dont you just use the surface normal of where the ray hits and if it hits the part do stuff

I dont think you need to convert anything, unless im missunderstood you

1 Like

Oh are you sure Cause yesterday I did that and it tilted slightly where I wanted it to go
Unless I did something wrong

Maybe you did it right but you didnt rotate it enough?

Im no expert at math and havent really had any experience using surface normals of rays

1 Like

This would be for random amounts of surface direction(normals) using a ray(Mouse position)
I believe you do have to convert something but I donâ€™t know the math

1 Like

Im sorry but Im not that good at math; I tried to help you as much as I could

I know you can figure it out
Good luck!

1 Like

Thank you anyways! I find the solution sooner or later

1 Like

Does this work?

``````local normal = -- the normal vector
local currentUpVec = part.CFrame.UpVector
local angle = math.acos(currentUpVec:Dot(normal)
local axis = currentUpVec:Cross(normal)

part.CFrame = CFrame.fromAxisAngle(axis, angle)*part.CFrame``````
1 Like

I would have to try this once I get home thanks for the reply hopefully this works!

I donâ€™t understand some lines of your code you typed in(â€śCrossâ€ť,acos",â€śDotâ€ť,â€śFromAxisAngleâ€ť) can you send me a video on where I can fully learn this line of code or tell me what I should be researching or if you can explain it yourself I would appreciate it

i think i know what you mean. you just find the delta difference of them and set it.

make sure to set that cframe to the pivot if the object u rotating is rigged

1 Like

The cross product of two vectors gives a vector that is perpendicular to the two vectors. The dot product of two unit vectors is a number that tells how close the directions are to each other. The arccosine of the dot product gives the angle between the vectors, because the dot product is the cosine of that angle.

CFrame.fromAxisAngle takes an axis to rotate around, and an angle that tells how much to rotate around that axis.

I donâ€™t know how these things are calculated.

3 Likes

by perpendicular you mean a vector3 between two other vector3 right?
and Im guessing fromaxisangle basically applys its own orientation as a 0,0,0

I believe RoBoPoJu is offline if anyone can tell me how they learned cross product or dot product that will be cool cause Iâ€™m completely confused I been researching about trigonometry and quaternions for the past 2 days for all of my periods including lunch in school and as well at home canâ€™t seem to figure this out I am a very visual learner but words will do fine as well

I want to note I barely found out last week what .unit is and how it works

When lines are perpendicular, thereâ€™s a 90 degree angle between them. For example, the global y axis in roblox is perpendicular to the global x and z axes. Thereâ€™s a 90 degree angle between y and z axes and between y and x axes. Thereâ€™s also a 90 degree angle between x and z axes, so each of these axes is perpendicular to the other two.

There are always two possible cross products for two vectors. The directions of these two are opposite and which one you get depends on which order you put the vectors in the calculation.

Vectors have a length, but the global axes are infinite to both directions. However, the positive direction of the y axis could be described with vector (0, 1, 0) and the positive direction of x axis with vector (1, 0, 0). The cross product of these would be the positive direction of z axis, which is (0, 0, 1) or its negative direction, which is (0, 0, -1). The cross product isnâ€™t always a unit vector (vector with length 1), but you can get its unit vector by didving the vector with its length. In roblox, the unit vector of a vector is the value of its unit property.

1 Like