Cocos2d-x Learning notes in the world coordinate system, local coordinate system, OpenGL coordinate system, screen coordinate system _c language

Source: Internet
Author: User
Tags addchild

The coordinate system of cocos2d-x is very important, it is very important to understand the engine well. Note written very clearly, control the results of the operation to see the code bar!

BOOL Helloworld::init () {bool BRet = false; do {cc_break_if (!
		Cclayer::init ()); OpenGL has the same coordinate system as the world coordinate system, in the lower left corner of the screen as the origin, to the right for the x axis of the increase in the direction of the Y axis increased direction//the world refers to the game world//The screen coordinate system is the upper left corner as the origin, is our familiar//local coordinate system also called the node coordinate system The world coordinate system is relative to the whole, and the local coordinate system is relative to the parent node, it is important to understand this//ccdirector::shareddirector ()->converttogl () to the OpenGL coordinate system// Ccdirector::shareddirector ()->converttoui () to the screen coordinate system//ccnode::converttoworldspace () converts the local coordinate system to the world coordinate system//ccnode: Converttonodespace () Converts the world coordinate system to the local coordinate system//sprite1, sprite2 picture size is 100,100 ccsprite * sprite1 = ccsprite::create ("Image1.png"
		);

		Sprite1->setposition (CCP (100,100)); Add Sprite2 to the Sprite1 node, at which point the lower-left corner of the sprite1 is the origin, although the sprite1 anchor is in the middle of its picture ccsprite * Sprite2 = ccsprite::create ("Image2.png"
		);
		The coordinates set in SetPosition are Sprite2 's local coordinates sprite2->setposition (CCP (100,100));

		Sprite1->addchild (SPRITE2);
		The coordinates obtained in the getPosition are also sprite2 local coordinates ccpoint point = Sprite2->getposition ();

		Cclog ("point.x=%f,point.y=%f", POINT.X,POINT.Y); Get sprite2 OpenGL coordinate system, screen coordinate system, worldIn the coordinate system, the incoming parameter of the local coordinate system//converttoworldspace is the Sprite2 coordinate system (local coordinates), which is the parameter passed in the SetPosition. The incoming parameter of the converttonodespace is the sprite2 world coordinate system, see who is the caller, what the incoming arguments are ccpoint WorldSpace = Sprite1->converttoworldspace (
		Point);

		Ccpoint nodespace = Sprite1->converttonodespace (WorldSpace); The following two functions accomplish the same function, except that the coordinates of the Sprite2 are based on the sprite1 anchor instead of the lower left corner, so we passed the CCP (50,50) ccpoint worldspacear = sprite1->
		Converttoworldspacear (CCP (50,50));

		Ccpoint nodespacear = Sprite1->converttonodespacear (worldspacear); In Converttoui, the OpenGL coordinate system is passed in, because the OpenGL coordinates are the same as the world coordinate system, so we pass in the world coordinates ccpoint UI = Ccdirector::shareddirector ()->
		Converttoui (WorldSpace);

		Ccpoint OpenGL = Ccdirector::shareddirector ()->converttogl (UI);
		Cclog ("opengl.x=%f,opengl.y=%f", opengl.x,opengl.y);
		Cclog ("ui.x=%f,ui.y=%f", UI.X,UI.Y);
		Cclog ("worldspace.x=%f,worldspace.y=%f", worldspace.x,worldspace.y);
		Cclog ("nodespace.x=%f,nodespace.y=%f", nodespace.x,nodespace.y);
	Cclog ("worldspacear.x=%f,worldspacear.y=%f", worldspacear.x,worldspacear.y);	Cclog ("nodespacear.x=%f,nodespacear.y=%f", nodespacear.x,nodespacear.y);

    This->addchild (SPRITE1);
  BRet = true;

  while (0);
return bRet;
 }

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.