So, in my recreation i’ve figures, who’ve animation unbiased props- that i wish to have dangling down. I m utilizing the spring engine, so there may be solely skeleton animation and no inbuilt unit-pieces-physics engine for that. So i cobbled this along with Chat GPT, which is satisfied that that is glorious work.
Im satisfied it appears like an individual utilizing a purchasing baggage, tablets, pizzaboxes and work baggage for nun chuks. The traditional martial arts of purchasing foo! I noticed that first! WhoeYa!
perform getParentPieceMap(unitID)
native pieceMap = Spring.GetUnitPieceMap ( unitID )
parentPieceMap = {}
for okay,v in pairs(pieceMap) do
information = Spring.GetUnitPieceInfo(unitID, v)
if kind(information.guardian) == "quantity" then
parentPieceMap[v] = information.guardian
finish
finish
return parentPieceMap
finish
perform normalize(x,y,z)
native len = math.sqrt(x*x + y*y + z*z)
return x/len, y/len, z/len
finish
perform getRandomDirection()
return {math.random(0,1)*randSign(), randSign(), math.random(0,1)*randSign()}
finish
perform getUp()
return {0, 1, 0}
finish
perform getDown()
return {0, -1, 0}
finish
native perform mulMatMat4(a, b)
native m = {}
for i = 1,4 do
m[i] = {}
for j = 1,4 do
m[i][j] = a[i][1]*b[1][j] + a[i][2]*b[2][j] + a[i][3]*b[3][j] + a[i][4]*b[4][j]
finish
finish
return m
finish
native perform getPieceWorldMatrix(unitID, piece, parentPieceMap)
native mat = {
{1,0,0,0},
{0,1,0,0},
{0,0,1,0},
{0,0,0,1},
}
native chain = {}
native p = piece
whereas p do
desk.insert(chain, 1, p) -- prepend parent-first
p = parentPieceMap[p]
finish
for _,laptop in ipairs(chain) do
native m = {Spring.GetUnitPieceMatrix(unitID, laptop)} -- native matrix (4x4 flat)
-- develop into 4x4
native localMat = {
{m[1], m[5], m[9], m[13]},
{m[2], m[6], m[10], m[14]},
{m[3], m[7], m[11], m[15]},
{m[4], m[8], m[12], m[16]},
}
mat = mulMatMat4(mat, localMat) -- accumulate correctly
finish
return mat
finish
perform turnPieceTowards(unitID, vecTowards, parentPieceMap, pieceNr, velocity)
native itteration = itterations or 1
native perform cross(ax,ay,az, bx,by,bz)
return ay*bz - az*by,
az*bx - ax*bz,
ax*by - ay*bx
finish
native perform transpose3x3(m) -- works as inverse for pure rotations
return {
{m[1][1], m[2][1], m[3][1]},
{m[1][2], m[2][2], m[3][2]},
{m[1][3], m[2][3], m[3][3]},
}
finish
-- Multiply a vector by a 3x3 matrix
native perform mulMatVec(m, x,y,z)
return
m[1][1]*x + m[1][2]*y + m[1][3]*z,
m[2][1]*x + m[2][2]*y + m[2][3]*z,
m[3][1]*x + m[3][2]*y + m[3][3]*z
finish
native perform mulMatMat(a, b)
native m = {{0,0,0},{0,0,0},{0,0,0}}
for i=1,3 do
for j=1,3 do
m[i][j] = a[i][1]*b[1][j] + a[i][2]*b[2][j] + a[i][3]*b[3][j]
finish
finish
return m
finish
native worldMat = getPieceWorldMatrix(unitID, pieceNr, parentPieceMap)
-- extract rotation solely (higher 3x3)
native rot = {
{worldMat[1][1], worldMat[1][2], worldMat[1][3]},
{worldMat[2][1], worldMat[2][2], worldMat[2][3]},
{worldMat[3][1], worldMat[3][2], worldMat[3][3]},
}
native perform getOriginPieceRotation()
native ox, oy, oz = Spring.GetUnitPieceDirection(unitID, pieceNr)
native len = math.sqrt(ox*ox + oy*oy + oz*oz)
ox, oy, oz = ox/len, oy/len, oz/len
native originYaw = math.atan2(ox, oz)
native originPitch = -math.asin(oy)
return originYaw, originPitch
finish
-- invert rotation (transpose)
native inv = transpose3x3(rot)
-- remodel world down into piece native area
native lx,ly,lz = mulMatVec(inv, 0,-1,0)
-- convert to Euler
native yaw = math.atan2(lx, lz)
native pitch = -math.asin(ly)
Spring.Echo("turnPieceTowards yaw="..math.deg(yaw).." pitch="..math.deg(pitch).." native="..lx..","..ly..","..lz)
if iterations and iterations > 1 then
native issue = 1.0
native dir = 1
native originYaw, originPitch = getOriginPieceRotation()
whereas issue > 0.1 do
native swingYaw = yaw + dir * issue * originYaw
native swingPitch = pitch + dir * issue * originPitch
WTurn(pieceNr, x_axis, swingYaw, velocity or 0)
WTurn(pieceNr, y_axis, swingPitch, velocity or 0)
Sleep(33) -- ~1 body (30Hz), tune as wanted
dir = -dir -- flip course
issue = issue * 0.95
finish
-- settle at middle
finish
Flip(pieceNr, x_axis, yaw, velocity or 0)
Flip(pieceNr, y_axis, pitch, velocity or 0)
finish
I’ve a take a look at animation loop, operating the arms of the civilians in circles alongside every axis, and the items don’t level down – or for that matter in any course constant.
The place did all of it go sideways?