Question

I'm working over simple test game. I have dynamic object that collides with wall. If velocity of the dynamic object is high (I think this is the reason) object walks trough the wall. Does anybody knows how to fix this ?

Wall:

// Create wall ...
    CGRect rect = CGRectMake(0, 0, 25, self.frame.size.height);
    SKNode *wallNode = [SKNode node];
    wallNode.position = CGPointMake(CGRectGetMidX(self.frame) + rect.size.width * 0.5,                                       250);
    wallNode.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:rect.size];
    wallNode.physicsBody.dynamic = NO;
    wallNode.physicsBody.categoryBitMask = CollisionTypeNet;
    [self addChild:wallNode];

Player :

SKSpriteNode *player = [SKSpriteNode spriteNodeWithImageNamed:@"player"];

player.name = playerCategoryName;

player.physicsBody = [SKPhysicsBody bodyWithRectangleOfSize:player.frame.size];
player.physicsBody.restitution = 0.1f;
player.physicsBody.friction = 0.0f;
player.physicsBody.dynamic = YES;

player.physicsBody.categoryBitMask = CollisionTypePlayer;
player.physicsBody.collisionBitMask = CollisionTypeNet ;
player.physicsBody.contactTestBitMask = CollisionTypeWall;
[self addChild:player];
Était-ce utile?

La solution

High velocity "pass throughs" are a common issue. As Cocos stated, set the usesPreciseCollisionDetection to true for your player object.

You should also apply a speed cap to faster moving objects as usesPreciseCollisionDetection is not guaranteed to work on very fast moving objects.

You can apply a speed cap like this:

if(mySpriteA.physicsBody.velocity.dx > 50)
    mySpriteA.physicsBody.velocity = CGVectorMake(50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dx < -50)
    mySpriteA.physicsBody.velocity = CGVectorMake(-50, mySpriteA.physicsBody.velocity.dy);
if(mySpriteA.physicsBody.velocity.dy > 50)
    mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, 50);
if(mySpriteA.physicsBody.velocity.dy < -50)
    mySpriteA.physicsBody.velocity = CGVectorMake(mySpriteA.physicsBody.velocity.dx, -50);
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top