Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
2

Orient object given surface normal?

Asked by
Nymint 85
6 years ago

Hi! Suppose a ray has been casted towards a sphere or any other shape, this gives you useful information, one of them being the surface normal which is calculated from the intersection point of the ray. I intend to use the surface normal in order to orient an object onto a sphere or other shape's planes.

This wiki document demostrates this well, and it's the base method I have been relying on.

However, this method stops functioning properly once the oriented object reaches the bottom-half of a sphere. Workarounds were attempted with narrow success.

I managed to get around this by getting the angle between the up vector and the surface normal with the dot product.

local Cross = Vector3.new(0, 1, 0):Cross(SurfaceNormal)
local Angle = Vector3.new(0, 1, 0):Dot(SurfaceNormal)
Part.CFrame = CFrame.new(Mouse.Hit.p + SurfaceNormal * Part.Size.Y / 2) * CFrame.fromAxisAngle(Cross.Magnitude == 0 and Vector3.new(1, 0, 0) or Cross.Unit, Angle)

But then new problem arrives: The object's Y axis begins to abnormally spin whenever it's nearby the bottom-center of the sphere. I can't seem to figure this one out. Here's a demostration.

Any advice is appreciated.

0
Pretty sure that just has to do with the way roblox calculates CFrame. As sad as it is, the only real way I see to solve this without experimenting (or someone happening to know here), would be how I have bee doing it. Use the cross product on (X,Z) and (Y,Z) to set both rotations. You can also save an offset that would counteract changes in the Y rotation, but that is less effective if you plan t Bellyrium 310 — 6y
0
o let them rotate the part through input Bellyrium 310 — 6y
0
Thank you for your time and response, could you please be more specific? Would I have to use fromAxisAngle twice for the Cross product's X,0,Z and the other for 0,Y,Z in that order? Nymint 85 — 6y
0
yeah i think ur right PhoenixVortex_RBLX 33 — 6y
View all comments (2 more)
0
I had this question to nc2r 117 — 3y
0
i recommend asking this on the devforum, people over there are better at math than here lol brokenVectors 525 — 3y

Answer this question