Cocos2d-x V3.0 calls gravity accelerator Acceleration

Source: Internet
Author: User

I am in a good mood to solve a problem today;

Today's solution is the cocos2d-x 3.0 call the problem of gravity accelerator, online search a lot of information found are not good, can not solve the problem, I do not know if I am using 3.0 version of the problem, however, some tutorials on the Internet refer to version 3.0. Is there a big difference between the beta version and the official version? Potholes,

Let's talk about this rewrite method: many of the methods searched on the Internet are to override the didAccelerate (CCAcceleration * pAccelerationValue) of the parent class; method, but the 3.0 I used does not have this method at all, 3.0 of the search results are still mentioned, which is a pain point;

Finally, when looking at the official testCpp example, I found the method to call the gravity accelerator now; example location: X: \ your cocos2d-x \ tests \ cpp-tests \ Classes \ AccelerometerTest
Here, the method to be rewritten is onAcceleration (Acceleration * acc, Event * event). Is there any difference in TMD;

Every version of The cocos2d-x has changed? Ah, potholes ......

However, the engine is still good, so I can only say that I am not doing well;

Okay, too much nonsense. start to talk about things;


Now that we have found the correct method, we can solve it. First, we need to define the method in the. h header file.

# Include "cocos2d. h "class GameScene: public cocos2d: Layer {public: static cocos2d: Scene * createScene (); virtual bool init (); // override the parent class method, method void onAcceleration (cocos2d: Acceleration * acc, cocos2d: Event * event); CREATE_FUNC (GameScene );};


The next step is to implement this method in the cpp file.
// Rewrite the gravity accelerator method void GameScene: onAcceleration (Acceleration * acc, Event * event) {auto director = Director: getInstance (); if (img = NULL) {return;} auto imgSize = img-> getContentSize (); auto imgPosition = img-> getPosition (); auto imgX = img-> getPositionX (); auto imgY = img-> getPositionY (); imgX + = acc-> x * gravityValue; // imgY + = acc-> y * gravityValue; // auto imgTemp = director-> convertToUI (imgPosition); // imgTemp. x + = acc-> x * gravityValue; // imgTemp. y-= acc-> y * gravityValue; // auto imgNext = director-> convertToGL (imgTemp); FIX_POS (imgX, (origin. x + imgSize. width/2), (origin. x + visibleSize. width-imgSize. width/2); FIX_POS (imgY, (origin. y ++ imgSize. height/2), (origin. y + visibleSize. height-imgSize. height/2); img-> setPosition (Point (imgX, imgY); log ("onAcceleration: acc-> x: % f, acc-> y: % f, acc-> z: % f, imgX: % f, imgY: % f ", acc-> x, acc-> y, acc-> z, imgX, imgY );}




We can see part of the code I commented out. This is the method for converting the "screen Coordinate System" and "OpenGL Coordinate System" in the original official example file, and converting the coordinates in the example, set the genie coordinates.


In fact, according to the output log test, I found that the default getPosition () of the cocos2d-x gets the coordinates and OpenGL coordinates are always, so I finally commented out the conversion method in the example, set the sprite position based on the coordinate point, which is the same as the converted one;


Below I drew a few pictures to briefly describe the OpenGL coordinate system and the screen UI coordinate system (I just think that if something is wrong, I hope my friends will correct it)

First, this cube is the three-dimensional space coordinate system of the OpenGL gravity accelerator I have drawn. The blue color next to it indicates the correspondence between the mobile phone and the coordinate system when it is flat. According to the direction of the arrow in the figure, the values of x, y, and z axes tend to be-1;




The following two are the plane UI coordinate system and the GL coordinate system.

The coordinate origin of the UI coordinate system is in the upper left corner, and the Y axis is extended down. The position of genie A is (10, 10)



The origin of the GL coordinate system is in the lower left corner, and the Y axis is extended up. The position of genie B (equivalent to Genie A in the UI coordinate drama) is (10,128 0) (when the screen resolution is 720*1280)
This is why the UI coordinate system and GL coordinate system need to be converted in the official Test example.


The default Coordinate System in the cocos2d-x is the same as the coordinate system of OpenGL, So I directly obtained the location of the genie according to the getPosition () method, so that less complex conversion, however, since the official conversion may have their intent, you don't need to go into it now;

This blog is updated synchronously with my original blog: Melove I love
OK, continue with the base code.

Related Article

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.