I’m using a Motor6D for a following spotlight, but whenever I go around the 360 point, the motor goes all the way back around before coming back to me instead of just taking the shorter path.
This is better depicted in the video attached:
If anyone knows how to work-around/fix this, it would be much appreciated. My code for the FollowSpot lookAt is below as well.
function FollowSpot:lookAt(TargetPosition: Vector3)
assert(typeof(TargetPosition) == "Vector3", "Argument #1 of FollowSpot:lookAt() must be a valid Vector3")
local CurrentPosition = self.CenterAttachment.WorldPosition
local DownLength = CurrentPosition.Y - TargetPosition.Y
local AcrossLength = TargetPosition.Z - CurrentPosition.Z
local AcrossLength2 = TargetPosition.X - CurrentPosition.X
local HypLength = math.sqrt((AcrossLength2 * AcrossLength2) + (AcrossLength * AcrossLength))
local Theta = math.atan2(HypLength, DownLength)
local Theta2 = math.atan2(AcrossLength2, AcrossLength)
self.YMotor.DesiredAngle = Theta
self.XMotor.DesiredAngle = Theta2
self.CurrentFocus = TargetPosition
end
None of these work. I realized the PutInRange function is not even necessary; I removed it and just set desired angle to arctan(HypLength, DownLength) and nothing changed.
Why is your DOMAIN_STEP not 2 * math.pi? That’s the period of the cosine and sine function.
Can you tinker with values in real-time (play mode) and see exactly which two angles (start & end) cause the motor to take the longer path?
Because the rotation doesn’t matter. As I said, the range is ultimately useless. The issue is that the math AUTOMATICALLY only returns between -π/2 and π/2 because that’s its main period, so when that range is exited, the math just restarts rather than going forward. How would I fix this?
What math are you referring to? Are you talking about math.atan2 or something internal to the motor? You may need to carefully consider what you’re assigning on these lines. Can you print the thetas each time they’re calculated? It might help you see what values are going into the motor when it starts acting up.
Does this behavior occur if you apply a period shift (+ or - pi) to the input and output? Consider changing theta and theta2 such that they are between 0 and 2*pi. You may not get the result you’re looking for right away, but you should first ensure this prevents the motor from taking a longer path.