17--Box2D使用(三、触摸交互)
Box2D引擎与触摸的交互通过创建鼠标关节以及碰撞检测来得到触摸点下面的刚体,在根据触摸操作完成相应的功能。首先添加触摸响应函数声明
virtual void ccTouchesBegan(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesMoved(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
virtual void ccTouchesEnded(cocos2d::CCSet* touches, cocos2d::CCEvent* event);
声明一个鼠标关节变量
b2World* world;
b2Body* wallBody ; float32 wallLineOffset ; b2MouseJoint* m_MouseJoint; //鼠标关节 GLESDebugDraw* m_debugDraw; //调试物理世界绘制对象
在init中打开触摸功能
m_MouseJoint = NULL;
this->setTouchEnabled(true);
创建一个碰撞查询回调类
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;
};
实现触摸函数
void HelloWorld::ccTouchesBegan(CCSet* touches, CCEvent* event){
CCSetIterator it;
CCTouch* touch;
for( it = touches->begin(); it != touches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->getLocation();
if (m_MouseJoint != NULL)
return ;
// 根据触摸点创建一个很小的碰撞检测矩形
b2AABB aabb;
b2Vec2 d;
b2Vec2 p = b2Vec2(location.x/PIXEL_TO_METER,location.y/PIXEL_TO_METER);
d.Set(0.001f, 0.001f);
aabb.lowerBound = p - d;
aabb.upperBound = p + d;
// 查询物理世界中的碰撞的刚体,回调对象(QueryCallback)中已经过滤的非动态物体
QueryCallback callback(p);
world->QueryAABB(&callback, aabb);
if (callback.m_fixture)
{
b2Body* body = callback.m_fixture->GetBody();
//为获取到的刚体创建一个鼠标关节
b2MouseJointDef md;
md.bodyA = wallBody;
md.bodyB = body;
md.target = p;
md.maxForce = 1000.0f * body->GetMass();
m_MouseJoint = (b2MouseJoint*)world->CreateJoint(&md);
body->SetAwake(true); //唤醒刚体
}
}
}
void HelloWorld::ccTouchesMoved(CCSet* touches, CCEvent* event){
CCSetIterator it;
CCTouch* touch;
for( it = touches->begin(); it != touches->end(); it++)
{
touch = (CCTouch*)(*it);
if(!touch)
break;
CCPoint location = touch->getLocation();
if (m_MouseJoint)
{
//更新刚体的位置
m_MouseJoint->SetTarget(b2Vec2(location.x/PIXEL_TO_METER,location.y/PIXEL_TO_METER));
}
}
}
void HelloWorld::ccTouchesEnded(CCSet* touches, CCEvent* event)
{
if (m_MouseJoint)
{
//触摸结算销毁鼠标关节
world->DestroyJoint(m_MouseJoint);
m_MouseJoint = NULL;
}
}
最后看看运行效果

17--Box2D使用(三、触摸交互)的更多相关文章
- Win8交互UX——用于 Windows 的触摸交互
用于 Windows 的触摸交互 Windows 8.1 提供一组在整个系统中使用的简单触摸交互功能.一致地应用此触摸语言可让用户对你的应用感觉已经很熟悉.通过让你的应用更容易学习和使用,可提高用 ...
- Legolas工业自动化平台入门(三)交互事件响应动作
在上一篇Legolas工业自动化平台入门(二)数据响应动作 一文中,我们介绍了"动作"相关内容,了解到"动作"分为多种,各种动作的添加方式相同,但是应用方式各自 ...
- ECharts.js学习(三)交互组件
ECharts.js 交互组件 ECharts.js有很多的交互组件,一般经常用到的组件有这些: title:标题组件,包含主标题和副标题. legend:图例组件,展现了不同系列的标记(symbol ...
- __x__(17)0906第三天__块元素block_内联元素inline_行内块元素inline-block
1. 块元素block 独占一行的元素 一般使用块元素包含内联元素,用作页面布局 <a> 标签可以包含任何除了a标签以外的元素 <p> 标签不能包含块元素 h1... ...h ...
- Win8交互UX——触摸板交互
针对触摸输入优化 Window 应用商店应用设计,并在默认情况下获得触摸板支持. 设计用户可以通过触摸板交互的 Windows 应用商店应用. 触摸板结合间接的多点触控输入和指针设备(如鼠标)的精确输 ...
- 【转载】Quick 中的触摸事件
原文地址 http://cn.cocos2d-x.org/article/index?type=quick_doc&url=/doc/cocos-docs-master/manual/fram ...
- [转]starling教程-触摸事件(Touch Events)(四)
在前面提到过,Starling是Sparrow的姊妹篇,正因为这样,Starling里的touch事件的机制其实是为移动设备的触摸交互设计的,所以当你使用它进行使用鼠标交互的桌面应用开发时,第一眼会感 ...
- 在子线程中使用runloop,正确操作NSTimer计时的注意点 三种可选方法
一直想写一篇关于runloop学习有所得的文章,总是没有很好的例子.游戏中有一个计时功能在主线程中调用: 1 + (NSTimer *)scheduledTimerWithTimeInterval:( ...
- iphone的手势与触摸编程学习笔记
一.基本概念与理解:Cocoa Touch将触摸事件发送到正在处理的视图.触摸传达的信息包括: 触摸发生的位置 (当前位置.最近的历史位置) 触摸的阶段 (按下.移动.弹起) 轻击数量 (tapCou ...
随机推荐
- java.io.FileNotFoundException: /exapp/hadoop/name/current/VERSION (Permission denied)
http://blog.csdn.net/blackiez/article/details/8570395
- 专门讲讲这个MYSQL授权当中的with grant option的作用
对象的owner将权限赋予某个用户(如:testuser1) grant select ,update on bd_corp to testuser1 [with grant option ]1.如果 ...
- Sum square difference
简单: e sum of the squares of the first ten natural numbers is, 12 + 22 + ... + 102 = 385 The square o ...
- 调试单片机内部扩展RAM
51单片机内部只有128字节的RAM(8051),而8052有256字节的RAM,低128字节RAM直接或间接寻址都可以,高128字节RAM与SRF特殊功能寄存器共用地址,SFR只能直接寻址,高128 ...
- Delphi从内存流中判断图片格式(好多相关文章)
废话不多说了,利用内存流来判断文件的格式,其实判断文件的前几个字节就可以简单的判断这个文件是什么类型的文件,例如jpg文件 是 FFD8 (从低位到高位就要反过来 D8FF 下面都是一样)BMP文件 ...
- 警惕P2B模式
大家都知道P2P是什么,估计也有很多人了解P2B的意思,这里也不多做解释,但是为什么要警惕P2B,这里我要做详细说明,希望能给大家一个参考. 首先我们要把P2B分成两种,一种是针对大型企业, ...
- linux内核链表的移植与使用
一. Linux内核链表为双向循环链表,和数据结构中所学链表类似,具体不再细讲.由于在内核中所实现的函数十分经典,所以移植出来方便后期应用程序中的使用. /********************* ...
- 动态规划 DP
10.1.5.253 1143 数字金字塔#include <iostream> #include<string.h> using namespace std; int a[1 ...
- SQL-Duplicate Emails
思路: 两种思路,可以给当成两个表也可以给当成一张表来做 group by就是按照某个属性给表分个组,然后having语句的含义是“对于每一个group”怎么怎么样 补充知识:count(column ...
- POJ-2386(深广搜基础)
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 25322 Accepted: 12759 D ...