English 中文(简体)
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.

问题回答

暂无回答




相关问题
Cocos2d - Shooting Game Problem

everyone, I made a shooting game just like 1942(classic shooting game),but here s the question, FPS drops to 5~6 when the fighter shoot out a straight line of bullet,about 7~8 bullets in the screen,...

AtlasSpriteManager animation

I am using AtlasSpriteManager and AltasSprite to create frame by fram animation with 1 one file. I wanna write something that at first show a simple picture, without any animation and for example when ...

background moves together with image

I have 4 images and a background. I already about to animate the image to move accordingly. But the back ground is not moving. I am not sure how to make the background move accordingly to the movement ...

NSMutable array invalid after objectAtIndex

@implementation Level @synthesize doors, rooms; - (id) init { self = [super init]; if (self != nil) { rooms = [[NSMutableArray alloc] init]; doors = [[NSMutableArray alloc] init]; ...

How to subclass AtlasSpriteManager in Cocos2d?

I need to create some compound sprites that will all move and rotate together. Since it s possible to change the position and rotation of an AtlasSpriteManager I ve been trying to subclass so I can ...

热门标签