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.
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