Chipmunk - simple example

BlitzMax Modules Forums/Brucey's Modules/Chipmunk - simple example

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Here's the single box converted to 4...
SuperStrict

Framework BaH.Chipmunk
Import BRL.GLMax2D
?win32
Import BRL.D3D7Max2D
?

' Initialize the physics engine
InitChipmunk()

' create a static body
Local staticBody:CPBody = New CPBody.Create(INFINITY, INFINITY)
	
ResetShapeIdCounter()

' setup the space for the simulation to run in
Local space:CPSpace = New CPSpace.Create()
space.ResizeStaticHash(20.0, 999)
space.SetGravity(Vec2(0, 100))

' create a static segment (line)
Local shape:CPShape = New CPSegmentShape.Create(staticBody, Vec2(0,479), Vec2(640,479), 0)
shape.SetElasticity(1.0)
shape.SetFriction(1.0)
space.AddStaticShape(shape)


Local verts:CPVect[] = [ ..
	Vec2(-8,-8), ..
	Vec2(-8, 8), ..
	Vec2( 8, 8), ..
	Vec2( 8,-8)]
	
' the positions of for boxes
Local positions:CPVect[] = [ ..
	Vec2(300, 20), ..
	Vec2(300, -2), ..
	Vec2(310, 10), ..
	Vec2(289, 10)]

For Local i:Int = 0 Until positions.length
	
	' Create our box body, slighty rotated to make things more exciting
	Local body:CPBody = New CPBody.Create(1.0, MomentForPoly(1.0, verts, Vec2(0,0)))
	body.SetPosition(positions[i])
	body.SetAngle(45)
	space.AddBody(body)
	
	' Create our box shape.
	shape = New CPPolyShape.Create(body, verts, Vec2(0,0))
	shape.SetElasticity(0.6)
	shape.SetFriction(0.3)
	space.AddShape(shape)
Next


Local dt:Float = 1.0/60.0

'
' Create the graphics context and run the main loop
Graphics 640, 480, 0

SetColor(255, 255, 255)

While Not KeyDown(KEY_ESCAPE)

	Cls
	
	' draw shapes...
	space.GetActiveShapes().ForEach(drawObject)
	space.GetStaticShapes().ForEach(drawObject)
	
	' run a simulation step
	space.DoStep(dt)
	
	Flip

Wend

End

' The draw callback
Function drawObject(shape:Object, data:Object)

	If CPPolyShape(shape) Then
		drawPolyShape(CPPolyShape(shape))
	ElseIf CPSegmentShape(shape) Then
		drawSegmentShape(CPSegmentShape(shape))
	End If
	
End Function

' Draw a segment (line)
Function drawSegmentShape(shape:CPSegmentShape)

	SetRotation 0

	Local body:CPBody = shape.GetBody()
	Local pos:CPVect = body.GetPosition()

	Local a:CPVect = pos.Add(shape.GetEndPointA().Rotate(body.GetRot()))
	Local b:CPVect = pos.Add(shape.GetEndPointB().Rotate(body.GetRot()))
	
	DrawLine a.x, a.y, b.x, b.y

End Function

' Draw a poly
Function drawPolyShape(shape:CPPolyShape)

	Local body:CPBody = shape.GetBody()
	Local pos:CPVect = body.GetPosition()

	Local verts:CPVect[] = shape.GetVerts()
	Local First:CPVect
	Local last:CPVect
	
	For Local i:Int = 0 Until verts.length
		Local v:CPVect = pos.Add(verts[i].Rotate(body.GetRot()))
		
		If Not First Then
			First = v
		End If
		
		If last Then
			DrawLine last.x, last.y, v.x, v.y
		End If
		
		last = v
	Next
	
	DrawLine last.x, last.y, First.x, First.y

End Function