PDA

View Full Version : Simple Chipmunk Physics



Cybermonkey
01-07-2011, 03:24 PM
Hi,

I feel a bit stupid, but is there anyone who could explain me how to implement the chipmunk physics in a Pascal program? I know there is cpInit etc. But let's assume I just want to have a ball which "bounces" and it is drawn with a simple circle function. Could anyone post some (pseudo)code which explains when I have to do the drawing? And how to "init" the physics system?

Andru
01-07-2011, 07:32 PM
Hmmm, demo11 from ZenGL (http://zengl.org/) maybe can help you with initialization of Chipmunk, and drawing... anyway it will be easy to translate into other engine/API :) As soon as I resolve a problem with translated Chipmunk demos into Pascal for Linux(using pure OpenGL and glut), I will add them to my Chipmunk Pascal binding - chipmunk-pascal (http://code.google.com/p/chipmunk-pascal/).

Cybermonkey
10-07-2011, 08:31 PM
Okay, I guess I can initialize chipmunk, but again... In demo11 the drawing is done by
cpDrawSpace( space, TRUE );
which means I have to "break down" the source of ZenGL. What if I would add a "ball" with your procedure
procedure cpAddBall( x, y, r, mass, e, u : cpFloat );
how do I know where to draw the circle when physics were calculated. Assuming my procedure looks like
circle (x,y,radius) and my engine is purely procedural driven which means I have to establish a game loop like

repeat
key=getkey();
//do all the stuff
until key=27;
How can I get the x and y value of my ball?

Cybermonkey
24-07-2011, 04:01 PM
Ok, since no one can answer me, I'll post here an example from Freebasic ;)

''
'' My first chipmunk simulation
''
#include "chipmunk/chipmunk.bi"
#inclib "chipmunk"

const as cpFloat mass = 5.0, radius = 10 ' Mass and radius values for our circle - you can play around with these
const as cpFloat dt = 60.0 / 1.0 ' The delta time for physics calculations

dim shared space as cpSpace ptr ' The space where the simulation takes place

screenres 800, 600, 16 ' Setup the screen

cpInitChipmunk() ' Initialize chipmunk

space = cpSpaceNew() ' Create a new space
space->iterations = 10 ' 10 Iterations per step
space->gravity = cpv(0, 50) ' Gravity x = 0, y = 50

dim shared circleBody as cpBody ptr ' Circle body
dim shared circleShape as cpShape ptr ' Circle shape

' Create a new circle body
circleBody = cpBodyNew( mass, cpMomentForCircle( mass, 0.0, radius, cpvzero ) )
circleBody->p = cpv( 400, 0 ) ' set the circle at the top-middle of the screen
cpSpaceAddBody( space, circleBody ) ' add the body to the simulation

' Crate a new circle shape
circleShape = cpCircleShapeNew( circleBody, radius, cpvzero )
cpSpaceAddShape( space, circleShape ) ' add the shape to the simulation

do
screenlock()
cls
locate 1, 1: print "The ball should fall down into a bottomless pit"

circle( circleBody->p.x, circleBody->p.y ), radius, rgb( 255, 0, 0 )
screenunlock()

' Advance the simulation
cpSpaceStep( space, dt )

sleep 1, 1
loop until multikey(&H01) ' Loop until Escape is pressed

' Cleans up everything associated with this space
cpSpaceFreeChildren( space )
cpSpaceFree( space )

end 0So I will try to port this to FreePascal. (Of course there's still a timer missing but one step after the other ...)

Andru
24-07-2011, 07:52 PM
and my engine is purely procedural driven which means I have to establish a game loop like
ZenGL is also fully procedural.


How can I get the x and y value of my ball?
Is it so hard to open zglChipmunk.pas and look to cpDrawSpace(and cpDrawShape), which is used in Draw function of demo11? :)

// sorry for late answer...

Ingemar
30-07-2011, 12:20 PM
Hmmm, demo11 from ZenGL (http://zengl.org/) maybe can help you with initialization of Chipmunk, and drawing... anyway it will be easy to translate into other engine/API :) As soon as I resolve a problem with translated Chipmunk demos into Pascal for Linux(using pure OpenGL and glut), I will add them to my Chipmunk Pascal binding - chipmunk-pascal (http://code.google.com/p/chipmunk-pascal/).

A bit incomplete though, with libraries for Windows and Linux but not Mac. Also, a simple demo would be helpful.

(And I am saying that just because FPC interfaces to Chipmunk Physics sounds really, really useful so I really want to try it!)

Andru
31-07-2011, 01:09 PM
A bit incomplete though, with libraries for Windows and Linux but not Mac. Also, a simple demo would be helpful.
Mmm? And what is libchipmunk.5.3.2.dylib in bin directory do you think? :) This library, and simple demo, you can find with ZenGL(demo11). Unfortunately I have no time to finish that chipmunk demos, about which I said before.

Ingemar
14-08-2011, 12:36 AM
Maybe so but I was referring to what I found at the chipmunk-pascal link.

Andru
14-08-2011, 10:25 AM
Maybe so but I was referring to what I found at the chipmunk-pascal link.
In chipmunk-pascal svn you can find libchipmunk.dylib too (http://code.google.com/p/chipmunk-pascal/source/browse/#svn%2Ftrunk%2Flib%2Fmacosx_i386).

Cybermonkey
25-02-2012, 09:40 AM
Two days ago I took another look at the Chipmunk physics. Well, I made some progress, as you can see here (using my SDL based egslengine):


http://youtu.be/d_4WQqkOJfE

However, I have a problem on Windows 32bit with the static linking (FPC 2.6.0) using the latest headers by Andru. Issue is

Target OS: Win32 for i386
Compiling physictest.pas
Linking physictest.exe
physictest.pas(124,1) Error: Undefined symbol: _cpInitChipmunk
physictest.pas(124,1) Error: Undefined symbol: _cpMomentForCircle
physictest.pas(124,1) Fatal: There were 2 errors compiling module, stopping
Fatal: Compilation aborted

Any idea?

Andru
25-02-2012, 11:03 AM
using the latest headers by Andru
Are you sure about latest? And if so - did you change path to object files? Structure of lib directory was changed, so in Lazarus you should specify something like this now - lib/$(TargetCPU)-$(TargetOS), or choose path for platform manually(in your case this is i386-win32).

Cybermonkey
25-02-2012, 11:53 AM
Oh, sorry, it was my fault. It works now. Thank you.