Coordinate System of [cocos2dx Game Development] (2) convertToNodeSpace and convertToWorldSpace

Source: Internet
Author: User

Some changes are often used in games:

Weapons and roles in the game are on the same layer. for efficiency, bullet, effect, and PhysicsParticle are placed on the unused layer, and the corresponding layer uses batchnode to improve efficiency.

The weapon is bound to PhysicsParticleLauncher (particle TRANSMITTER). During the launch, a physical particle effect will be generated to the same position of PhysicsParticleLayer's weapon.

 

ConvertToNodeSpace and convertToWorldSpace are often used to convert coordinates.

We add three sprite, r0, r1, r2,

The parent node of r0 is layer, and the coordinates of the parent node are (100,100)

The parent node of r1 is r0, and the coordinates of the parent node are (100,100)

The parent node of r2 is r1, and the coordinates of the parent node are)

 

 

Bool HelloWorld: init () {// 1. super init firstif (! Layer: init () {return false;} // Add r0 to the root node (Layer) Coordinate (0, 0) Sprite * r0 = Sprite :: create ("data/pic/gun.png"); r0-> setAnchorPoint (Point (0.f, 0.f)); this-> addChild (r0 ); // r1 joins Sprite * r1 = Sprite: create ("data/pic/gun.png"); r1-> setAnchorPoint (Point (0.f, 0.f )); r1-> setPosition (Point (100, 0); r0-> addChild (r1); // r2 is added to Sprite * r2 = Sprite :: create ("data/pic/gun.png"); r2-> setAnchorPoint (Point (0.f, 0.f)); r2-> setPosition (Point (100, 20 )); r1-> addChild (r2 );}

 

How do I convert the coordinates of r2 to the world coordinates?

How do I convert the coordinates of r2 to the local coordinates of r0?

 

// Obtain the world coordinates of r2. Two methods are available. // Method 1: Start With the r2 coordinate system and convert it to the root node (World coordinates). The coordinates must be (0, 0) point p2world = r2-> convertToWorldSpace (Point (0, 0); // Method 2: Start from the r1 coordinate system and convert it to the root node (World coordinate, the coordinate is the coordinate p2world = r1-> convertToWorldSpace (r2-> getPosition () of r2 in the parent node r1 ()); // obtain the coordinates of r2 in the r0 coordinate system // the coordinates to be passed in by convertToNodeSpace are the world coordinates Point pt = r0-> convertToNodeSpace (p2world );


 

The method is as follows:

For how it works, see cocos2dx code.

 

Point Node::convertToWorldSpace(const Point& nodePoint) const{    kmMat4 tmp = getNodeToWorldTransform();    kmVec3 vec3 = {nodePoint.x, nodePoint.y, 0};    kmVec3 ret;    kmVec3Transform(&ret, &vec3, &tmp);    return Point(ret.x, ret.y);}kmMat4 Node::getNodeToWorldTransform() const{    kmMat4 t = this->getNodeToParentTransform();    for (Node *p = _parent; p != nullptr; p = p->getParent())        kmMat4Multiply(&t, &p->getNodeToParentTransform(), &t);    return t;}


 

Cocos2d-x coordinate system details: http://www.tairan.com/archives/3367

 

Cocos2dx game development coordinate system (1) Click here

 

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.