Cocos2d-x mouse or finger move Rigid Body

Source: Internet
Author: User

[Cpp]/********************************** * ********************************** // * QueryCallback *//************************************* * *********************************/class QueryCallback: public b2QueryCallback {public: QueryCallback (const b2Vec2 & point) {m_point = point; m_fixture = NULL;} bool ReportFixture (b2Fixture * fixture) {b2Body * body = fixture-> GetBody (); if (body-> GetType () = B2_dynamicBody) {bool inside = fixture-> TestPoint (m_point); if (inside) {m_fixture = fixture; // We are done, terminate the query. return false;} // Continue the query. return true;} b2Vec2 m_point; b2Fixture * m_fixture ;}; /*************************************** ******************************** // QueryCallback *// **************************************** ********************************/ Class QueryCallback: public b2QueryCallback {public: QueryCallback (const b2Vec2 & point) {m_point = point; m_fixture = NULL;} bool ReportFixture (b2Fixture * fixture) {b2Body * body = fixture-> GetBody (); if (body-> GetType () = b2_dynamicBody) {bool inside = fixture-> TestPoint (m_point); if (inside) {m_fixture = fixture; // We are done, terminate the query. return false;} // Continue the query. return true;} b2V Ec2 m_point; b2Fixture * m_fixture;}; [cpp] bool HelloWorld: MouseDown (const b2Vec2 & p) {if (m_mouseJoint! = NULL) {return false;} // Make a small box. b2AABB aabb; b2Vec2 d; d. set (0.001f, 0.001f); aabb. lowerBound = p-d; aabb. upperBound = p + d; // Query the world for overlapping shapes. queryCallback callback (p); world-> QueryAABB (& callback, aabb); if (callback. m_fixture) {b2Body * body = callback. m_fixture-> GetBody (); b2MouseJointDef md; md. bodyA = m_groundBody; md. bodyB = body; md.tar get = p; md. m AxForce = 1000.0f * body-> GetMass (); m_mouseJoint = (b2MouseJoint *) world-> CreateJoint (& md); body-> SetAwake (true); return true ;}} bool HelloWorld: MouseDown (const b2Vec2 & p) {if (m_mouseJoint! = NULL) {return false;} // Make a small box. b2AABB aabb; b2Vec2 d; d. set (0.001f, 0.001f); aabb. lowerBound = p-d; aabb. upperBound = p + d; // Query the world for overlapping shapes. queryCallback callback (p); world-> QueryAABB (& callback, aabb); if (callback. m_fixture) {b2Body * body = callback. m_fixture-> GetBody (); b2MouseJointDef md; md. bodyA = m_groundBody; md. bodyB = bodymongomd.tar get = p; md. maxForce = 1000.0f * body-> GetMass (); m_mouseJoint = (b2MouseJoint *) world-> CreateJoint (& md); body-> SetAwake (true); return true ;}} [cpp] void HelloWorld: MouseMove (const b2Vec2 & p) {// m_mouseWorld = p; if (m_mouseJoint) {m_mouseJoint-> SetTarget (p) ;}} void HelloWorld :: mouseMove (const b2Vec2 & p) {// m_mouseWorld = p; if (m_mouseJoint) {m_mouseJoint-> SetTarget (p) ;}} [cpp] void HelloWorld :: mouseUp (const b2Vec2 & p) {if (m_mouseJoint) {world-> DestroyJoint (m_mouseJoint); m_mouseJoint = NULL;} void HelloWorld: MouseUp (const b2Vec2 & p) {if (m_mouseJoint) {world-> DestroyJoint (m_mouseJoint); m_mouseJoint = NULL ;}} [cpp]/* To test moving the whole world */virtual bool MouseDown (const b2Vec2 & p); virtual void MouseUp (const b2Vec2 & p ); void MouseMove (const b2Vec2 & p); // b2Vec2 m_mouseWorld; b2MouseJoint * m_mouseJoint; b2Body * m_groundBody; // empty b2body, to set A/* rigid body for the mouse joint, use it to test moving the whole world */virtual bool MouseDown (const b2Vec2 & p); virtual void MouseUp (const b2Vec2 & p ); void MouseMove (const b2Vec2 & p); // b2Vec2 m_mouseWorld; b2MouseJoint * m_mouseJoint; b2Body * m_groundBody; // empty b2body, to set A [cpp] bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {isStart = pTouch-> getLocation (); CCPoint nodePosition = convertToNodeSpace (isStart); // PTM_RATIO = 32 return MouseDown (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO);} bool HelloWorld: ccTouchBegan (CCTouch * pTouch, CCEvent * pEvent) {isStart = pTouch-> getLocation (); CCPoint nodePosition = convertToNodeSpace (isStart ); // PTM_RATIO = 32 return MouseDown (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO);} [cpp] void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCPoint start = pTouch-> getLocation (); // get the coordinate of the touch point in openGL CCPoint nodePosition = convertToNodeSpace (start); this-> MouseMove (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO);} void HelloWorld: ccTouchMoved (CCTouch * pTouch, CCEvent * pEvent) {CCPoint start = pTouch-> getLocation (); // get the coordinate of the touch point in openGLCCPoint nodePosition = convertToNodeSpace (start); this-> MouseMove (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO);} [cpp] [cpp] void HelloWorld: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {CCPoint nodePosition = convertToNodeSpace (pTouch-> getLocation (); this-> MouseUp (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO);} void HelloWorld: ccTouchEnded (CCTouch * pTouch, CCEvent * pEvent) {CCPoint nodePosition = convertToNodeSpace (pTouch-> getLocation ()); this-> MouseUp (b2Vec2 (nodePosition. x/PTM_RATIO, nodePosition. y/PTM_RATIO ));}

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.