Hinge constraint servo facing mouse

Hello.

I’ve been working on this T72B3 model recently, and have been having a lot of issues working out the turret.

Here’s my code for reference:

--MODULE:
local m={}
m.TurnTargetAngle = function(target: Vector3,origin: Vector3)
    local delta = target - origin
    local new_delta = Vector3.new(delta.x, 0, delta.z)
    local unit_delta = new_delta.unit
    local angle = math.atan2(unit_delta.z, unit_delta.x)
    return (-math.deg(angle)+math.pi)
end
return m;
--SCRIPT:
coroutine.resume(coroutine.create(function()
    RunService.Heartbeat:Connect(function()
        if currentdriver ~= nil then
            local MP = MousePosition:InvokeClient(game.Players[tostring(currentdriver)])
            local t = TargetModule.TurnTargetAngle(MP,Parts.TurretRing1.Position)
            Parts.TurretRing2.Rotator.TargetAngle = t
        end
    end)
end))

And here’s what’s happening:


Does anybody know what’s wrong?

I would love to help you but cannot see what the problem is from the images shown.
I believe the turret simply turns round eg has no tilt so images showing the view from above would show more about the rotation issue.
Can you provide one for me?

Let me sum it up as well as I can.

When I point the mouse towards a specific direction, the turret ring decides to turn to the side.

Here’s the best visualization I can make:

image

In terms of rotation, the bottom face points towards the “target”, and the front face points towards the sky.

(To clarify, this is using a hinge constraints target angle)

From what I can find it seems to be due to the fact that math.atan2 is relative to the positive world X axis. I’m no vector math genius so my solution is to mess around with negatives and subtracting rotation and stuff

Edit: I solved it by subtracting the WorldOrientation.Y of the Attachment0 of the turret base servo

Can you maybe explain it in a little more detail?

When I try to subtract the angle by (A0.Y-TargetAngle) it comes out to some absurd number.
image

When I try subtracting (A0.Y-TargetAngle) by the angle I get a weird assortment of random numbers.
image

I’m trying a few other methods and editing this as I go, but so far nothing has worked.

AS OF NOW THIS IS WHAT IT LOOKS LIKE:
image

SCRIPT:

    RunService.Heartbeat:Connect(function()
        if currentdriver ~= nil then
            local MP = MousePosition:InvokeClient(game.Players[tostring(currentdriver)])
            local t = TargetModule.TurnTargetAngle(MP,Parts.TurretRing2.Armor.WorldPosition,(Parts.TurretRing2.Rotator.Attachment0.WorldOrientation.Y))
            Parts.TurretRing2.Rotator.TargetAngle = t
        end
    end)

MODULE:

local m={}
m.TurnTargetAngle = function(target: Vector3,origin: Vector3, sa)
    local delta = target - origin
    local new_delta = Vector3.new(delta.x, 0, delta.z)
    local unit_delta = new_delta.unit
    local angle = math.atan2(unit_delta.z, unit_delta.x)
    print(sa-(-math.deg(angle)+math.pi))
    return sa-(-math.deg(angle)+math.pi)
end
return m;

You have to do it the other way around, such that sa is subtracting from the final angle.

Here is the code that has worked for me:

(-math.deg(AngleYaw)) - TurretAttach.WorldOrientation.Y

where AngleYaw is angle and TurretAttach.WorldOrientation.Y is sa

1 Like