rigid bodies
-
- Posts: 12
- Joined: Tue Mar 02, 2010 9:25 pm
- Contact:
rigid bodies
Slembcke, in your code & readme you say "you should use the rigid bodies to determine where to draw your sprites." thanks for that - is there some example code somewhere that does that? I've looked over the demo code, and there's some great examples in objective-C on OSX - but I can't find a simple C / C++ example using rigid bodies for drawing stuff. Maybe I just missed it. Thanks!
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: rigid bodies
By that I mean that you should use the cpBody.p and cpBody.a to determine what position and angle (in radians) to draw your sprite at. People seem to more closely associate collision shapes with sprites, and a collision shape isn't really going to help you to figure out where to draw your sprite at.
Additionally, if you are using OpenGL, you can skip straight to using the body to generate a transformation matrix. I have a code snippet hidden away in an unorganized section of the documentation wiki that does that.
Additionally, if you are using OpenGL, you can skip straight to using the body to generate a transformation matrix. I have a code snippet hidden away in an unorganized section of the documentation wiki that does that.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 12
- Joined: Tue Mar 02, 2010 9:25 pm
- Contact:
Re: rigid bodies
Thanks for that code pointer slembcke! I'll definitely take a look at that. Let me ask another question – I’ve been staring at the demo code for two nights now – I’m familiar with OpenGL & glut – that’s all familiar. On the way the demo draws things I see that you:
Going back to the original question where you say "you should use the rigid bodies to determine where to draw your sprites" ... are you saying we’re NOT to use the method of drawing above? Are we supposed to do it at a higher level? If so is there a code sample out there (in C / C++) that demonstrates this? Sorry if I'm not catching on quick enough
p.s. full disclosure: I’m trying to write a JNI wrapper to chipmunk so I can use it on my android app. This will be the second time I’ve done something like this – my first app was an Apple2 Emulator. Thanks!
Code: Select all
1. Call drawspace in the glutmainloop
2. drawSpace passes a pointer to the function drawObject to cpSpaceHashEach
3. cpSpaceHashEach calls cpHashSetEach with the drawSpace function in the pair
4. cpHashSetEach calls the function

p.s. full disclosure: I’m trying to write a JNI wrapper to chipmunk so I can use it on my android app. This will be the second time I’ve done something like this – my first app was an Apple2 Emulator. Thanks!
- slembcke
- Site Admin
- Posts: 4166
- Joined: Tue Aug 14, 2007 7:13 pm
- Contact:
Re: rigid bodies
From drawspace.c
Basically, in your sprite code, you should keep a reference to the chipmunk body that the sprite is attached to. Then when drawing your sprites, it pulls the transform out of the Chipmunk body. Iterating the bodies in a Chipmunk space and using the data pointer on them to get at your sprite data is a bad idea because Chipmunk spaces were not designed to be easily or efficiently iterated. Additionally, Chipmunk's hash tables don't have a guaranteed iteration order. You won't get a consistent drawing order if you add/remove things from the space.
I don't have any simple code handy, but how I tend to write Chipmunk based games is to make a GameObject that can be added/removed to a GameState. The gamestate has a rendering system and the physics system. When you add/remove GameObjects, the GameState adds/removes their sprites and physics objects from their respective structures. Then the GameState can update the physics without having to know anything about the graphics, and the graphics can be rendered while knowing a minimal amount (only about the body transforms) about the physics.
Code: Select all
/*
IMPORTANT - READ ME!
This file sets up a simple interface that the individual demos can use to get
a Chipmunk space running and draw what's in it. In order to keep the Chipmunk
examples clean and simple, they contain no graphics code. All drawing is done
by accessing the Chipmunk structures at a very low level. It is NOT
recommended to write a game or application this way as it does not scale
beyond simple shape drawing and is very dependent on implementation details
about Chipmunk which may change with little to no warning.
*/
I don't have any simple code handy, but how I tend to write Chipmunk based games is to make a GameObject that can be added/removed to a GameState. The gamestate has a rendering system and the physics system. When you add/remove GameObjects, the GameState adds/removes their sprites and physics objects from their respective structures. Then the GameState can update the physics without having to know anything about the graphics, and the graphics can be rendered while knowing a minimal amount (only about the body transforms) about the physics.
Can't sleep... Chipmunks will eat me...
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
Check out our latest projects! -> http://howlingmoonsoftware.com/wordpress/
-
- Posts: 12
- Joined: Tue Mar 02, 2010 9:25 pm
- Contact:
Re: rigid bodies
Ok, thanks slembcke I really appreciate you replying to my messages, it's great to talk directly to the developer! I think your code is brilliant (I can tell by the demo's - very nice) but I think I'm going to look around and see if I can find another engine that is more accessable / has more fundamental code examples. For example - I'm trying to get this going - but I don't even know how to iterate through the objects in the space! I think for an expert with lots of time w/ coding skillz near your level they would have this all figured out in a weekend, but I have a wife & two kids - I need some bootstrapping
cheers!
Bart

