iOS 3D engine SceneKit Development (4)--Some questions about rotation (1)

Source: Internet
Author: User

The rotation model is often encountered, we can rotate a view before using cabasicanimation, in fact it can also rotate a scnnode.

First we must understand a concept, each scnnode has its own three-dimensional coordinate system, with cabasicanimation is to let Scnnode around its three-dimensional axis rotation, so pay special attention to is the axis, not this scnnode geometric center. The coordinate system origin (0,0,0) of the several geometries of the general Scenekit is the center of its geometry, such as scnbox;scnsphere and so on, so it looks exactly like the rotation around the geometric center.

We start with the demo, here is the simulation of the Sun-Earth-Moon celestial Movement of the demo, the following points:

1, earth, moon rotation
2, the moon goes around the earth
3, the moon system goes around the sun

Such as:

1th to achieve the Earth, the moon rotation

    // Rotate the moon    animation = [CABasicAnimation animationWithKeyPath:@"rotation"];    animation.duration1.5;    animation.toValue = [NSValue valueWithSCNVector4:SCNVector4Make(0102)];    animation.repeatCount = FLT_MAX;    [_moonNode addAnimation:animation forKey:@"moon rotation"];

In fact, the Scenekit framework has its own animation API, we here the rotation of the earth with it, let everyone understand

[_earthNode runAction:[SCNAction repeatActionForever:[SCNAction rotateByX:0 y:2 z:0 duration:1]]];   //地球自转

As you can see from the code above, it is rotated around the y axis.

The 2nd Moon revolves around the Earth (father and son scnnode skills)

The moon needs to revolve around the Earth's Y axis, and we know that only the Earth rotates around its own Y axis. So we can let the earth rotate around its own y axis with the moon. It is simple to add the moon to the Earth:

[_earthNode addChildNode:_moonNode];

This is the result of the moon orbiting around the Earth.

In reality, however, the rotation of the earth is not the same as the lunar cycle. So the moon can not be added to the Earth on this scnnode, we have to re-create a scnnode, the location of the same as the Earth, the _moonnode added to the new Scnnode, and then rotate it.

_moonnode. Position= Scnvector3make (3,0,0);Moon-rotation (center of rotation of the Moon around the Earth) scnnode *moonrotationnode = [Scnnode node];[Moonrotationnode Addchildnode:_moonnode];Rotate the moon around the Earth cabasicanimation *moonrotationanimation = [Cabasicanimation animationwithkeypath:@"Rotation"];Moonrotationanimation. Duration=1.5;Moonrotationanimation. Tovalue= [Nsvalue Valuewithscnvector4:scnvector4make (0,1,0, M_PI *2)];Moonrotationanimation. RepeatCount= Flt_max;[Moonrotationnode addanimation:animation forkey:@"Moon rotation around Earth"];

The 3rd Land Moon system revolves around the sun

At this time the Earth and the moon go around the sun, and the lunar system can be regarded as a whole. We create a monthly system Earthgroupnode, and then add Moonrotationnode and Earthnode to it:

    [_earthGroupNode addChildNode:_earthNode];    [_earthGroupNode addChildNode:moonRotationNode];

The sun does not spin, similarly, creates a scnnode in the same position as the sun, allowing the Earth Moon system to earthgroupnode with this node

   //离太阳的距离  _earthGroupNode.position = SCNVector3Make(10, 0, 0);
Earth-rotation (center of rotation of the Earth around the Sun) scnnode *earthrotationnode = [Scnnode node];[_sunnode Addchildnode:earthrotationnode];Earth-group (would contain the Earth, andThe Moon) [Earthrotationnode Addchildnode:_earthgroupnode];Rotate the Earth around the Sun cabasicanimation *animation = [Cabasicanimation animationwithkeypath:@"Rotation"];Animation. Duration=10.0;Animation. Tovalue= [Nsvalue Valuewithscnvector4:scnvector4make (0,1,0, M_PI *2)];Animation. RepeatCount= Flt_max;[Earthrotationnode addanimation:animation forkey:@"Earth rotation around Sun"];

Then the general flow is over. Then we can add some animation to the Sun map, let him have the effect of lava, add some light effect, the whole is more real.

The overall effect is as follows:

Demo code has been uploaded to GitHub

Https://github.com/pzhtpf/SceneKitRoationDemo

On the continuous improvement of rotation ...

iOS 3D engine SceneKit Development (4)--Some questions about rotation (1)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.