【问题标题】:Setting SCNNode's rotation with valid values, but it's always 0,0,0,0使用有效值设置 SCNNode 的旋转,但始终为 0,0,0,0
【发布时间】:2015-04-18 15:04:04
【问题描述】:

我正在尝试设置我的相机节点的旋转,并且值在那里,但它永远不会从 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,并且示例代码运行良好)

【问题讨论】:

  • 我无法重现这一点。在创建节点和设置相机旋转之间发生了什么? (如果它们以不同的方法发生,它们是什么?)哪些节点有物理体,它们有哪些类型的物理体?
  • @rickster: 编辑包括节点创建和被调用的gestureRecognizer...
  • 如果不设置交易的持续时间会怎样? (或开始然后提交事务)

标签: ios objective-c rotation scenekit scnnode


【解决方案1】:

简单的答案?我是新手。

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

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

【讨论】:

    猜你喜欢
    • 2014-10-07
    • 1970-01-01
    • 1970-01-01
    • 2012-01-19
    • 2015-09-05
    • 1970-01-01
    • 2016-03-18
    • 2022-01-15
    • 2020-04-24
    相关资源
    最近更新 更多