cheers!
Bart
- Tam Toucan
- Posts: 141
- Joined: Tue Jun 23, 2009 4:26 pm
- Contact:
Re: rigid bodies
IMO I think you might have trouble finding a more accessible library. They are all going to have a learning curve and Chipmunk's it pretty low.
For Chipmunk you never need to iterate through the objects. All you do (and this is what Scott was saying) is keep your own list of the objects. You'll have to do this anyway since Chipmunk is just Physics so you need to have some kind of object to store all the other stuff you need e.g. health, ammo, graphic, color, number of bananas, etc.
One of the things your object will contain is a reference to the cpBody(s) and cpShape(s) making up the object. When you need to draw your object you just use the cpBody.p and cpBody.a. When you need to move the object you apply forces or impulses to them and Chipmunk does the rest. To detect collisions you register collision functions to be called when things collide. That's pretty much it.
Anyway good luck with whatever you decide.
For Chipmunk you never need to iterate through the objects. All you do (and this is what Scott was saying) is keep your own list of the objects. You'll have to do this anyway since Chipmunk is just Physics so you need to have some kind of object to store all the other stuff you need e.g. health, ammo, graphic, color, number of bananas, etc.
One of the things your object will contain is a reference to the cpBody(s) and cpShape(s) making up the object. When you need to draw your object you just use the cpBody.p and cpBody.a. When you need to move the object you apply forces or impulses to them and Chipmunk does the rest. To detect collisions you register collision functions to be called when things collide. That's pretty much it.
Anyway good luck with whatever you decide.
-
- Posts: 12
- Joined: Tue Mar 02, 2010 9:25 pm
- Contact:
Re: rigid bodies
That's cool Tam, do you know if there's a simple code example out there that demonstrates that? I don't need a whole game engine, just a simple working example of someone actually using the engine the way you said.
Maybe you could give me something to work with and I can clean it up and post it on the wiki for other people like me to re-use?
Here's the thing - the demos are great - but if that's not how I'm supposed to use the engine, I need a practical working example of how I should. I get the feeling one of you guys could bust out a main.cpp file in like 30 minutes that would demonstrate this - any chance? I can donate!!!
And I want to give back. I'm also very grateful that slembcke opened up the source code. Very generous.

Here's the thing - the demos are great - but if that's not how I'm supposed to use the engine, I need a practical working example of how I should. I get the feeling one of you guys could bust out a main.cpp file in like 30 minutes that would demonstrate this - any chance? I can donate!!!

