objective-copenglcocos2d-iphoneccsprite

cocos2d - How to orient CCSprite in 3d?


I have a circular disc sprite that always rotates around its center, spinning like a wheel. I want to re-orient it in 3d space and keep its rotation. I've tried messing with the grid property, skewX/Y, the camera, everything I can find, but I can't figure out how to produce this effect with rotation.

example


Solution

  • HachiEthan's answer is good, and allows you to set the perspective with skewX/skewY while maintaining rotation. However, I think that it is easier to work with the camera for setting perspective. In that case, you can achieve the effect by wrapping your rotating sprite in a parent node.

    For example:

    CCNode *node3D;
    CCSprite *sprite;
    
    node3D = [CCNode node];
    sprite = [CCSprite spriteWithFile:@"HugeDisc_Red.png"];
    
    //[[node3D camera] setCenterX:15 centerY:45 centerZ:40]; // You can use the camera
    [node3D setSkewX:25]; // Skew also works if you prefer it
    [node3D setRotation:145]; // Skew + rotation looks pretty convincing
    [node3D setPosition:ccp(middleX+105, middleY-110)];
    
    [node3D setIgnoreAnchorPointForPosition:NO];
    [node3D setAnchorPoint:ccp(0.5f,0.5f)];
    
    [sprite setPosition:ccp(node3D.contentSize.width/2, node3D.contentSize.height/2)];
    [sprite runAction:rotate];
    
    [node3D addChild:sprite];
    

    Ultimately, it comes down to a matter of preference. Do you want to make a custom sprite object for this? Do you prefer camera, or skew? Depending on your needs, one of these should fit your needs.