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

How can I prevent gimbal locking on this?

Asked by 6 years ago

I have had a difficult time understanding the concepts that have been presented while searching for the answer to this, such as using quaternions. I have no idea how to apply them. Any help would be highly appreciated.

This is ripped in its entirety from the Roblox wiki article "Springs", just edited to be used with rotation.

local rs = game:GetService("RunService")
local cc = workspace.CurrentCamera

local spring = {}

function spring.new(current, velocity, target)
    local self = setmetatable({}, {__index = spring})

    self.current = current
    self.velocity = velocity
    self.target = target
    self.k = 1
    self.d = 1

    return self
end

function spring:update()
    local x = self.target - self.current
    local f = x * self.k
    self.velocity = (self.velocity * (1 - self.d)) + f
    self.current = self.current + self.velocity
end

local part0 = Instance.new("Part", workspace)
part0.Anchored = true
part0.CanCollide = false

local part1 = Instance.new("Part", workspace)
part1.Anchored = true
part1.CanCollide = false

local new = spring.new(part0.Orientation, Vector3.new(), part1.Orientation)
new.k = 0.5
new.d = 0.5

rs.RenderStepped:connect(function()
    new.target = part0.Orientation
    new:update()
    part0.CFrame = cc.CFrame
    part1.CFrame = part0.CFrame * CFrame.new(-2, -2, -5)
    part1.Orientation = new.current
end)
1
gimbal locking? Isn't that just restraining an axis for rotation? I feel like I can help, you just need to be a bit more specific. Do you want it to travel freely in all directions? Bellyrium 310 — 6y
0
I'm not even sure if gimbal locking is the correct term, I'm not well informed on this sort of stuff. Basically whenever any of the axes reach 180/-180 it flips. Free rotation in all directions is pretty much what I'm looking for, yes. reallybadweather 0 — 6y
0
OHH, Ok, give me a minute. Bellyrium 310 — 6y
0
Sure. reallybadweather 0 — 6y
0
if you need, Ill do it when im off work today. Bellyrium 310 — 6y

1 answer

Log in to vote
0
Answered by
Bellyrium 310 Moderation Voter
6 years ago

Ok, I don't have the time to finish the script. Basically what I was going to do was make it measure the distance between the rotations. If its over 180deg, it reverses the angle.

(the problem being that roblox doesn't naturally loop from 180 over to -180, you have to do that math yourself. So if the last angle is 180, and the next angle is supposed to be -170 it will go backwards. You want to tell it to keep going those remaining degree instead of backtracking.

Radians since they work in relation to a circle itself would be my preferred rout if I had time to do the math. Especially since they auto fill if you add or subtract with them.

You could also use axis rotations I think, it would require more math though i think..

0
I don't know how I'd apply this to what I have. If you put the code in my original post into a localscript in startercharacterscripts and move the camera around you'll see what I'm talking about. reallybadweather 0 — 6y
Ad

Answer this question