开发者

box2d iterations

Hey, im programming for the iphone using box2d and cocos2d frameworks.

Currently, 开发者_运维百科I have 3 Classes all subclasses of CCSprite.

They are: DynamicBlock1, DynamicBlock2 and DynamicEgg1

Basically my problem is that I want to separate the iterations in the tick method so that I can iterate certain classes and not others.

Currently the Tick (iteration) method is split into two parts, isMoving == YES/NO.. This switches the iteration between b2body(Master)-sprite(Slave) and b2body(Slave)-sprite(Master). This way I can delegate who controls who. And it works quite well.

Once again, the problem is that this code below, will iterate over ALL my bodies from all my classes in GetBodyList(). When I just want the iteration to occur to the one class (DynamicBlock1)...

Is there a way to do this? To isolate the iterations?

A thousand thank you's

Oliver

-(void) tick:(ccTime)dt
{
int32 velocityIterations = 8;
int32 positionIterations = 1;
world->Step(dt, velocityIterations, positionIterations);

for (b2Body* b = world->GetBodyList(); b; b = b->GetNext())
{
    DynamicBlock1 *block1 = (DynamicBlock1*)b->GetUserData();
            if (block1.isMoving == NO){
                    block1.position = CGPointMake( b->GetPosition().x * PTM_RATIO, b->GetPosition().y * PTM_RATIO);
                    block1.rotation = -1 * CC_RADIANS_TO_DEGREES(b->GetAngle());
            }
            if (block1.isMoving == YES){
                    b2Vec2 b2Position = b2Vec2(block1.position.x/PTM_RATIO, block1.position.y/PTM_RATIO);
                    float32 b2Angle = -1 * CC_DEGREES_TO_RADIANS(block1.rotation);
                    b->SetTransform(b2Position,b2Angle);
            }
    }
}


If you want to separate the iterations you have to maintain separate lists (or Arrays) for different classes yourself. And whenever you create body, you can add the reference to that body in respective lists. For example you have lists like

NSArray *DynamicBlocksList; NSArray *DynamicEgssList;

Now when you create DynamicBlock body, you also add it in "DynamicBlocksList" and when this body is destroyed, you can remove it from the list. And in Tick() function you can iterate only required list.

But in my personal opinion this may not be a very good idea to do this. Its fine to iterate over "world->GetBodyList()" and then you can delegate the processing of different bodies to different classes or functions.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