使用有效值设置SCNNode的旋转,但它始终为0,0,0,0

时间:2015-02-17 19:31:12

标签: ios objective-c rotation scenekit scnnode

我试图设置我的相机节点的旋转,并且值已经存在,但它永远不会从0,0,0,0变化... 初始化播放器节点(省略其他设置,节点没有几何体,但其物理体确实有几何体)

playerNode = [SCNNode node];

然后设置它的位置并将其添加到场景的根节点......

- (void)viewDidLoad
{
    [super viewDidLoad];

    // create a new scene
    SCNScene *scene = [SCNScene scene];
    scene.physicsWorld.gravity = SCNVector3Make(0, -9, 0);
    scene.physicsWorld.timeStep = 1.0/360;

    // add world node
    worldNode = [SCNNode node];
    worldNode.name = @"world";
    [scene.rootNode addChildNode:worldNode];

    // add terrain
    .../* terrain stuff */

    // add player node
    playerNode = [SCNNode node];
    playerNode.name = @"player";
    playerNode.position = SCNVector3Make(0, 0, 0);
    playerNode.physicsBody = [SCNPhysicsBody bodyWithType:SCNPhysicsBodyTypeDynamic shape:[SCNPhysicsShape shapeWithGeometry:[SCNCylinder cylinderWithRadius:0.2 height:1] options:nil]];
    playerNode.physicsBody.angularDamping = 0.9999;
    playerNode.physicsBody.damping = 0.9999;
    playerNode.physicsBody.rollingFriction = 0;
    playerNode.physicsBody.friction = 0;
    playerNode.physicsBody.restitution = 0;
    playerNode.physicsBody.velocityFactor = SCNVector3Make(1, 0, 1);
    playerNode.physicsBody.categoryBitMask = playerCategory;
    [scene.rootNode addChildNode:playerNode];

    // create and add a camera to the scene
    SCNNode *cameraNode = [SCNNode node];
    [playerNode addChildNode:cameraNode];

    cameraNode.camera = [SCNCamera camera];
    cameraNode.camera.xFov = 53;
    cameraNode.camera.zNear = 0.01;
    cameraNode.camera.zFar = 5000;
    // place the camera
    cameraNode.position = SCNVector3Make(0, 0, 0);
    .../* rest of view did load */
}

然后尝试设置旋转:

-(void)lookGestureRecognized:(UIPanGestureRecognizer *)gesture {
    SCNView *scnView = (SCNView *)self.view;
    CGPoint translation = [gesture translationInView:self.view];
    NSLog(@"lookGestureRecognized: translation x %g y %g", translation.x, translation.y);
    CGFloat hAngle = acos(((float)translation.x / 200) - (float)M_PI_2);
    CGFloat vAngle = acos(((float)translation.y / 200) - (float)M_PI_2);

    // rotate hero
    [playerNode.physicsBody applyTorque:SCNVector4Make(0, 1, 0, hAngle) impulse:YES];

    // tilt camera
    [SCNTransaction setAnimationDuration:0.0];
    elevation = MAX((float)-M_PI_4, MIN((float)M_PI_4, elevation + vAngle));
    NSLog(@"elevation: %g", elevation);
    SCNVector4 cameraRotation = SCNVector4Make(1, 0, 0, elevation);
    cameraNode.rotation = cameraRotation;
    // cameraNode.transform = SCNMatrix4Rotate(cameraNode.transform, 1, 0, 0, elevation); // tried this, didn't work either
    NSLog(@"cameraNode.rotation = x %g y %g z %g, w %g", cameraNode.rotation.x, cameraNode.rotation.y, cameraNode.rotation.z, cameraNode.rotation.w);

    // reset translation
    [gesture setTranslation:CGPointZero inView:self.view];
}

正确计算高程,但尝试将其设置为节点的旋转失败...日志总是显示0,0,0,0 ......

NSLog sample output:
2015-02-17 14:37:11.732 usingGestureRecognizer.01[96111:289778] lookGestureRecognized: translation x 0 y 0.5
2015-02-17 14:37:11.733 usingGestureRecognizer.01[96111:289778] elevation: -0.785398
2015-02-17 14:37:11.733 usingGestureRecognizer.01[96111:289778] cameraNode.rotation = x 0 y 0 z 0, w 0

有什么想法吗?

(旁注,模仿我从示例中找到的代码,将其从swift转换为obj-c,示例代码完美运行)

1 个答案:

答案 0 :(得分:1)

简单的答案?我是个新手。

实际答案?从教程中复制代码时要非常小心......我已经声明了一个私有变量SCCNode cameraNode,并且正在尝试更改此节点的旋转,但是,执行摄像头工作的实际摄像头节点被声明为私有方法viewDidLoad中的变量......

所以,再次,非常感谢你们的帮助,这个网站对我来说非常宝贵,我学到了很多东西,但我个人提出了2个问题,我的两个都是我的疏忽。再次感谢,真诚地