SpriteKit game development [5] Rotation & amp; Anchorpoint, spritekit Game Development

Source: Internet
Author: User
Tags spritekit

SpriteKit game development [5] Rotation & Anchorpoint, spritekit Game Development

Recently, I encountered some problems about the animation effect of rotation. I have a deeper understanding of the rotation in SpriteKit in the process of solving the problem. I would like to summarize it here.

I want to achieve the following effect: A pipe rotates around the center of the White Planet.


The most natural idea is to use SpriteKit directly.SKAction. rotateByAngleMethod.

So the question is, how can a SKSpriteNode element rotate around a specified vertex?

By default, each SKSpriteNode node has an Anchorpoint, And the rotateByAngle method rotates at the center of the Anchorpoint.

For example, when the anchor is in the center of the screen:


For example, when the anchor is in the lower left corner of the screen:



Set the Anchorpoint of the pipe to the center of the White Planet.(Modifying Anchorpoint is not a good solution)

However, the default Anchorpoint is (0.5, 0.5), which is the center point of the Water Pipe Element and represents the position relative to the ratio of SKSpriteNode.

Why can't Anchorpoint be directly set to a fixed pixel position? Isn't it simpler and easier to use?

Because the anchor is actually an "abstract point" of an element, and the position of an element is the position of the anchor, the abstract point is usually located inside the element.

When the rotateByAngle method is used for rotation, although the object rotates around the anchor, the anchor does not move, soThe Position of an object has never been changed. The change is caused by the error message:

Example:


In the figure, the blue border represents the collision shape calculated by the physical engine, the white border represents the frame of SKSpriteNode, and the center of the blue villain is the position of the car anchor.

Obviously, here the anchor of the car is (0, 0 ).

However, for the shape of a physical collision, its anchor does not change with the anchor of a car (that is, if the anchor of SKSpriteNode is modified, a deviation occurs when the physical engine is used, maybe this is a bug in the current version of SpriteKit ).

So, if you want to use the default physical engine for collision detection and apply force to the object, and want the object to rotate around the given point? What should we do?

That is to say, how can we rotate an object around a given point without modifying the Anchorpoint?

It's easy to manually modify the position and zrtation of an object.

Assume that the object is an element and the point is rotated by the rad radians per frame. You can call the following method for each update:

(For details about calculation of Point A's rotation around point B, see click to open the link)

Func rotateByPoint (element: SKNode, point: CGPoint, rad: CGFloat) {let tempPos = element. position let resultX = point. x + (tempPos. x-point. x) * cos (rad)-(tempPos. y-point. y) * sin (rad) let resultY = point. y + (tempPos. x-point. x) * sin (rad) + (tempPos. y-point. y) * cos (rad) // modify the angle element. zrtation + = rad // modify the position element. position. x = resultX element. position. y = resultY}


If it is simpler, the effect of rotating around a fixed point is only for good looks, and there is no interaction with other elements (or just roughly checks whether frames overlap)

You can consider modifying Anchorpoint for implementation.

The specific method is:

Add a Father's Day point and set its position to the position of the point to be rotated;

Set the object location to the new coordinate of the parent node;

Perform rotateByAngle on Father's Day.

In general, Anchorpoint exists to "fix" an object to a fixed position. If you want to make the object rotate around any point without affecting the use of the physical engine, you should consider modifying the position and zrtation of an object.

This scheme should be able to be written as an extended SKAction. However, I have no in-depth understanding of the SKAction mechanism and try to encapsulate it in the SKAction later.

[I have just started to get started with SpriteKit, but I still have a wealth of experience. If there are any errors, please make sure that they are correct.]


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.