Ragdolls in FlaDE

BlitzMax Forums/BlitzMax Programming/Ragdolls in FlaDE

I've created a ragdoll thingy, but I'm not sure how to post it on this forum. How do you post things here? Do you just use html?

Put it up in the Code Archives.

thanks. Anyway I can include something from the code archives in to a forum post? Like with bb or html?

try the [codebox ][/codebox ]

You posted yet?
I cant seem to find anything in the code archives, unless it has a cryptic name :)

Sounds cool

Here's the main module...

Strict
'ragdoll

Import "include/frame.bmx"
Include "include/ragdoll.bmx"
Incbin "include/cour.ttf"
Incbin "include/cursor.bmp"

'SetGraphicsDriver GLMax2DDriver()
Graphics 640,480,32,30
'SetBlend ALPHABLEND
'glEnable GL_LINE_SMOOTH

HideMouse()
Global runtime:sim=New sim
runtime.presetcoefficients(COEFFICIENTS_NORMAL)

buildlevel()
Local ragdolls:TList=New TList
Local testobject:ragdoll=New ragdoll
ragdolls.AddLast testobject.create(runtime,100,100)

Global font:TImageFont=LoadImageFont("incbin::include/cour.ttf",11)
SetMaskColor 255,0,255
Global curs:TImage=LoadImage("incbin::include/cursor.bmp")
SetImageFont font

SetClsColor 8,8,8
SetBlend ALPHABLEND

Local gx#,gy#,gravmag#
Local controltype=0

Repeat
	Cls
	
	If KeyDown(KEY_DOWN) Then testobject.applyforce(0,-0.5)
	If KeyDown(KEY_RIGHT) Then testobject.applyforce(-0.5,0)
	If KeyDown(KEY_UP) Then testobject.applyforce(0,0.5)
	If KeyDown(KEY_LEFT) Then testobject.applyforce(0.5,0)
		
	If KeyHit(KEY_RETURN) Then
		testobject=New ragdoll
		ragdolls.AddLast testobject.create(runtime,MouseX(),MouseY())
	EndIf
	
	If KeyHit(KEY_SPACE) Then
		controltype=1-controltype
	EndIf
	
	runtime.simulate()
	runtime.drawsimple(1,0,1)
	
	SetColor 255,255,255
	SetScale .95,.95
	DrawText "frames per sec capper is 30",3,0
	DrawText "frames per sec "+Int(runtime.fps),3,TextHeight("")
	DrawText "millisecs per iteration "+Int(runtime.mspf),3,TextHeight("")*2
	DrawText "memory allocated for simulation "+(GCMemAlloced()/1024)+" kilobytes",3,TextHeight("")*3
	DrawText "memory allocated for physic engine "+SizeOf(runtime)+" bytes",3,TextHeight("")*4
	
	gx=runtime.world.gravity.x
	gy=runtime.world.gravity.y
	
	gravmag=(1.0+gy)/(1.0+gx)
	
	DrawText "coefficients...",400,0
	DrawText "damping  "+(runtime.world.coeffDamp),400,TextHeight("")
	DrawText "friction "+(runtime.world.coeffFric),400,TextHeight("")*2
	DrawText "rest     "+(runtime.world.coeffRest),400,TextHeight("")*3
	DrawText "gravity  "+gravmag,400,TextHeight("")*4
	
	DrawText "number of ragdolls simulated "+ragdolls.Count(),3,TextHeight("")*5
	DrawText "memory used by ragdoll physical data "+SizeOf(ragdolls)+" bytes",3,TextHeight("")*6
	SetScale 1,1
	
	SetColor 255,255,255
	DrawImage curs,MouseX(),MouseY()
	
	Flip
Until esckey()

Function esckey()
	Return KeyDown(KEY_ESCAPE)
EndFunction

Function buildlevel()
	runtime.world.addSurface RectangleTile.create(320,10,639,40)
	runtime.world.addSurface RectangleTile.create(10,240,40,479)
	
	runtime.world.addSurface RectangleTile.create(320,469,639,40)
	runtime.world.addSurface RectangleTile.create(629,240,40,479)
	
	runtime.world.addSurface CircleTile.create(350,240,90)
	runtime.world.addSurface CircleTile.create(200,240,60)
	runtime.world.addSurface CircleTile.create(500,240,60)
