Chipmunk - simple example

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

Yahfree(Posted 2008) [#1]
Hey, i'm having a hard time coping with this, The examples are a bit hard to understand, and the documentation is a little blunt.

Could someone show me how to make a simple cube in space and let it drop to the floor?

Also, i see vec2 used alot, what is this?


Brucey(Posted 2008) [#2]
Okay, here's something reasonably small...



Vec2() is a convenience function to create a 2D vector (x, y), used for things like location, velocity, etc.


plash(Posted 2008) [#3]
Interesting "explosion" effect



Brucey(Posted 2008) [#4]
Here's the single box converted to 4...



Yahfree(Posted 2008) [#5]
Thanks, alot more documented so i understand what the calls are for, brucey, if you could, add one of these to the examples with the module


Yahfree(Posted 2008) [#6]
right two questions-

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



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


Brucey(Posted 2008) [#7]
1) That's called tunnelling.
Basically, the simulation runs in time segements, and, if an object happens to be moving fast enough so that between two steps it could completely pass through another object, the simulation could miss that collision altogether.
There are 2 ways you can use to avoid this.
A) Decrease the time-step size so that in effect, the simulation moves more slowly, and therefore is more likely to catch the contact of a fast moving object.
B) Use Continuous Collision Detection. This is more expensive processing wise, but guarantees collisions won't be missed.

Chipmunk doesn't do CCD, but you can apply A with something like the following to your example above:
' number of calculation steps per visual frame
Local steps:Float = 4

Local dt:Float = 1.0/60.0/steps

While Not AppTerminate()
Cls
	
	'update everything
	space.GetActiveShapes().ForEach(drawObject)
	space.GetStaticShapes().ForEach(drawObject)
	' process the steps
	For Local i:Int = 0 Until steps
		space.DoStep(dt)
	Next
	CheckInput()

Flip
Wend

If you know objects have a limited maximum velocity, you can apply this method without too much of a performance hit.

Box2D does CCD, but I've not finished the wrap yet.


Brucey(Posted 2008) [#8]
Demo2 does images, btw :-)

If you look at main2.bmx, at the drawPolyShape() and drawCircleShape() functions, you'll see it does something like this:
	Local body:CPBody = shape.GetBody()
	Local pos:CPVect = body.GetPosition()


	SetRotation body.GetAngle()
	DrawImage crate, pos.x, pos.y

I believe that the body "position" is the midpoint of the body, which makes using images a bit easier - eg. AutoMidHandle().

Of course, if your body is more complex than a square, (perhaps made up of several different shapes), then your image should match that shape.
As you can see from the code, it's not too difficult.


Yahfree(Posted 2008) [#9]
I see that now, i thought the graphics were chipmunk's internaly, not Blitz shapes!

new question, i'm trying to make a chain function, bugging my thus far, I need to figure out what direction the chain is going (i'm betting on CPVect.angle), to calculate the next position of a link based on the position of the last one..

this is what i have so far:




Yahfree(Posted 2008) [#10]
Hey guys, I couldnt figure out how to calculate the advances dirction in pixels for the chain links, so i just settled on horizontal chains.

I must say, i'm impressed with Chipmunk, but the only major problem it gives me is when you try to stack elastic ships, (they cant hold still) other then that, i'm amazed at how fast it actually is


If anyone is interested, here is the little physics sandbox source-



instructions will be printed at the top, but incase you cant read them:

[1]-[4] makes shapes
[>][<][^][v] (arrow/cursor keys) move the player circle
[P] pauses and plays the simulation, (allowing you to place objects then start)