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.

right two questions-

i knocked this up with a basic understanding on how chipmunk works, (use the arrow keys to move the box)

SuperStrict

Import Bah.chipmunk

AppTitle = "Untitled Physics Project"

Graphics 800,600

InitChipmunk()

Global staticBody:CPBody = New CPBody.Create(INFINITY, INFINITY)

ResetShapeIdCounter()

Global space:CPSpace = New CPSpace.Create()
	space.ResizeStaticHash(20.0, 999)
	space.SetGravity(Vec2(0, 100))
	

Global box1:CPBody = Box(Vec2(400,300),50,0,20,20,True)



MakeWalls()



Local dt:Float = 1.0/60.0

While Not AppTerminate()
Cls
	
	'update everything
	space.GetActiveShapes().ForEach(drawObject)
	space.GetStaticShapes().ForEach(drawObject)
	space.DoStep(dt)
	CheckInput()

Flip
Wend
End


'makes a box
Function Box:CPBody(p:CPVect, a:Float, w:Float,WIDTH:Int,HEIGHT:Int,r:Int = False)

	Global verts:CPVect[] = [ ..
		Vec2(-WIDTH/2,-HEIGHT/2.0), ..
		Vec2(-WIDTH/2, HEIGHT/2.0), ..
		Vec2( WIDTH/2, HEIGHT/2.0), ..
		Vec2( WIDTH/2,-HEIGHT/2.0) ]
	Rem	
	Global verts:CPVect[] = [ ..
		Vec2( -WIDTH/2,-HEIGHT/2), ..
		Vec2( WIDTH/2, -HEIGHT/2), ..
		Vec2( -WIDTH/2, HEIGHT/2), ..
		Vec2( WIDTH/2, HEIGHT/2) ]
	End Rem
	Local body:CPBody = New CPBody.Create(1.0, MomentForPoly(1.0, verts, CPVZero))
	body.SetPosition(p)
	body.SetAngle(a)
	body.SetAngularVelocity(w)
	space.AddBody(body)
	
	Local shape:CPShape = New CPPolyShape.Create(body, verts, CPVZero)
	shape.SetElasticity(0.6)
	shape.SetFriction(0.3)
	space.AddShape(shape)
	
	If r = True Return body
End Function

' 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


'make the walls
Function Makewalls()
	' create a static segment (line)
	Local shape:CPShape = New CPSegmentShape.Create(staticBody, Vec2(0,599), Vec2(800,599), 0)
	shape.SetElasticity(1.0)
	shape.SetFriction(1.0)
	space.AddStaticShape(shape)
	' create a static segment (line)
	shape = New CPSegmentShape.Create(staticBody, Vec2(0,599), Vec2(0,0), 0)
	shape.SetElasticity(1.0)
	shape.SetFriction(1.0)
	space.AddStaticShape(shape)
	' create a static segment (line)
	shape = New CPSegmentShape.Create(staticBody, Vec2(800,599), Vec2(800,0), 0)
	shape.SetElasticity(1.0)
	shape.SetFriction(1.0)
	space.AddStaticShape(shape)
	' create a static segment (line)
	shape = New CPSegmentShape.Create(staticBody, Vec2(0,0), Vec2(800,0), 0)
	shape.SetElasticity(1.0)
	shape.SetFriction(1.0)
	space.AddStaticShape(shape)
End Function

'Check for user input
Function CheckInput()
	If KeyHit(KEY_RIGHT) box1.applyimpulse(Vec2(200,0),Vec2(200,0))
	If KeyHit(KEY_LEFT) box1.applyimpulse(Vec2(-200,0),Vec2(-200,0))
	If KeyHit(KEY_UP) box1.applyimpulse(Vec2(0,-200),Vec2(0,-200))
	If KeyHit(KEY_DOWN) box1.applyimpulse(Vec2(0,200),Vec2(0,200))
End Function


1) if you push the box fast enough it passes through the lines, any ways to prevent this?
2) Can anyone knock something up involving images? I cant get my head around it, how would you make a image have physics properties, as right now i'm messing with white lines