Here’s what I’ve come up with this morning, just a little test to see what’s possible but I need to improve my physics and I’d also like to see what others come up with for realistic movement and such.
function setup()
--Movement point (the head in this case)
mp = vec2(WIDTH/2,HEIGHT/2)
--Movement point delta for additional velocity, not used at the moment
mpdt = vec2()
--Vector table for body points
vtbl = {}
vtbl[1] = vec2(0,0)--head
vtbl[2] = vec2(0,-30)--pelvis
vtbl[3] = vec2(5,-55)--right knee
vtbl[4] = vec2(-5,-55)--left knee
vtbl[5] = vec2(5,-75)--right foot
vtbl[6] = vec2(-5,-75)--left foot
vtbl[7] = vec2(0,-5)--shoulder joint
vtbl[8] = vec2(5,-20)--right elbow
vtbl[9] = vec2(-5,-20)--left elbow
vtbl[10] = vec2(5,-35)--right hand
vtbl[11] = vec2(-5,-35)--left hand
--Velocity table (if used)
vel = {}
--Point table for final positions of joints (table of points that gets drawn)
p = {}
--For all the joints create the table of velocity for each joint and the point of each joint
for i = 1,#vtbl do
vel[i] = vec2(0,0)
p[i] = mp+vtbl[i]
end
end
function touched(t)
if t.state == BEGAN or t.state == MOVING then
--Set body position
mp = vec2(t.x,t.y)
--Set velocity of joints
--mpdt = vec2(t.deltaX,t.deltaY)/5
end
for i=1,#vtbl do
if i > 1 then
vel[i] = vel[i] + mpdt
end
end
end
function draw()
background(0)
--Set the gravity variable
local grav = vec2(0,-0.1)
--Localise table vtbl, call it v for the drawing
local v = vtbl
for i=1,#vtbl do
vel[i] = vel[i] + grav*3
vel[i] = vel[i] * 0.9
end
--Create and update all joint positions and create *bones*
vtbl[1] = mp
vtbl[2] = vtbl[1]+(vtbl[2]-vtbl[1]):normalize()*30 + vel[2]
vtbl[3] = vtbl[2]+(vtbl[3]-vtbl[2]):normalize()*25 + vel[3]
vtbl[4] = vtbl[2]+(vtbl[4]-vtbl[2]):normalize()*25 + vel[4]
vtbl[5] = vtbl[3]+(vtbl[5]-vtbl[3]):normalize()*20 + vel[5]
vtbl[6] = vtbl[4]+(vtbl[6]-vtbl[4]):normalize()*20 + vel[6]
vtbl[7] = vtbl[1]+(vtbl[2]-vtbl[1]):normalize()*5
vtbl[8] = vtbl[7]+(vtbl[8]-vtbl[7]):normalize()*20 + vel[8]
vtbl[9] = vtbl[7]+(vtbl[9]-vtbl[7]):normalize()*20 + vel[9]
vtbl[10] = vtbl[8]+(vtbl[10]-vtbl[8]):normalize()*15 + vel[10]
vtbl[11] = vtbl[9]+(vtbl[11]-vtbl[9]):normalize()*15 + vel[11]
--Create spacing between elbows,hands and knees
if vtbl[3]:dist(vtbl[4]) < 10 then
vtbl[3] = vtbl[3]+(vtbl[3]-vtbl[4]):normalize()*0.5
vtbl[4] = vtbl[4]+(vtbl[4]-vtbl[3]):normalize()*0.5
end
if vtbl[9]:dist(vtbl[8]) < 20 then
vtbl[8] = vtbl[8]+(vtbl[8]-vtbl[9]):normalize()*0.5
vtbl[9] = vtbl[9]+(vtbl[9]-vtbl[8]):normalize()*0.5
end
if vtbl[10]:dist(vtbl[11]) < 30 then
vtbl[10] = vtbl[10]+(vtbl[10]-vtbl[11]):normalize()*0.5
vtbl[11] = vtbl[11]+(vtbl[11]-vtbl[10]):normalize()*0.5
end
--Draw the body
strokeWidth(5)
stroke(200,200,200,255)
line(v[1].x,v[1].y,v[2].x,v[2].y)
line(v[2].x,v[2].y,v[3].x,v[3].y)
line(v[2].x,v[2].y,v[4].x,v[4].y)
line(v[3].x,v[3].y,v[5].x,v[5].y)
line(v[4].x,v[4].y,v[6].x,v[6].y)
line(v[7].x,v[7].y,v[8].x,v[8].y)
line(v[7].x,v[7].y,v[9].x,v[9].y)
line(v[8].x,v[8].y,v[10].x,v[10].y)
line(v[9].x,v[9].y,v[11].x,v[11].y)
ellipse(v[1].x,v[1].y,20)
end
If you have any input on this then please share!