objective c - Revolve a SCNode object in the 3D Space using SceneKit -
objective c - Revolve a SCNode object in the 3D Space using SceneKit -
i trying larn way around scenekit , example, thought i'd seek build solar scheme using sdk. able add together scnode objects , configure material properties pattern , lighting. also, able rotate planets, can't seem understand how "revolve" them along particular path.
my code far looks like:
// create new scene scnscene *scene = [scnscene scene]; // create , add together photographic camera scene scnnode *cameranode = [scnnode node]; cameranode.camera = [scncamera camera]; [scene.rootnode addchildnode:cameranode]; // place photographic camera cameranode.position = scnvector3make(0, 0, 2); // create , add together 3d sphere - sun scene scnnode *spherenode = [scnnode node]; spherenode.geometry = [scnsphere spherewithradius:0.3]; [scene.rootnode addchildnode:spherenode]; // create , configure material scnmaterial *material = [scnmaterial material]; material.diffuse.contents = [nsimage imagenamed:@"suntexture"]; material.specular.contents = [nscolor whitecolor]; material.specular.intensity = 0.2; material.locksambientwithdiffuse = yes; // set material 3d object geometry spherenode.geometry.firstmaterial = material; // create , add together 3d sphere - earth scene scnnode *earthnode = [scnnode node]; earthnode.geometry = [scnsphere spherewithradius:0.15]; nslog(@"sun position = %f, %f, %f", spherenode.position.x, spherenode.position.y, spherenode.position.z); earthnode.position = scnvector3make(0.7, 0.7, 0.7); nslog(@"earth position = %f, %f, %f", earthnode.position.x, earthnode.position.y, earthnode.position.z); //earthnode.physicsbody = [scnphysicsbody dynamicbody]; [scene.rootnode addchildnode:earthnode]; scnmaterial *earthmaterial = [scnmaterial material]; earthmaterial.diffuse.contents = [nsimage imagenamed:@"earthtexture"]; earthnode.geometry.firstmaterial = earthmaterial;
i read somewhere in appledocs need add together planets in sun's co-ordinate scheme , not in root node not sure if understood anyway.
let me know how doing this.
scenekit doesn't provide animate-along-path api. there several ways set solar system, though.
ever seen orrery? model bit in scnnode documentation talking about; it's wwdc presentation on "scene graph summary" slide. trick utilize 1 node represent frame of reference planet's revolution around sun — armature holds planet in orrery, if create node rotate around central axis, kid node attach follow circular path. relevant code in ascscenegraphsummary.m
in sample code project (condensed show node hierarchy setup here):
// sun _sunnode = [scnnode node]; _sunnode.position = scnvector3make(0, 30, 0); [self.contentnode addchildnode:_sunnode]; // earth-rotation (center of rotation of earth around sun) scnnode *earthrotationnode = [scnnode node]; [_sunnode addchildnode:earthrotationnode]; // earth-group (will contain earth, , moon) _earthgroupnode = [scnnode node]; _earthgroupnode.position = scnvector3make(15, 0, 0); [earthrotationnode addchildnode:_earthgroupnode]; // earth _earthnode = [_wireframeboxnode copy]; _earthnode.position = scnvector3make(0, 0, 0); [_earthgroupnode addchildnode:_earthnode]; // moon-rotation (center of rotation of moon around earth) scnnode *moonrotationnode = [scnnode node]; [_earthgroupnode addchildnode:moonrotationnode]; // moon _moonnode = [_wireframeboxnode copy]; _moonnode.position = scnvector3make(5, 0, 0); [moonrotationnode addchildnode:_moonnode];
you can't create animation follows specific path model in 3d space, create keyframe animation interpolates between several positions in 3d space. (untested) code below moves node around square shape in xz-plane... add together more points rounder shape.
cakeyframeanimation *anim = [cakeyframeanimation animationwithkeypaths:@"position"]; anim.values = @[ [nsvalue valuewithscnvector3:scnvector3make(0, 0, 1)], [nsvalue valuewithscnvector3:scnvector3make(1, 0, 0)], [nsvalue valuewithscnvector3:scnvector3make(0, 0, -1)], [nsvalue valuewithscnvector3:scnvector3make(-1, 0, 0)], ]; [node addanimation:anim forkey:"orbit"];
use physics! scnphysicsfield
class models radial gravity, can drop gravity field scene, add together physics bodies planets, , sit down , watch solar scheme cataclysmically destroy itself come life! getting realistic behavior here takes lot of trial , error — you'll need set initial velocity each planet tangential expect orbit be, , tweak velocity right. researching ephemeris info our solar scheme might help.
objective-c osx 3d scenekit
Comments
Post a Comment