views:

392

answers:

1

I'm making slow but steady progress with a Cocos2d game, but I'm stuck creating moving platforms.

The main character needs physics and collision detection, and is therefore a chipmunk shape/body. I wrote a class to iterate over the TMXTiledMap in order to cut back on the amount of bodies in the chipmunk space. So with a map like this

----------
--------x-
-xxx----x-
----------

instead of having 5 individual bodies (rects), there are two bodies, One is three tiles wide, the other is two tiles tall.

I've managed to get the code working to identify which tiles are part of a moving platform and to move the tiles as needed.

However, the bodies need to move with the tiles in order for this to work properly. And this is where I'm stuck. The bodies are of a static mass so...

platformShape->body->p = cpv(x,y);

Doesn't do anything (I'm guessing that this is the expected behavior).

But if I set their mass to anything other than static, all the physics comes into play and the bodies do not behave as expected, or they behave perfectly depending on you how you look at it. They move erratically and the rotate when they hit another body (eg: the main character). What I'm after is the typical type of moving platform you would expect to find in a typical platform game that moves smoothly in any given direction.

My question is; Has anyone implemented something like this before and what was your technique? Or, if you were to implement something like this, how would you do it?

The relevant code is here. I put it in a pastebin since I figure it's more of a conceptual misunderstanding than anything else.

+1  A: 

It turns out you need to call

cpRehashStaticShapes

Obvious really, but easy to miss in my opinion.

gargantaun