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

How do you limit the angle you can rotate on mouse delta joint c0 rotation with a spring?

Asked by
YTGonzo 20
4 years ago

I'm trying to put a spring on my character body but It keeps rotating farther than I wanted.

How would I put a limit on the rotation?

--This is the Spring Module
-- Constants

local ITERATIONS    = 8

-- Module

local SPRING    = {}

-- Functions

function SPRING.create(self, mass, force, damping, speed)
    local spring    = {
        Target      = Vector3.new();
        Position    = Vector3.new();
        Velocity    = Vector3.new();

        Mass        = mass or 5;
        Force       = force or 50;
        Damping     = damping or 4;
        Speed       = speed  or 4;
    }

    function spring.shove(self, force)
        local x, y, z   = force.X, force.Y, force.Z
        if x ~= x or x == math.huge or x == -math.huge then
            x   = 0
        end
        if y ~= y or y == math.huge or y == -math.huge then
            y   = 0
        end
        if z ~= z or z == math.huge or z == -math.huge then
            z   = 0
        end
        self.Velocity   = self.Velocity + Vector3.new(x, y, z)
    end

    function spring.update(self, dt)
        local scaledDeltaTime = math.min(dt,1) * self.Speed / ITERATIONS

        for i = 1, ITERATIONS do
            local force         = self.Target - self.Position
            local acceleration  = (force * self.Force) / self.Mass

            acceleration        = acceleration - self.Velocity * self.Damping

            self.Velocity   = self.Velocity + acceleration * scaledDeltaTime
            self.Position   = self.Position + self.Velocity * scaledDeltaTime
        end

        return self.Position
    end

    return spring
end

-- Return

return SPRING
-- This is a localscript inside the character scripts

local Player = game.Players.LocalPlayer
local Character = Player.Character
local spring = require(game.ReplicatedStorage.spring)
local mySpring = spring.create()
local Mouse = Player:GetMouse()
local TurnLimit = 20
local random = Random.new()


game:GetService("RunService").RenderStepped:Connect(function(deltaTime)
    local mouseDelta = game:GetService("UserInputService"):GetMouseDelta()

    print(Character.UpperTorso.Waist.C0.ToAxisAngle(Character.UpperTorso.Waist.C0))

    mySpring:shove(Vector3.new(mouseDelta.x / 500,mouseDelta.y / 200, 0))

    local movement = mySpring:update(deltaTime)

    Character.UpperTorso.Waist.C0 = Character.UpperTorso.Waist.C0 * CFrame.Angles(0,-movement.x,0)
end)

Answer this question