chain simulator: can you do it?

Miscellaneous Forums/Blitz Showcase/chain simulator: can you do it?

I made this "chain simulator" some time ago...

http://radicalrebound.com/chain.exe

i.e. there are a bunch of balls linked one to each other, and you can grab one (clicking the mouse) moving all the chain.

It uses a really neat trick to do the physics (which otherwise are really a mess), the code is 280 lines.

I'm not giving the code away yet: can you do something similar?

Sure. Its easy. Physics isnt a mess at all. All you need to do this is an integrator and a constraint system. I'm suprised it took as much as 280 lines. Especially since the balls dont even have collisions. and its 2d...

The only time physics is a mess is when you factor in stuff like relativity, quantum mechanics, and string theory.

Started coding this 4:40 PM (now its 5:20 PM) so, a 40 minute job

Graphics 640,480,32,2

ov.verlet=New verlet
ov\x=20
ov\y=20
ov\ox=20+Rnd(-2,2)
ov\oy=20+Rnd(-2,2)
For i=1 To 10
	v.verlet=New verlet
	v\x=i*40+40
	v\y=40
	v\ox=i*40+40+Rnd(-2,2)
	v\oy=40+Rnd(-2,2)
	v\size=20
	c.constraint=New constraint
	c\Head=ov
	c\Tail=v
	c\Length=40
	ov=v
Next

SetBuffer BackBuffer()
While Not KeyHit(1)
	Update
	Draw
	If MouseDown(1) Then
		If selection.verlet<>Null Then
			selection\x=(selection\x+MouseX())/2
			selection\y=(selection\y+MouseY())/2
		Else
			For v.verlet=Each verlet
				If MouseX()>v\x-v\size Then
					If MouseX()<v\x+v\size Then		;optimized bounding box check first
						If MouseY()>v\y-v\size Then
							If MouseY()<v\y+v\size Then
								If Sqr((MouseX()-v\x)*(MouseX()-v\x)+(MouseY()-v\y)*(MouseY()-v\y))<v\size Then
									selection=v
									Goto skipNull
								EndIf
							EndIf
						EndIf
					EndIf
				EndIf
			Next
			selection=Null
			.skipnull
		EndIf
	Else
		selection=Null
	EndIf
	Flip
	Cls
Wend

Type Verlet
 Field x#, y#, ox#, oy#, size
End Type

Type Constraint
 Field Head.Verlet, Tail.Verlet, Length
End Type

Function Update()
	For v.Verlet=Each Verlet
		tx#=v\x
		ty#=v\y
		v\x=v\x+.99*(v\x-v\ox)
		v\y=v\y+.99*(v\y-v\oy)+.1
		If v\x>GraphicsWidth()-v\size Then v\x=GraphicsWidth()-v\size ElseIf v\x<v\size Then v\x=v\size 
		If v\y>GraphicsHeight()-v\size Then v\y=GraphicsHeight()-v\size ElseIf v\y<v\size Then v\y=v\size
		v\ox=tx
		v\oy=ty
	Next
	For i=0 To 10				;Iterate
		For c.Constraint=Each Constraint
			dx#=c\Tail\x-c\Head\x
			dy#=c\Tail\y-c\Head\y 
			dl#=Sqr(dx*dx+dy*dy)
			d#=(dl-c\length)/Float(dl)
			dx#=dx/2
			dy#=dy/2
			c\Head\x=c\Head\x+d*dx
			c\Head\y=c\Head\y+d*dy
			c\Tail\x=c\Tail\x-d*dx
			c\Tail\y=c\Tail\y-d*dy		;Simple, optimized rigid constraint algo
		Next
	Next
End Function

Function Draw()
	For v.verlet=Each verlet
		Oval v\x-v\size,v\y-v\size,v\size Shl 1,v\size Shl 1
	Next
End Function


This system can actually mimic any physical structure in 2 dimensions. Just 94 lines. I would add ball-ball collision, but im too lazy, and have only met your challenge. Done this before in 3d, and it was pretty simple. Same stuff actually except for collisions.

wow.

I thought I was pretty smart to get the algorithm, but looks that everybody knows it... you move the balls as if there was no link then you get them back together in various iterations, right? its a neat trick, I thought I had invented haha.

yeah, its 280 lines because I skip a line between two lines and I added some stuff that is commented and I put some code to change the colors :P

oh, since youre there you might also check out this one, its good enough

http://radicalrebound.com/particles.zip

I'm sure you can do this one :P

Yeah, this method is usually used in any game where you see ragdolls. It is also used in alot of rigid body systems, at least as a base, since fairly complex collision algorithms for affecting individual points must be invoked. 2d is a really nice place to do physics, once you get to 3d, the collisiosn stuff gets yah :) not impossible but alot harder than 2d.

Anyway, yah, thats pretty cool. I could probably make it, but since we have the allegro version there's not too much point.

lol

you were-are in the allegro forums too? I quit using allegro (not that I used it much, I made that thing only!), I bought blitzmax, very cool.

I'd like to do a 2d beatem-up where characters are made of balls, but I never get the time. I stopped at the chain thing haha...

yeah, I guess in 3D its a mess but I just love 2D. Moreover I have never seen a 2d beatemup using ragdoll. I guess its a matter of applying strength to keep the "human chain" from collapsing and making it move. but it should be cool.

just have to find some time...

Nah, I just recognize the dll.

Yeah, 2d ragdoll would be cool. It'd have to be semi-3d though or else it wouldnt work.

Najdorf: Somebody got that idea first ;)
http://www.lionhead.com/personal/mhealey/index.html

thats awesome. I'll keep it simpler though.