EndFunction


and now the ragdoll include...

'ragdoll type
'Strict

Type ragdollposdata
	Field headx#[4]
	Field heady#[4]
	
	Field bodyx#[4]
	Field bodyy#[4]
	
	Field limbhandp1x#[4]
	Field limbhandp1y#[4]
	
	Field limbhandp2x#[4]
	Field limbhandp2y#[4]
	
	Field limbjointx#[4]
	Field limbjointy#[4]
EndType

Type ragdoll
	Field animmode:Int 'can be ragdoll=0 or animated=1
	Field head:SpringBox
	Field body:SpringBox
	Field waist:SpringConstraint
	Field waistconstraint:SpringConstraint[2]
	Field limbjoint:RectangleParticle[4]
	Field limbhandp1:RectangleParticle[4]
	Field limbhandp2:RectangleParticle[4]
	Field upperlimb:SpringConstraint[4]
	Field lowerlimbp1:SpringConstraint[4]
	Field lowerlimbp2:SpringConstraint[4]
	Field lowerlimbp3:SpringConstraint[4]
	Field limbconnect:RectangleParticle[4]
	Field necktruss:TList=New TList
	Field angcons:AngularConstraint[4]
	Field angdefault:Float[4]
	
	Method applyforce(xvel#,yvel#)
		head.p0.prev.y:+yvel
		head.p1.prev.y:+yvel
		head.p2.prev.y:+yvel
		head.p3.prev.y:+yvel
		
		head.p0.prev.x:+xvel
		head.p1.prev.x:+xvel
		head.p2.prev.x:+xvel
		head.p3.prev.x:+xvel
		
		body.p0.prev.y:+yvel
		body.p1.prev.y:+yvel
		body.p2.prev.y:+yvel
		body.p3.prev.y:+yvel
		
		body.p0.prev.x:+xvel
		body.p1.prev.x:+xvel
		body.p2.prev.x:+xvel
		body.p3.prev.x:+xvel
		
		For Local j=0 To 3
			limbjoint[j].prev.y:+yvel
			limbjoint[j].prev.x:+xvel
			
			limbhandp1[j].prev.y:+yvel
			limbhandp1[j].prev.x:+xvel
			
			limbhandp2[j].prev.y:+yvel
			limbhandp2[j].prev.x:+xvel
		Next
	EndMethod
	
	Method setposition(pos:ragdollposdata)
	EndMethod
	
	Method create:ragdoll(physobj:sim,manx#,many#)
		'make bodyparts
		body = SpringBox.create(manx,many,6,18,physobj.world,$3333ff)
		
		waist = SpringConstraint.create(body.p2,body.p3,$3333ff)
		waist.setRestLength(1)
		physobj.world.addConstraint(waist)
		
		head = SpringBox.create(manx,many-23,8,8,physobj.world,$ffff88)
		
		'make neck
		necktruss.AddLast SpringConstraint.create(head.p3,body.p0,$666666)
		necktruss.AddLast SpringConstraint.create(head.p2,body.p1,$666666)
		necktruss.AddLast SpringConstraint.create(body.p0,head.p2,$666666)
		necktruss.AddLast SpringConstraint.create(body.p1,head.p3,$666666)
		
		For Local k:SpringConstraint=EachIn necktruss
			k.setRestLength(2.5)
			physobj.world.addConstraint(k)
		Next
		
		'initialize limb connectors
		limbconnect[0]=body.p0
		limbconnect[1]=body.p1
		limbconnect[2]=body.p2
		limbconnect[3]=body.p3
		
		For Local j=0 To 3
			'make limbs
			limbjoint[j]=RectangleParticle.create(manx, many, 4, 4)
			physobj.world.addPrimitive(limbjoint[j])
			
			limbhandp1[j]=RectangleParticle.create(manx-13, many-3, 4, 4)
			physobj.world.addPrimitive(limbhandp1[j])
			
			limbhandp2[j]=RectangleParticle.create(manx-13, many+3, 4, 4)
			physobj.world.addPrimitive(limbhandp2[j])
			
			upperlimb[j]=SpringConstraint.create(limbconnect[j],limbjoint[j],$ff5555)
			upperlimb[j].setRestLength(6)
			physobj.world.addConstraint(upperlimb[j])
			
			lowerlimbp1[j]=SpringConstraint.create(limbjoint[j],limbhandp1[j],$55ff55)
			lowerlimbp1[j].setRestLength(7)
			
			lowerlimbp2[j]=SpringConstraint.create(limbjoint[j],limbhandp2[j],$55ff55)
			lowerlimbp2[j].setRestLength(7)
			
			lowerlimbp3[j]=SpringConstraint.create(limbhandp2[j],limbhandp1[j],$55ff55)
			lowerlimbp3[j].setRestLength(2)
			
			physobj.world.addConstraint(lowerlimbp1[j])
			physobj.world.addConstraint(lowerlimbp2[j])
			physobj.world.addConstraint(lowerlimbp3[j])
			
			'constrain joints
			angcons[j] = AngularConstraint.create(limbhandp1[j], limbjoint[j], limbconnect[j])
			physobj.world.addConstraint(angcons[j])
			angdefault[j] = angcons[j].targetTheta
			angcons[j].setStiffness(0.1)
			If j<2 Then
				angcons[j].targetTheta:+45
			Else
				angcons[j].targetTheta:-45
			EndIf
		Next
		
		Return Self
	EndMethod
EndType


finally the framework... (you might have to tweak the include statements)
'physics framework
Strict 'disables auto typing

Include "Vector.bmx"
Include "Particle.bmx"
Include "MovieClip.bmx"
Include "Surface.bmx"
Include "CircleParticle.bmx"
Include "RectangleParticle.bmx"
Include "Constraint.bmx"
Include "DynamicsEngine.bmx"
Include "Line.bmx"
Include "AngularConstraint.bmx"
Include "RimParticle.bmx"
Include "Wheel.bmx"
Include "LineSurface.bmx"
Include "Command.bmx"
Include "AbstractTile.bmx"
Include "RectangleTile.bmx"
Include "SpringBox.bmx"
Include "SpringConstraint.bmx"
Include "Gfx.bmx"
Include "CircleTile.bmx"

Const COEFFICIENTS_NORMAL=0
Const COEFFICIENTS_WATER=1
Const COEFFICIENTS_WEIGHTLESS=2

Type sim
	Field world:DynamicsEngine
	Field fps#,fpms#,mspf#,oms#,ms#
	Global ctr
	
	Method New()
		world=DynamicsEngine.create()
		world.setDamping(.998)
		world.setGravity(.0,.191)
		world.setSurfaceBounce(.35)
		world.setSurfaceFriction(.5)
	EndMethod
	
	Method oldcoefficients()
		setcoefficients()
	EndMethod
	
	Method presetcoefficients(coeftype)
		Select coeftype
		Case 0
			oldcoefficients()
		Case 1
			setcoefficients(0,.091,.9,.9)
		Case 2
			setcoefficients(0,0)
		EndSelect
	EndMethod
	
	Method setcoefficients(gravx#=0,gravy#=.191,damp#=.998,bounce#=.35,fric#=.5)
		world.setDamping(damp)
		world.setGravity(gravx,gravy)
		world.setSurfaceBounce(bounce)
		world.setSurfaceFriction(fric)
	EndMethod
	
	Method drawsimple(drawsurf=1,drawprim=1,drawcons=1)
		If drawsurf Then world.paintSurfaces()
		If drawcons Then world.paintConstraints()
		If drawprim Then world.paintPrimitives()
	EndMethod
	
	Method simulate()
		world.timeStep()
		ctr:+1
		
		Local ms=MilliSecs()
		If ctr>3 Then
			mspf=Abs(ms-oms)
			fpms=mspf^-1
			fps=fpms*1000
			ctr=0
		EndIf
		oms=ms
	EndMethod
EndType


There! Call the ragdoll include "ragdoll.bmx" and the framework "frame.bmx" and put them in a directory called "include". Then download the ported version of the FlaDE engine and put the files in the same include folder, and you've got it! Enjoy!

Oh, yeah, and Admin, if you're reading this, could you please put a copy of this in the code archive? I put it here and realised this was probably not the right place for it later.