开发者

How to implement moving platforms with Cocos2d, TMXTiledMaps and Chipmunk

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.


It turns out you need to call

cpRehashStaticShapes

Obvious really, but easy to miss in my opinion.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