SK3DNode not rotating

时间:2015-09-01 22:52:03

标签: ios sprite-kit scenekit

I have a 3D sphere model saved as a dae file and loaded to xcode.

Problem is I cannot get it spinning/rotating - it just stays static... I've tried several ways, even tried to get the camera to move around it.

SCNScene *model = [SCNScene sceneNamed:@"model3.dae"];
SK3DNode *node = [[SK3DNode alloc] initWithViewportSize:CGSizeMake(50, 50)];

//workaround
id s1 = [node valueForKey:@"_scnRenderer"];
NSLog(@"%@", s1);

node.physicsBody = [SKPhysicsBody bodyWithCircleOfRadius:node.frame.size.width/2];

node.physicsBody.affectedByGravity = NO;
node.physicsBody.allowsRotation = YES;

node.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame));
[node setScnScene:model];

//THIS IS THE PART! I cannot get it to spin around
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"rotation"];
rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(1, 2, 3, M_PI * 2)];
rotationAnimation.duration = 1;
rotationAnimation.repeatCount = FLT_MAX;
[model.rootNode addAnimation:rotationAnimation forKey:nil];

//camera
SCNCamera *camera = [SCNCamera camera];
camera.xFov = 0;
camera.yFov = 0;
camera.zNear = 0.0;
camera.zFar = 10.0;
SCNNode *cameraNode = [SCNNode node];
cameraNode.camera = camera;
cameraNode.position = SCNVector3Make(0, 0, 5);
[model.rootNode addChildNode:cameraNode];

node.pointOfView = cameraNode;
[world addChild:node];

Any ideas? Basically I want to try get the 3D model moving around inside the SK3DNode. Hope this makes sense. Think spinning planet.

Thanks in advance, Alex

1 个答案:

答案 0 :(得分:2)

修改场景的根节点的变换(例如通过添加动画)没有效果,因为摄像机由该根节点的子节点保持。球体和相机之间的相对变换保持不变。您应该将动画添加到相机节点或球体节点。