Chris Loonam
Chris Loonam

Reputation: 5745

SCNSphere not rotating properly

I am making a game using SpriteKit, and in the main menu of that game is an SK3DNode that contains an SCNScene designed to contain a rotating planet. I set it up like this

//create scene
SCNScene *planetScene = [[SCNScene alloc] init];
SCNSphere *planet = [SCNSphere sphereWithRadius:2.0];
planet.firstMaterial.diffuse.contents = [UIImage imageNamed:@"Planet_2_d.png"];
SCNNode *plNode = [SCNNode nodeWithGeometry:planet];
[planetScene.rootNode addChildNode:plNode];

//animate planet
CABasicAnimation *rotationAnimation = [CABasicAnimation animationWithKeyPath:@"rotation"];
rotationAnimation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0, 1, 0, M_PI * 2)];
rotationAnimation.duration = 6; // One revolution in ten seconds.
rotationAnimation.repeatCount = FLT_MAX; // Repeat the animation forever.
[plNode addAnimation:rotationAnimation forKey:nil]; // Attach the animation to the node to start it.

//create and add sprite kit node
SK3DNode *planetNode = [[SK3DNode alloc] initWithViewportSize:CGSizeMake(125, 125)];
planetNode.scnScene = planetScene;
planetNode.position = CGPointMake(loadGameButton.position.x - 200, CGRectGetMidY(self.frame));
planetNode.autoenablesDefaultLighting = YES;
planetNode.playing = YES;

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

[self addChild:planetNode];

This works as planned, except for the fact that the planet does more than rotate. In addition to rotating, it also zooms in and out. I can't see anything in the above code that would cause it to behave this way. How can I get the planet to just rotate and not zoom?

Upvotes: 0

Views: 461

Answers (1)

Chris Loonam
Chris Loonam

Reputation: 5745

This problem was solved by adding a camera to the scene like this

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, 3);
[planetScene.rootNode addChildNode:cameraNode];

planetNode.pointOfView = cameraNode;

Upvotes: 1

Related Questions