1

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?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Rony
  • 1,229
  • 4
  • 22
  • 42

3 Answers3

0

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.

Doodloo
  • 869
  • 5
  • 18
0

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.

fith
  • 101
  • 5
0

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];
Robin
  • 10,011
  • 5
  • 49
  • 75