简体   繁体   English

cocos2d-如何在3d中定向CCSprite?

[英]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. 我想在3d空间中重新定位它并保持其旋转。 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. 我试过将Grid属性,skewX / Y,相机以及我能找到的所有东西弄乱了,但我不知道如何旋转来产生这种效果。

例

A couple routes: 两条路线:

  • Figure out the maths to get the correct skew value for the rotation value. 找出数学公式,以获取旋转值的正确偏斜值。 (I don't know off of the top of my head, sorry, although you can probably reverse engineer it from option-2) (抱歉,我不知道该怎么办,尽管您可以从选项2进行反向工程)

  • You could sub-class CCSprite , override CCNode 's - (CGAffineTransform)nodeToParentTransform method and have to apply the skew first, THEN rotation, in order to get your desired effect. 您可以子类化CCSprite ,重写CCNode- (CGAffineTransform)nodeToParentTransform方法,并且必须先应用偏斜,然后旋转,才能获得所需的效果。 This spares you figuring out the proper skew calculation at the expense of a fairly kludgy sprite subclass. 这使您不必花大量的时间就可以计算出正确的偏斜计算,而不必花很多钱来计算精灵子类。

From CCNode.h you can learn its current order of operations: CCNode.h您可以了解其当前操作顺序:

 /* Order in transformations with grid disabled
 -# The node will be translated (position)
 -# The node will be rotated (rotation)
 -# The node will be skewed (skewX, skewY)
 -# The node will be scaled (scale, scaleX, scaleY)
 -# The node will be moved according to the camera values (camera)
 ... etc. */

Here's a hacked together version of that method with skew first: (lacks optimizations found in original method in CCNode.m ) 这是该方法的先破解后的版本:(缺少CCNode.m中原始方法中的CCNode.m

- (CGAffineTransform)nodeToParentTransform
{
    if ( isTransformDirty_ ) {
        // Translate values
        float x = position_.x;
        float y = position_.y;

        if ( ignoreAnchorPointForPosition_ ) {
            x += anchorPointInPoints_.x;
            y += anchorPointInPoints_.y;
        }

        transform_ = CGAffineTransformMake(1.0f, tanf(CC_DEGREES_TO_RADIANS(skewY_)),
                                           tanf(CC_DEGREES_TO_RADIANS(skewX_)), 1.0f,
                                           x, y );
        // Rotation values
        float c = 1, s = 0;
        if( rotation_ ) {
            float radians = -CC_DEGREES_TO_RADIANS(rotation_);
            c = cosf(radians);
            s = sinf(radians);
        }

        CGAffineTransform rotMatrix = CGAffineTransformMake( c * scaleX_,  s * scaleX_,
                                           -s * scaleY_, c * scaleY_,
                                          0.0f, 0.0f );
        transform_ = CGAffineTransformConcat(rotMatrix, transform_);

        // adjust anchor point
        if( ! CGPointEqualToPoint(anchorPointInPoints_, CGPointZero) )
            transform_ = CGAffineTransformTranslate(transform_, -anchorPointInPoints_.x, -anchorPointInPoints_.y);

        isTransformDirty_ = NO;
    }
    return transform_;
}

HachiEthan's answer is good, and allows you to set the perspective with skewX/skewY while maintaining rotation. HachiEthan的答案很好,它允许您在保持旋转的同时使用skewX / skewY设置透视图。 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? 是否要为此创建一个自定义的Sprite对象? Do you prefer camera, or skew? 您喜欢相机还是偏斜? Depending on your needs, one of these should fit your needs. 根据您的需求,其中之一应满足您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM