Hello. I am working on creating an hub interface for a game and one of the core features is the ability to interact with a globe using buttons/attachments that are placed on the globe. The idea is that once you click the point on the globe, the globe will pivot in such a way that the attachment/point is oriented in the center of the camera.
Unfortunately I have been unable to come up with the proper math to accomplish this. I got very close, and the angles I came up with are correct. However, I neglected to consider how orientation actually works on ROBLOX and simply coming up with two angles and setting the CFrame Orientation using them results in a slightly offset Orientation.
I attempted to uncover multiple solutions, all of which provided similar results or something drastically different. Some of my attempts came from:
Here is the code I am currently using. All I really did was triangulate the three positions and solve for the necessary angle on both the Y and Z axis. The issue is that I did not consider the change to the X axis that would occur by modifying both the Y and Z, and so the resulting CFrame is offset from what I’d like it to be.
local cPos = workspace.CurrentCamera.CFrame.Position -- camera
local aPos = attachment.WorldPosition -- location of attachment on globe
local pPos = pivot.Position -- location of center of globe
local pc = pivot.CFrame:ToObjectSpace(workspace.CurrentCamera.CFrame)
local pa = pivot.CFrame:ToObjectSpace(attachment.WorldCFrame)
local ca = CFrame.new(Vector3.new(math.abs(cPos.X - aPos.X), (cPos.Y - aPos.Y), (cPos.Z - aPos.Z)))
---
local paHY = math.sqrt(pa.Z^2 + pa.X^2)
local paHZ = math.sqrt(pa.X^2 + pa.Y^2)
local caHY = math.sqrt(ca.Z^2 + ca.X^2)
local caHZ = math.sqrt(ca.X^2 + ca.Y^2)
local Z = math.acos( ((paHZ ^ 2) + (pc.X ^ 2) - (caHZ ^ 2)) / (2 * (paHZ * math.abs(pc.X))) )
local Y = math.acos( ((paHY ^ 2) + (pc.X ^ 2) - (caHY ^ 2)) / (2 * (paHY * math.abs(pc.X))) )
print(math.deg(Z)) -- 39.8
print(math.deg(Y)) -- 45
game:GetService('TweenService'):Create(pivot, TweenInfo.new(1), {CFrame = pivot.CFrame * CFrame.Angles(0, -Y, Z)}):Play()
The code above provides me with the angles I thought I could apply to orient the pivot to the camera, using the position of the attachment. By themselves, these angles work. (ie: (0, -Y, 0) will properly align the attachment on the Y axis, and same with Z), but when applied simultaneously result in the off-center orientation seen above.
Any insight is appreciated!