- Tam Toucan
- Posts: 141
- Joined: Tue Jun 23, 2009 4:26 pm
- Contact:
Re: rigid bodies
Thing is what you are looking for is nothing to do with and is independent of Chipmunk. The basic idea of what we are describing is very simple, but there are a million ways to do it depending on what you want. There isn't a "right way" to do it. It's the design of a game engine.
Look at it this way, you could write the entire thing without knowing anything about Chipmunk, but with interfaces to get positions, move things and for collisions. You could then plugin Chipmunk or some other physics library.
My real code is tons and tons of over-engineered rubbish. It relies on loads of my library classes e.g. a InfoFile class which does a sort of key/value pair mapping which I then use to make config files to represent the Chipmunk parts (bodies, shapes, constraints) and the graphical parts (textures, shaders etc) and the linkage between the two (parts have names so I bind a Chipmunk body to a game graphic).
So for 30mins all I can provide is the following typed in OTTOMH example API (not full code). This isn't how I currently do it, and I'm not saying this is a good way, there's probably a major flaw in API design, blah blah blah, but hopefully it will get across the ideas..I've not shown adding collisions, but again you get the idea
Look at it this way, you could write the entire thing without knowing anything about Chipmunk, but with interfaces to get positions, move things and for collisions. You could then plugin Chipmunk or some other physics library.
My real code is tons and tons of over-engineered rubbish. It relies on loads of my library classes e.g. a InfoFile class which does a sort of key/value pair mapping which I then use to make config files to represent the Chipmunk parts (bodies, shapes, constraints) and the graphical parts (textures, shaders etc) and the linkage between the two (parts have names so I bind a Chipmunk body to a game graphic).
So for 30mins all I can provide is the following typed in OTTOMH example API (not full code). This isn't how I currently do it, and I'm not saying this is a good way, there's probably a major flaw in API design, blah blah blah, but hopefully it will get across the ideas..
Code: Select all
#include <vector>
#include <list>
// Represent a position and angle of an object
struct Position
{
float x,y;
float angle;
};
// Interface to provide a world Position to render a GameGraphic at
class I_PostionProvider
{
public:
virtual Position getPosition() = 0;
};
class GameGraphic
{
public:
// Some kind class to represent a graphic
};
// A Game Object. Has a graphic and a position so that it can be rendered
//
class I_GameObject : public I_PostionProvider
{
public:
virtual Position getPosition() = 0;
virtual GameGraphic* getGraphic() = 0;
virtual void remove() = 0;
};
// ChipmunkObject to provide position
//
class ChipmunkObject : public I_PostionProvider
{
public:
// Populate Position using cpBody.p and a
Position getPosition();
private:
cpBody* pBody;
std::vector<cpShape*> shapeList;
};
// Probably Singleton class
//
class GameWorld
{
public:
// Add/Remove objects
void addObject(I_GameObject* pObject);
void removeObject(I_GameObject* pObject);
// Render the game. Calls redner on all the I_GameObject's
void render();
private:
std::list<I_GameObject*> objectList;
};
// Probably Singleton
//
class ChipmunkWorld
{
public:
// call cpSpaceStep
void update(float timePassed);
ChipmunkObject* createObject(/* some kind of config */);
void removeObject(ChipmunkObject* pObject);
};
class Player : public I_GameObject
{
public:
Player()
{
// create ChipmunkObject
// create GameGraphic
};
// Get position from the ChipmunkObject
Position getPosition();
// Get the graphic from the GameGraphic
GameGraphic* getGraphic() = 0;
// Call ChipmunkWorld::removeObject passing in the ChipmunkObject
void remove() = 0;
// Need some kind of movement method to apply forces etc to ChipmunkObject
private:
ChipmunkObject* pChipmunkObject;
GameGraphic* pGameGraphic;
};
-
- Posts: 12
- Joined: Tue Mar 02, 2010 9:25 pm
- Contact:
Re: rigid bodies
Thanks Tam - yes you nailed it. The wiki would benefit from a simple generic game engine example (maybe even with static entities, nothing fancy) that demonstrates how to plug the engine into it with collisions.
p.s. thanks for the code example you put up - can you provide any of the actual code? Like what getPosition looks like? thanks
p.s. thanks for the code example you put up - can you provide any of the actual code? Like what getPosition looks like? thanks
- Tam Toucan
- Posts: 141
- Joined: Tue Jun 23, 2009 4:26 pm
- Contact:
Re: rigid bodies
Erm, it's just going to beLouisB wrote:Like what getPosition looks like? thanks
Code: Select all
Position ChipmunkObject::getPosition()
{
Position retVal;
retVal.x = float(pBody->p.x);
retVal.y = float(pBody->p.y);
retVal.angle = float(pBody->a);
retun retVal;
};
I'm really not sure the wiki would benefit from some kind of example. The demos and wiki show how to use Chipmunk and my example isn't showing anything new. And it isn't showing a solution to a chipmunk problem. It's something that needs solved regardless and as I mentioned there are a million and one ways to do it. A game has to have some list of objects which have positions and graphics. At the simplest level all that's required to integrate Chipmunk is to store the cpBody and get the position and angle from it.
This is actually the first time I've seen someone posting about this. The most common problem people seem to have is how to link the Chipmunk objects (cpBody/cpShape) to their equivalent of GameObject's. That is in fact a really simple thing since you just use the data pointer, but somehow loads of people get hung up on it. I think an example of kind you want would actually generate more questions and problems than it would solve since it can't be a full proper solution (there is no one size fits all solution to game engine design).
Who is online
Users browsing this forum: No registered users and 7 guests