I've been trying to figure out how to measure angles, specifically changes in angles, of bones. What I'd like to do is then set the angle of other bones to a manipulation of that value. The goal is to simulate a sort of double-hinged joint like the bones in a forearm.
What I came up with:
(State is initiated after its done being built)
- Code: Select all
HingeState = State{
Main = function(self)
WaitSeconds(2) #unpacking of the unit, you can ignore this
while not self:IsDead() do
WaitSeconds(0.5) #necessary to stop endless update and game crash
#Set up joints 1 and 2
self.J1 = CreateRotator(self, 'joint1', 'x') #the two bones ill be manipulating later
self.J2 = CreateRotator(self, 'joint2', 'x')
self.Trash:Add(self.J1) #these things that are just required for whatever reason?
self.Trash:Add(self.J2)
#Get angle position
local angle = {}
angle.x, angle.y, angle.z = self:GetBoneDirection('pitch')
#Im hoping this is a local x angle measured from starting orientation
LOG('pitch angle is') #so i know what is happening
LOG(angle.x)
#Translate differences in angle
#local R1 = {}
local R2 = {}
local R3 = {}
local R4 = {}
R1 = -0.0064*angle.x^2 #R1 to R4 come from the approximated hinge behavior for my specific set of bones
R2 = 0.9653*angle.x
R3 = 0.0089*angle.x^2
R4 = -1.5412*angle.x
LOG('J1 rotating by')
LOG(R1+R2)
LOG('J2 rotating by')
LOG(R4+R3)
self.J1:SetCurrentAngle(R1+R2) #moves joint1 and joint2 bones to their new positions
self.J2:SetCurrentAngle(R3+R4)
end
end
},
So that's what I have, but what ends up happening is J1 and J2 don't stop moving, they just keep bouncing to a new position.
Picture of set up for visualizing:
Basically the code should rotate joint1 and joint2 as to keep the model representation "attached" to the small green cylinder at the top and the sphere at the bottom.
Any ideas?
Thanks