cocos3d动态创建3D盒子

发布于 2024-11-26 17:31:13 字数 1463 浏览 0 评论 0原文

我是cocos3d的新手,但我了解cocos2d。我想动态创建 3d 盒子。所以我在 cc3layer 中做了什么

-(void) initializeControls {

    [self schedule:@selector(create_box:) interval:2 ];

}

-(void)create_box:(id)sender{

    [self unschedule:@selector(mov_cel:)];
    [[testWorld sharedcontescWorld] world_create_box];
}

,在 cc3world 类中做了什么,

static testWorld *_sharedcontescWorld=nil;

+(testWorld *)sharedcontescWorld{
    @synchronized([testWorld class]){
        if (!_sharedcontescWorld)
            [self alloc];
        return _sharedcontescWorld;
    }return nil;
}
+(id)alloc{
    @synchronized([testWorld class])    {
        _sharedcontescWorld = [super alloc];
        return _sharedcontescWorld;
    }return nil;
}

    -(void) world_create_box{

    int minx=-50;
    int maxx=50;
    float posx=(float)(minx+arc4random()%maxx);

    CC3MeshNode* aNode;
    aNode = [CC3BoxNode nodeWithName: @"Simple box"];
    CC3BoundingBox bBox;
    bBox.minimum = cc3v(-10.0, -10.0, -10.0);
    bBox.maximum = cc3v( 10.0,  10.0,  10.0);
    [aNode populateAsSolidBox: bBox];
    [aNode setLocation:cc3v(posx,0,0)];
    aNode.material = [CC3Material material];
    [self addChild:aNode];
    id move3d=[CC3MoveTo actionWithDuration:1 moveTo:cc3v(posx,0,100)];
    id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeObj:)];
    [aNode runAction:[CCSequence actions:move3d,remove,nil]];

}

但它不起作用......任何人都可以帮助我吗?

I am new in cocos3d but i know cocos2d. i want to create 3d box dynamically. so what i did inside cc3layer is

-(void) initializeControls {

    [self schedule:@selector(create_box:) interval:2 ];

}

-(void)create_box:(id)sender{

    [self unschedule:@selector(mov_cel:)];
    [[testWorld sharedcontescWorld] world_create_box];
}

and in cc3world class is

static testWorld *_sharedcontescWorld=nil;

+(testWorld *)sharedcontescWorld{
    @synchronized([testWorld class]){
        if (!_sharedcontescWorld)
            [self alloc];
        return _sharedcontescWorld;
    }return nil;
}
+(id)alloc{
    @synchronized([testWorld class])    {
        _sharedcontescWorld = [super alloc];
        return _sharedcontescWorld;
    }return nil;
}

    -(void) world_create_box{

    int minx=-50;
    int maxx=50;
    float posx=(float)(minx+arc4random()%maxx);

    CC3MeshNode* aNode;
    aNode = [CC3BoxNode nodeWithName: @"Simple box"];
    CC3BoundingBox bBox;
    bBox.minimum = cc3v(-10.0, -10.0, -10.0);
    bBox.maximum = cc3v( 10.0,  10.0,  10.0);
    [aNode populateAsSolidBox: bBox];
    [aNode setLocation:cc3v(posx,0,0)];
    aNode.material = [CC3Material material];
    [self addChild:aNode];
    id move3d=[CC3MoveTo actionWithDuration:1 moveTo:cc3v(posx,0,100)];
    id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeObj:)];
    [aNode runAction:[CCSequence actions:move3d,remove,nil]];

}

but it doesn't work......can anyone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

舞袖。长 2024-12-03 17:31:13

您还准备好相机和灯光吗?

如果没有相机信息,很难说,但我认为你可能没有在看立方体(相机丢失或瞄准错误),或者立方体太大而你在里面。

尝试使用相机和灯光以及一些更适中的尺寸。

以下是在 cocos3d XCode 项目模板中对我有用的一些示例代码:

        // Create the camera, place it back a bit, and add it to the world
        CC3Camera* cam = [CC3Camera nodeWithName: @"Camera"];
        cam.location = cc3v( 0.0, 0.0, 10.0 );
        [self addChild: cam];

        // Create a light, place it back and to the left at a specific
        // position (not just directional lighting), and add it to the world
        CC3Light* lamp = [CC3Light nodeWithName: @"Lamp"];
        lamp.location = cc3v( -2.0, 0.0, 0.0 );
        lamp.isDirectionalOnly = NO;
        [cam addChild: lamp];


        float maxx=5.0;
        float posx=CCRANDOM_MINUS1_1()*maxx;

        CC3BoxNode* aNode;
        aNode = [CC3BoxNode nodeWithName: @"Simple box"];
        CC3BoundingBox bBox;
        bBox.minimum = cc3v(-1.0, -1.0, -1.0);
        bBox.maximum = cc3v( 1.0,  1.0,  1.0);
        [aNode populateAsSolidBox: bBox];
        [aNode setLocation:cc3v(posx,0.0f,-5.0f)];
        aNode.material = [CC3Material material];
        [self addChild:aNode];
        id move3d=[CC3MoveTo actionWithDuration:1.0f moveTo:cc3v(posx*-1.0,0.0f,-5.0f)];
