Ragdoll-elastic man!

Miscellaneous Forums/Blitz Showcase/Ragdoll-elastic man!

Thx!

it all started about I year ago when I got interested in simulating "balls" : the first program I did was "particles", in allegro, with which I learned how to make collisions between balls (and also put gravity!)

http://radicalrebound.com/particles.zip

THen 6 months after I wondered how one could link the balls to form a chain: in a week I managed to find a good algorithm:


lines=0

vecchiotempo=0
cycles=0
r=8

raggiofalso=r

numballs=50
iteraz=300
g#=0.6


Global angle#[numballs+1]
Global rv# [numballs+1]
Global x# [numballs+1]
Global y# [numballs+1]
Global z# [numballs+1]
Global xv#[numballs+1]
Global yv#[numballs+1]
Global oldx# [numballs+1]
Global oldy#[numballs+1]
Global yspeed#[numballs+1]
Global xspeed#[numballs+1]
Global unicol[3+1]
Global col[numballs+1,3+1]
Global adder[3+1]


Global forzax1#[numballs+1]
Global forzay1#[numballs+1]
Global forzax0#[numballs+1]
Global forzay0#[numballs+1]

Global dist#[numballs+1]

Global m#[numballs+1]



For i=1 To 3
adder(i)=1
Next

For i=1To numballs

x(i)=i


For j=1 To 3
col(i,j)=Rnd(255)

Next
Next

grabbed=0
grabx#=0
graby#=0

x(1)=400
y(1)=300



xwin=640'1024
ywin=480'768


xwin=1024
ywin=768

Graphics xwin, ywin,0,70





Repeat

Cls

For i=1 To numballs
oldx(i)=x(i)
oldy(i)=y(i)
Next 




If  MouseDown(1)<>1

grabbed=0
End If

If (grabbed=0) & (MouseDown(1)=1)

For i=1 To numballs

If ((MouseX()>x(i)) & (MouseX()<x(i)+2*raggiofalso)  & (MouseY()>y(i)) & (MouseY()<y(i)+2*raggiofalso)) Then
grabbed=i

grabx=MouseX()-x(i)
graby=MouseY()-y(i)


End If

Next

End If

grabbed=1


For i=1To numballs

m(i)=1
Next

'm(numballs)=10

'm(1)=10



If (grabbed>0)

m(grabbed)=1000000000


x(grabbed)=MouseX()-grabx
y(grabbed)=-graby+MouseY()
xv(grabbed)=0
yv(grabbed)=0
End If





'applica le forze alla velocita' vecchia



For i=1 To numballs


yv(i)=yv(i)+g

x(i)=x(i)+xv(i)
y(i)=y(i)+yv(i)





Next



For l=1 To iteraz'!!!!!

For i=2 To numballs



dist(i)=Sqr((x(i)-x(i-1))*(x(i)-x(i-1))+(y(i)-y(i-1))*(y(i)-y(i-1)))-2*r

If (1=2)
angle(i)=ATan2(-y(i)+y(i-1),x(i)-x(i-1))
forzax1(i-1)=dist(i)/(1+m(i-1)/m(i))*Cos(angle(i))
forzay1(i-1)=-dist(i)/(1+m(i-1)/m(i))*Sin(angle(i))


forzax0(i)=-dist(i)/(1+m(i)/m(i-1))*Cos(angle(i))
forzay0(i)=dist(i)/(1+m(i)/m(i-1))*Sin(angle(i))
End If

If (1=1)
If ( dist(i)+2*r )<>0

forzax1(i-1)=dist(i)/(1+m(i-1)/m(i))* ( (x(i)-x(i-1) )/ ( dist(i)+2*r ) )

forzay1(i-1)=dist(i)/(1+m(i-1)/m(i))*( (y(i)-y(i-1) )/ ( dist(i)+2*r ) )


forzax0(i)=-dist(i)/(1+m(i)/m(i-1))*( (x(i)-x(i-1) )/ ( dist(i)+2*r ) )
forzay0(i)=-dist(i)/(1+m(i)/m(i-1))*( (y(i)-y(i-1) )/ ( dist(i)+2*r ) )
End If
End If

Next


For i=1 To numballs

x(i)=x(i)+forzax0(i)+forzax1(i)
y(i)=y(i)+forzay0(i)+forzay1(i)

Next

Next



For i=1 To numballs




xv(i)=x(i)-oldx(i)
yv(i)=y(i)-oldy(i)

If (x(i)>xwin -2*r)
x(i)=xwin-2*r
xv(i)=-xv(i)
End If
If (x(i)<0) 
x(i)=0
xv(i)=-xv(i)
End If
If (y(i)>ywin-2*r) 
y(i)=ywin-2*r
yv(i)=-yv(i)
xv(i)=0.96*xv(i)
End If
If (y(i)<0) 
y(i)=0
yv(i)=-yv(i)
End If


Next





For i=1 To 3
If (Rnd(0,1000)>990)
adder(i)=-adder(i)
End If

unicol(i)=unicol(i)+adder(i)
If (unicol(i)>254) Then adder(i)=-1
If (unicol(i)<1) Then adder(i)=1

Next

If (lines=0)
For i=1 To numballs

SetColor unicol(1),unicol(2),unicol(3)

'Color col(i,1),col(i,2),col(i,3)

DrawOval x(i),y(i),2*raggiofalso,2*raggiofalso
Next

End If

If (lines=1)
For i=1 To numballs-1

SetColor unicol(1),unicol(2),unicol(3)

'Color col(i,1),col(i,2),col(i,3)

DrawLine x(i),y(i),x(i+1),y(i+1)
Next
End If

	DrawText fps+"   "+grabbed+"   "+MouseDown(1), 20, 40


FlushMem

'If cycles=Floor(cycles/10)*10

	Flip
'	End if
	
cycles=cycles+1
If (MilliSecs()-vecchiotempo>1000)
vecchiotempo=MilliSecs()
fps=cycles
cycles=0
End If
	
		
Until KeyDown (KEY_ESCAPE)
	
End


That I discovered some people here already knew.

Last step is to put tensions between angles, somehow I hacked an algorithm that works, though it still has some problems (for example, you cant make an angle completely rigid)

Now put everything together, collisions, links and tensions and you can, in theory, make a cool model of anything, just using balls, which are the easiest shape to work with ;-)