Organic VR character torso tilt

Need help with some of the cframe math i was working on, i didnt test to make sure the player can turn around and noticed the issue however im unsure of a good way to fix it i understand the cause being my :Inverse() though

local Torso = workspace.torso
local Head = workspace.head
local RightArm = workspace.RightArm
local LeftArm = workspace.LeftArm

for i = 1,1 do
    wait()
    local RightOffset = (RightArm.Position - Torso.Position) -- RIGHT ARM offset from torso equivelent to :ToObjectSpace()
    local LeftOffset = (LeftArm.Position - Torso.Position)  -- LEFT ARM offset from torso equivelent to :ToObjectSpace()

    local pitch = (RightOffset.Z - LeftOffset.Z) / 3 -- calculate pitch
    local roll = (LeftOffset.Y - RightOffset.Y) / 3  -- calculate roll

    local TX,TY,TZ = Head.CFrame:ToEulerAnglesXYZ() -- get head rotations x,y,z order in euler
    
    Torso.CFrame = Head.CFrame -- Set Torso CFrame to head CFrame
        * CFrame.new(0,-0.5,0) -- Offset torso below the head
        * Head.CFrame.Rotation:Inverse() -- remove rotation influence from the head
        * CFrame.Angles(TX/10,0,0) -- Use head up down angel to influence a small lean
        * CFrame.new(0,-1.5,0) -- Offset torso downward by a fixed number
        * CFrame.Angles(0, math.rad(-pitch * 10), math.rad(-roll * 10)) -- Rotate torso based on hand positions forward backward up and down relitive to torso
end

i have tried separating TY into its own and inverting it it came pretty close but the torso still had a weird lean to it specifically -18 on the x. any help would be really appreciated honestly.

edit: updated the code a bit still having the same issue shown

1 Like

i solved the issue for anyone interested and who may find use for it the fixed code is this:

local Torso = workspace.torso
local Head = workspace.head
local RightArm = workspace.RightArm
local LeftArm = workspace.LeftArm
local Pi = math.pi

for i = 1,500 do
	wait()
	local TorsoCFrame = Torso.CFrame -- save torso CFrame
	local RightOffset = TorsoCFrame:Inverse() * RightArm.Position -- calculate Right arm torso relitive positions
	local LeftOffset = TorsoCFrame:Inverse() * LeftArm.Position -- calculate Left arm torso relitive positions

	local pitch = (RightOffset.Z - LeftOffset.Z) / 3 -- calculate pitch
	local roll = (LeftOffset.Y - RightOffset.Y) / 3  -- calculate roll
	
	local LookVector = Head.CFrame.LookVector -- Get current Head lookvector
	local TY = math.atan2(LookVector.X, LookVector.Z) -- Calculate Yaw from LookVector's X and Z component
	local TX = math.asin(-LookVector.Y) -- Calculate pitch from LookVector's Y component

	Torso.CFrame = Head.CFrame -- Set Torso CFrame to head CFrame
		* CFrame.new(0,-0.5,0) -- Offset torso below the head
		* Head.CFrame.Rotation:Inverse() -- remove rotation influence from the head
		* CFrame.Angles(-TX/10,TY+Pi,0) -- Use head up down angel to influence a small lean and re complinsate for the inverse head rotation
		* CFrame.new(0,-1.5,0) -- Offset torso downward by a fixed number
		* CFrame.Angles(0, math.rad(-pitch * 10), math.rad(-roll * 10)) -- Rotate torso based on hand positions forward backward up and down relitive to torso
end

commented to help break down each lines use im unsure of how good it is on performance (i would imagine its not the best) however it does work fully as i had originally intended with a few oddities

2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.