//      id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeChild:)];
        [aNode runAction:[CCSequence actions:move3d,/*remove,*/nil]];

注意:

  • cocos3d 自述文件包含安装 XCode 项目模板的说明(如果您还没有安装的话)。

  • 我用这些东西替换了模板中的所有“Hello World”对象代码(注意它是相同的相机和灯光代码/注释);你可以把它留下,但你的盒子前面会有一些文字。

  • 为了清楚起见,将 CC3MeshNode 更改为 CC3BoxNode

  • 用 cocos2d 辅助函数替换了 arc4random()
    CCRANDOM_MINUS1_1() 只是为了便于阅读(并分享这个
    cocos2d gem)。

  • 注释掉了CCFuncCallND,因为我没有removeObj函数。希望你能做到。 ;)

希望有帮助。

Do you have a camera and light set up as well?

Without camera info it's a hard to tell, but I think you might either not be looking at the cube (camera missing or aimed wrong), or the cube is too big and you're inside it.

Try it with a camera and light, and some more modest sizes.

Here's some example code that worked for me in the cocos3d XCode project template:

        // Create the camera, place it back a bit, and add it to the world
        CC3Camera* cam = [CC3Camera nodeWithName: @"Camera"];
        cam.location = cc3v( 0.0, 0.0, 10.0 );
        [self addChild: cam];

        // Create a light, place it back and to the left at a specific
        // position (not just directional lighting), and add it to the world
        CC3Light* lamp = [CC3Light nodeWithName: @"Lamp"];
        lamp.location = cc3v( -2.0, 0.0, 0.0 );
        lamp.isDirectionalOnly = NO;
        [cam addChild: lamp];


        float maxx=5.0;
        float posx=CCRANDOM_MINUS1_1()*maxx;

        CC3BoxNode* aNode;
        aNode = [CC3BoxNode nodeWithName: @"Simple box"];
        CC3BoundingBox bBox;
        bBox.minimum = cc3v(-1.0, -1.0, -1.0);
        bBox.maximum = cc3v( 1.0,  1.0,  1.0);
        [aNode populateAsSolidBox: bBox];
        [aNode setLocation:cc3v(posx,0.0f,-5.0f)];
        aNode.material = [CC3Material material];
        [self addChild:aNode];
        id move3d=[CC3MoveTo actionWithDuration:1.0f moveTo:cc3v(posx*-1.0,0.0f,-5.0f)];
//      id remove=[CCCallFuncND actionWithTarget:self selector:@selector(removeChild:)];
        [aNode runAction:[CCSequence actions:move3d,/*remove,*/nil]];

Notes:

  • cocos3d readme has instructions to install the XCode project templates, if you haven't yet.

  • I replaced all the "Hello World" object code in the template with this stuff (note it's the same camera and light code/comments); you can leave it but you'll have some words in front of your box.

  • Changed CC3MeshNode to CC3BoxNode for clarity, but totally CC3MeshNode works too.

  • Replaced arc4random() with the cocos2d helper function
    CCRANDOM_MINUS1_1() just for ease of reading (and to share this
    cocos2d gem).

  • Commented out the CCFuncCallND because I didn't have a removeObj function. Hope you do. ;)

Hope that helps.

青衫负雪 2024-12-03 17:31:13

我还没有开始使用 cocos3d,但我在 stackoverflow 上找到了这段有效的代码

CC3BoundingBox bounds = makeBounds(9.5, 5.0, 4.0, 0, 0, 0);
CC3MeshNode *cube = [[CC3MeshNode alloc] init];
[cube populateAsSolidBox:bounds];

I have not started with cocos3d but I have found this code on stackoverflow that works

CC3BoundingBox bounds = makeBounds(9.5, 5.0, 4.0, 0, 0, 0);
CC3MeshNode *cube = [[CC3MeshNode alloc] init];
[cube populateAsSolidBox:bounds];
梦里寻她 2024-12-03 17:31:13

有一个 CC3BoxNode 类可以帮助您做到这一点。例如:

        CC3BoxNode      *box        = [CC3BoxNode nodeWithName:@"MyBox"];
        [box populateAsSolidBox:CC3BoxMake(-1, -1, -1, 1, 1, 1)];
        [self addChild:box];

这将创建一个 2x2x2 的盒子,其原点位于其绝对中心。
希望有帮助。

There is a CC3BoxNode class to help you do that. Eg:

        CC3BoxNode      *box        = [CC3BoxNode nodeWithName:@"MyBox"];
        [box populateAsSolidBox:CC3BoxMake(-1, -1, -1, 1, 1, 1)];
        [self addChild:box];

This would create a 2x2x2 box with its origin point in its absolute center.
Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文