0
votes

I am trying to make a physics world using CCPhysicsNode for a game (physics will be used to bullet collision detection). The debug drawing for the physics bodies are off where they actually are. I believe this may be due to having a CCNode as parent of the physics world, and the CCNode follows the player sprite (game world is larger than screen). Here is the relevant code:

_controlLayer = [CCNode node];
_controlLayer.position = ccp(0, 0);

_gameLayer = [CCNode node];

[self addChild:_gameLayer];
[self addChild:_controlLayer];

_physicsWorld = [CCPhysicsNode node];
_physicsWorld.debugDraw=YES;
_physicsWorld.collisionDelegate = self;
[_gameLayer addChild:_physicsWorld];

[self setupMap];
[self setUpControls];
[self setupPlayer];

_allZombies = [[NSMutableArray alloc]init];

[_gameLayer runAction:[CCActionFollow actionWithTarget:_player worldBoundary:_map.boundingBox]];

-(void)setupPlayer {
     _player = [CCSprite spriteWithImageNamed:@"Player.png"];
     _player.position = CGPointMake(1024/2+60,768/2+60);
     _player.anchorPoint = ccp(0.5, 0.5);
    _player.physicsBody = [CCPhysicsBody bodyWithRect:_player.boundingBox cornerRadius:0];
    _player.physicsBody.elasticity = 0;
    _player.physicsBody.sensor=YES;
    _player.physicsBody.collisionGroup = @"playerGroup";
    _player.physicsBody.collisionType = @"playerCollision";
    [_player setScale:0.5];

    [_physicsWorld addChild:_player];
}

So my question is, why is the drawing off where the player sprite is?

1

1 Answers

0
votes

The physics world doesn't automatically scroll along with the nodes. If you change where things are drawn by changing the position of a node containing the "world objects" then you're offsetting everything visually from where things are in the physics world.

There's a way to scroll the world that acts as if you had a camera panning over the world, leaving the relative coordinates of physics world and nodes (visuals) unaffected. It's explained in the Sprite Kit programming guide, the same setup should also work with cocos2d.