物理游戏与cocos2d for iphone v3.0.0

时间:2014-06-04 08:44:17

标签: cocos2d-iphone game-physics

我是cocos2d-iphone的新手,我正在用cocos2d-iphone创建关于物理的演示应用

我想创建一个基本物理节点的轮廓(节点#1 ),以保持某个节点“在门内”

我将一些节点(节点#2 )添加到节点#1

enter image description here

但是当我旋转节点#1时,所有节点#2 都会下降。我无法将它们保留在节点#1中。

我正在使用Cocos2D-iPhone 3.0.0版

我的演示在这里:https://github.com/ditimtriky/DemoCocos2d

你能帮我解决一下吗?

==================

您可以快速查看https://plus.google.com/photos/105576008720153830534/albums/6020993675161389681/6020993675419257554

https://lh6.googleusercontent.com/-F3WIN7j8_nE/U47d2OI9CtI/AAAAAAAAGVI/eAzccfAPZds/w180-h240-no/1401870652451id172912.gif

=================

#import "MyScene.h"

@implementation MyScene {
    CCPhysicsNode *_physicsNode;
}

+ (instancetype)scene {
    return [[self alloc] init];
}

- (void)onEnter {
    [super onEnter];
    glClearColor(0.1, 0.1, 0.1, 1.0);

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"newton.plist"];

    _physicsNode = [CCPhysicsNode node];
    _physicsNode.gravity = ccp(0, 3*-980.665);
    _physicsNode.debugDraw = YES;
    [self addChild:_physicsNode];

    // create an outline of the basic physics node, to keep physics "in door"
    CCNode *outline = [CCNode node];
    outline.contentSize = CGSizeMake(280, 300);
    outline.anchorPoint = ccp(0.5, 0.5);
    outline.position = ccp(160, 284);

    outline.physicsBody = [CCPhysicsBody bodyWithPolylineFromRect:CGRectMake(0, 0, 280, 300) cornerRadius:3];
    outline.physicsBody.friction = 1.f;
    outline.physicsBody.elasticity = 0.5f;
    outline.physicsBody.collisionCategories = @[@"outline"];
    outline.physicsBody.collisionMask = @[@"sphere", @"rope"];
    [_physicsNode addChild:outline];

    for (int i = 0; i < 20; i++) {
        CCSprite *sprite = [CCSprite spriteWithImageNamed:@"letter.o.png"];
        sprite.anchorPoint = ccp(0.5,0.5);
        sprite.position = ccp(160, 284);

        CCPhysicsBody *body =
        [CCPhysicsBody bodyWithCircleOfRadius:25
                                    andCenter:ccp(sprite.contentSize.width/2,sprite.contentSize.height/2)];
        sprite.physicsBody = body;

        body.friction = 0.5;
        body.elasticity = 1.0;
        body.collisionCategories = @[@"sphere"];
        body.collisionMask = @[@"sphere", @"outline"];

        [_physicsNode addChild:sprite];
    }

    //rotate
    [outline runAction:
     [CCActionRepeatForever actionWithAction:
      [CCActionSequence actionWithArray:@[[CCActionRotateTo actionWithDuration:1 angle:180],
                                          [CCActionRotateTo actionWithDuration:1 angle:360]]]]];

    // add a reset button
    CCButton *resetButton = [CCButton buttonWithTitle:@"" spriteFrame:[CCSpriteFrame frameWithImageNamed:@"reset.png"]];
    resetButton.positionType = CCPositionTypeNormalized;
    resetButton.position = (CGPoint){0.90f, 0.80f};;
    [resetButton setTarget:self selector:@selector(onResetClicked:)];
    [self addChild:resetButton];
}

- (void)onResetClicked:(id)sender
{
    // recreate the scene
    [[CCDirector sharedDirector] replaceScene:[self.class scene]];
}

@end

2 个答案:

答案 0 :(得分:3)

问题是在3.0中将动作和物理混合在一起。该动作仅设定位置,使物理身体“传送”#34;随之而来。对于带有动作的静态物体,这在3.1中已得到修复。

如果你想留在3.0,你需要手动更新身体的角度和角速度。

答案 1 :(得分:1)

我看到两个可能的问题:

  1. 如果您使用的是v3.0,则不应混合操作(例如CCActionRotateTo)和物理。而不是动作只需施加力,冲动或设定角速度。
  2. 你的界限太薄了。包含的物体刚刚穿过它们。尝试使用4个矩形表示边界。
  3. 希望这有帮助。

    更新(回复评论):

    首先,您需要使用fixedUpdate:方法旋转轮廓体,而不是update:

    但是,最有可能的问题是边界太薄了。

    尝试像这样创建容器:

    //Thickness of bounds
    float thickness = 30.0f;
    
    //Bounds shapes using rects
    CCPhysicsShape *bottom = [CCPhysicsShape rectShape:CGRectMake(0,0,280,thickness) cornerRadius:0];
    CCPhysicsShape *right = [CCPhysicsShape rectShape:CGRectMake(280-thickness,0,thickness,300) cornerRadius:0];
    CCPhysicsShape *top = [CCPhysicsShape rectShape:CGRectMake(0, 300-thickness, 280, thickness) cornerRadius:0];
    CCPhysicsShape *left = [CCPhysicsShape rectShape:CGRectMake(0, 0, thickness, 300) cornerRadius:0];    
    
    //Creating body using shapes
    outline.physicsBody = [CCPhysicsBody bodyWithShapes:@[bottom, left, right, top]];
    
    //Body is static
    outline.physicsBody.type = CCPhysicsBodyTypeStatic;
    
    //..rest of your code..