Hi! I’m having a problem with making some fairly confusing CFraming work for every case, I was wondering if I anyone could help. Basically, I’m trying to rotate a rectangular prism towards a target point BY rotating the cylinder its welded to like so:
The pink triangle represents the angle of rotation between the two points. The purple rectangle represents desired rotation, and the arrows are the rotation of the cylinder.
I’ve gotten this to work in a specific case (rotated 90 degrees from the origin), however I’m having trouble fitting it for every case. When starting from a different initial rotation, the CFraming is thrown out of wack and does not function properly. This is the relevant code for the case that works:
local function getAngle(u, v)
local a = (u.Position * Vector3.new(1,0,1)).Unit
local b = (v.Position * Vector3.new(1,0,1)).Unit
local sign = math.sign(a:Cross(b).Y)
local angle = (math.acos(a:Dot(b)/(a.Magnitude*b.Magnitude))) * sign
return angle
end
local point = target:WaitForChild("Attachment")
local cylinderBase = game.Workspace:WaitForChild("cylinder")
local t_CF = (point.WorldCFrame):ToObjectSpace(cylinderBase.CFrame)
local a1 = getAngle(cylinderBase.CFrame, t_CF) -- Angle
local targetCF = cylinderBase.CFrame * CFrame.Angles(0, a1 , 0)
If you need any more info about the issue, just ask. Any help would be appreciated, thanks!
Instead of trying to calculate the angle you could utilize the second parameter of CFrame.new() which is a position to point its front surface in. Also I’m not really sure how you want it to get rotated, do you want it to rotate like a ball socket constraint around the hub of the cylinder, or do you want it like a hinge where it’s rotating on one axis? (basically like a lever)
This would make it act like a ball socket constraint.
Then since I’m assuming that’s not the cylinder’s front surface you’re trying to rotate you can just rotate the CFrame (it should be a multiple of 90 on the Y axis) above to get it to face the right way.
Thanks for your response! I tried the method you suggested, however it does not work for reasons I don’t entirely understand. My guess though would be that its axis of rotation the lookVector utilizes is distorted by attempting to shift by 90 degrees in the Y direction. To answer your question, I’d like it to work more like a hinge constraint, rotating on one axis to achieve the desired position.
It should make the front face of the cylinder face the thingy you wanted. If you just want to rotate it on the Y axis, you can convert it into angles and only change the Y Orientation of the orientation Vector
What does it look like? And also you can solve that by locking the rotation to one axis.
The weird thing about you wanting it to be a hinge though makes it so that it would only look like it’s pointing straight at the target if it was either in front or behind it. What about something to the left or right of it? Does it still need to be axis-locked?
This actually did work. I had a careless variable mix-up that caused the error. Thanks so much for your quick and accurate response! Much simpler solution than I was suspecting. I was overcomplicating the issue with my approach.
Practice and reading solutions of people more knowledgeable than you are. If you’re looking for some great guides on CFrame math and such I would recommend checking out AxisAngle, EgoMoose, and EchoReaper’s topics and posts.