Chipmunk free problems

BlitzMax Forums/BlitzMax Programming/Chipmunk free problems

VinceA(Posted 2007) [#1]
EDIT (Solved)
Can anybody help me removing objects in Chipmunk. If anybody have a demo would I love to see it or if anybody can tell me what’s wrong in the code sample below.
I also think a GetCollisionType() method for shapes could be really useful.

SuperStrict
Import BaH.Chipmunk

Global staticBody:CPBody
Global space:CPSpace
Global ticks:Int = 0
Global removeList:TList = CreateList()

InitChipmunk()
init()


Graphics 640, 480, 0

SetColor(255, 255, 255)

While Not KeyDown(KEY_ESCAPE)

	Cls
	
	ticks:+ 1
	update(ticks)
	
	If MouseHit(1) Then 
		Local verts:CPVect[] = [ ..
		Vec2(-15,-15), ..
		Vec2(-15, 15), ..
		Vec2( 15, 15), ..
		Vec2( 15,-15)]

		Local box:CPBody = New CPBody.Create(1.0, MomentForPoly(1.0, verts, Vec2(0,0)))
		box.SetPosition(Vec2(MouseX(), MouseY()))
		space.AddBody(box)
	
		Local shape:CPShape = New CPPolyShape.Create(box, verts, Vec2(0,0))
		shape.SetElasticity(0)
		shape.SetFriction(1.5)
		shape.SetCollisionType(1)
		space.AddShape(shape)
	End If
	
	If MouseHit(2) Then
		For Local sh:CPShape = EachIn  removeList 
		  	sh.GetBody().Free()
			sh.Free()
			removeList.remove(sh)
		Next
	End If

	space.GetActiveShapes().ForEach(drawObject)
	space.GetStaticShapes().ForEach(drawObject)
	
	DrawText " Add Box: Left Click",0,0
	DrawText " Remove Boxes in contact with ground: Right Click",0,20

	Flip
Wend

End



Function update(ticks:Int)
	Local steps:Int = 10
	Local dt:Float = 1.0/60.0/steps
	
	For Local i:Int = 0 Until steps
		space.DoStep(dt)
	Next
End Function


Function collFunc:Int(shapeA:cpShape, shapeB:cpShape, contacts:CPContact[], normalCoeficient:Float, data:Object)
		
	If CPPolyShape(shapeA) And Not(removeList.Contains(shapeA)) Then removeList.AddFirst(shapeA)
	
	If CPPolyShape(shapeB) And Not(removeList.Contains(shapeB)) Then removeList.AddFirst(shapeB)

	Return True
End Function


Function drawObject(shape:Object, data:Object)
	If CPPolyShape(shape) Then
		drawPolyShape(CPPolyShape(shape))
	ElseIf CPSegmentShape(shape) Then
		drawSegmentShape(CPSegmentShape(shape))	
	End If
End Function

Function init()
	staticBody = New CPBody.Create(INFINITY, INFINITY)
	ResetShapeIdCounter()
	space = New CPSpace.Create()
	space.ResizeStaticHash(20.0, 999)
	space.SetGravity(Vec2(0,100))
	Local ground:CPShape = New CPSegmentShape.Create(staticBody, Vec2(0, 450), Vec2(640, 450), 0)
	ground.SetElasticity(3.0)
	ground.SetFriction(1.0)
	space.AddStaticShape(ground)
	space.AddCollisionPairFunc(1, 0, collFunc)	
End Function

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

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



Brucey(Posted 2007) [#2]
Nothing apparently wrong with it here. Left click adds a box, right click removes them :-)

Are you using the latest version of the module?

1.02 was released on Nov 28. (and there's always SVN-access for the latest pre-release code if you are in a hurry).

http://code.google.com/p/maxmods/downloads/list

And yes, we should probably have GetCollisionType()!


VinceA(Posted 2007) [#3]
I have tried to build the update version with no luck.
Is it possible to download an already build version?

I should note that the Free() problems only occur when you add a number of objects before removing them. For some reason it works fine with only one object.

Anyways love the mod. (Blazing fast!)


Brucey(Posted 2007) [#4]
I just updated my Windows Max to 1.28, unzipped chipmunk_1_02_src.zip in BlitzMax/mod/bah.mod (so that it created a folder chipmunk.mod in there), and ran Build Modules.

Pasted the above source into Max, and it seems to be running great :-)
Adding lots of boxes and then right-clicking when some are touching the floor results in those disappearing, and any that were on top, falling down until they too land on the floor.

What problems are you having?


VinceA(Posted 2007) [#5]
When i try to build modules i get:

Building Modules
Compiling:blitz_app.c
Build Error: failed to compile C:/Programmer/BlitzMax/mod/brl.mod/blitz.mod/blitz_app.c
Process complete

I'm not really sure what i'm doing wrong..


Brucey(Posted 2007) [#6]
Ahhh... you need MinGW installed to compile modules.

Have a look HERE for details.


Brucey(Posted 2007) [#7]
Just for you, and because it's Freyja's day (yay!), I've added a pre-compiled version of the module on the downloads page for Win32 users that may not have MinGW installed.

:o)


VinceA(Posted 2007) [#8]
The strange thing is that it MinGW IS installed. Before i did'nt have the build module option. hmm..
I quess I have made made a mistake somewhere


VinceA(Posted 2007) [#9]
hehe .. Ahhh cool .. Thanks Alot