C ++ 11 code display: Simple Gesture Recognition and response, up-down left and right, 11 gestures

Source: Internet
Author: User

C ++ 11 code display: Simple Gesture Recognition and response, up-down left and right, 11 gestures

This is the code of the parkfun game, so it follows the normal logic to the left to the right. sliding up and down is supposed to perform the jump operation. The current scenario is static, so the logic is not pulling the camera before and after moving.


The lambda function implementation of C ++ 11 is used to compare operations in STL.

Although I know that there is only one camera with a 3D projection angle, the value of cameraMask found by the corresponding pRenderNode is camera3D-> setCameraFlag (CameraFlag: USER3, however, it will be very convenient to add camera for dual-camera operations in the future.


I plan to implement this parkour as a role model and camera model for Skip operations. The main scenario is only responsible for dynamic loading.

Camera operations are relatively simple for the time being, with a fixed shift per second.

The main problem is that there is a small problem with the role movement. When the role moves forward, a jump operation is added. At this time, animation fusion is required (ignore when the model is not supported ), the physical displacement track is a double action, and then it is switched back to a normal walking operation.


In gesture recognition, the default recognition mode is single-touch. The last hitting point is recorded first, and this gesture is called when the touch is determined to be in a non-cancel state, that is, the end state. Take the vector normal direction of two points, and perform the dot multiplication with the positive and right methods. The positive and negative signs can be used to confirm the bitwise upper and lower operations. Based on the left-right deviation to compare the threshold value, it is first recognized as the left-right sliding operation.


Void PlayerInputController: reveiveTouchBegin (Vec2 pos, Node * pRenderNode) {this-> _ touchBeginPos = std: move (pos);} void PlayerInputController: reveiveTouchEnd (Vec2 pos, node * pRenderNode) {this-> _ touchEndPos = std: move (pos); Vec2 diff = _ touchEndPos-_ touchBeginPos; diff. normalize (); auto temp = Vec2 (1, 0); // horizontal lineauto result = Vec2: dot (diff, temp); // steering jitter threshold value, the absolute amount of offset is greater than the threshold value to be regarded as the valid left and right movement const floa T Threshold = std: sqrt (2)/2; // If the moving direction and horizontal direction are in the 45 ° angle (-135 °, the point multiplication value is sqrt (2)/2, that is, the area of the parallelogram if (std: abs (result)> Threshold) {if (result> 0) // right {// it can be moved and is not executing the right-side move operation Gets an action from the running action list by its tag. if (_ pPlayer-> getCurPlayerSprite ()-> getPositionX () <= MIDDLE_LINE_POS_X &&! _ PPlayer-> getCurPlayerSprite ()-> getActionByTag (TURN_LEFT )&&! _ PPlayer-> getCurPlayerSprite ()-> getActionByTag (TURN_RIGHT) {auto action = MoveBy: create (0.2f, Vec3 (10, 0, 0 )); action-> setTag (TURN_RIGHT); this-> _ pPlayer-> getCurPlayerSprite ()-> runAction (action );}} else // left {// it can be moved and the left and right movement operation is not being performed. Gets an action from the running action list by its tag. if (_ pPlayer-> getCurPlayerSprite ()-> getPositionX ()> = MIDDLE_LINE_POS_X &&! _ PPlayer-> getCurPlayerSprite ()-> getActionByTag (TURN_LEFT )&&! _ PPlayer-> getCurPlayerSprite ()-> getActionByTag (TURN_RIGHT) {auto action = MoveBy: create (0.2f, Vec3 (-10, 0, 0 )); action-> setTag (TURN_LEFT); this-> _ pPlayer-> getCurPlayerSprite ()-> runAction (action );}}} else // up or down {# define MOVE_FORWARD (POS_X) \ auto cameraMask = _ pPlayer-> getCurPlayerSprite ()-> getCameraMask (); \ std :: vector <Camera *> cameras = pRenderNode-> getScene ()-> getCameras (); \ auto func = [camer AMask] (decltype (* cameras. begin () targetCamera)-> bool \ {\ return (unsigned short) targetCamera-> getCameraFlag () & cameraMask )! = 0 ;\};\\ auto it = std: find_if (cameras. begin (), cameras. end (), func); \ while (it! = Cameras. end () \ {\ auto temp = (* it)-> getPosition3D (); \ temp. add (Vec3 (0, 0, POS_X); \ (* it)-> setPosition3D (temp); \ std: find_if (++ it, cameras. end (), func); \} if (diff. y> 0) {MOVE_FORWARD (-5);} else {MOVE_FORWARD (5 );}}}


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.