What is inverse kinematics? It is that cool arm robot from iron man.
In computer animation and robotics, inverse kinematics is the mathematical process of calculating the variable joint parameters needed to place the end of a kinematic chain, such as a robot manipulator or animation character’s skeleton, in a given position and orientation relative to the start of the chain. (straight off wikipedia)
process of obtaining joint angles from known coordinates of end effector.
(OPEN SOURCE)
This module uses FABRIK (Forward and Backward Reaching Inverse Kinematics) You can enable 2D or 3D.
In the future I will add constraints to it. Meaning certain points can turn only at a certain angle.
I might add more stuff to the module in the future.
Kinematics.lua (5.2 KB)
ExamplePlace.rbxl (56.4 KB)
Preview 2D
Preview 3D
Code Documentation (its short so ill put it here)
Create IK 3D
local Kinematics = require(game:GetService("ServerScriptService").Kinematics).new{
Type = "2D", -- Create a 2D type. it only accepts "2D" or "3D"
Arm = {7, 5, 3} -- Create our arm of lengths. Add as much arms you want
-- First arm is 7 Studs, second arm is 5 studs, third are is 3 studs
}
while task.wait() do
local originPosition = workspace.Origin.Position -- get start position in workspace
local targetPosition = workspace.End.Position -- get target position in workspace
local success = Kinematics:FABRIK{ -- use FABRIK (Forward and Backward Reaching Inverse Kinematics)
Origin = originPosition, -- give it start position
Target = targetPosition, -- give it target position
Tolerance = 0.1, -- Optional doesnt need to be here unless you want manual settings
MaxIterations = 100 -- Optional doesnt need to be here unless you want manual settings
}
local cframes = Kinematics:GetPartCFrames(originPosition, targetPosition) -- get the cframes so we can display and show
for armIndex = 1, #cframes do -- loop through them numerical loop cuz fast and cool
local cframe = cframes[armIndex] -- get the cframe
local arm = workspace[`Arm{armIndex}`] -- get the part in workspace that we are gonna apply cframe to
arm.CFrame = cframe -- set the part cframe and done.
end -- do other stuff
end
All the stuff you can do
local Kinematics = require(game:GetService("ServerScriptService").Kinematics)
-- Create new Kinematic Solver
local solver = Kinematics.new{
Type = "3D", -- what type? "2D" or "3D"
Arm = { 3, 3 } -- lengths of arms, can have as much arms as you want and as much lengths
}
local success = solver:FABRIK{ -- use FABRIK, forwa and bawa reac ivner kinema
Origin = Vector3.new(2, 5, 6), -- start position
Target = Vector3.new(2, 2, 2), -- end position
Tolerance = 0.2, -- not needed since its automatically set but tolerance meaning how close it needs to be for to be success
MaxIterations = 25, -- max amount it'll do forwrad and back if not reached tolerance
} -- success means if it reached the target position within the tolerance and maxInterations
local armsData = solver:GetArmsData() -- gets all the data of each arm, gets points and length
-- returns { { position: Vector3, length: number, constraint: not used } }
local positions = solver:GetPositions() -- gets position of all points
-- it returns and array of positions so { Vector3, Vector3, Vector3 }
local cframes = solver:GetPartCFrames() -- gets cframes of all points similar to points but it gets the cframe directly and positions them in the middle
-- it returns and array of cframes so { CFrame, CFrame, CFrame }
Check out my machine learning (VERY COOL AI) library