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 ...
随机推荐
- 路由器WDS桥接教程
因为有吧友买了此款路由又不会桥接,因此做这个教程.老鸟自动路过,废话不多说,下面开始讲解. 1.wifi密码破解和路由器用户名和密码部分请自行解决,我只讲桥接部分.首先,在浏览器里输入192.168. ...
- win8.1(64位) apache2.4.3+php5.6.3+mysql5.6安装
win8.1(64位) apache2.4.3+php5.6.3+mysql5.6安装 http://blog.csdn.net/jiangzeyun/article/details/41676639
- css之marquee,让你的文字跳起来
当你看到别人的网页文字动态效果美美哒,而你却为不会使用js而遗憾时,不妨看看这篇文章,教你如何只用css即可实现漂亮的文字滑动效果. 1.问题提出: 在一个特定大小的div中,如何让p标签内的内容动态 ...
- head,tail,cat,more,less
tail FILE -n 4,查看文件最后4行内容head FILE -n 10,查看文件最前4行内容 使用cat more less都可以查看文本内容,但是它们三者有什么区别呢?more和less的 ...
- BZOJ1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富
1668: [Usaco2006 Oct]Cow Pie Treasures 馅饼里的财富 Time Limit: 3 Sec Memory Limit: 64 MBSubmit: 459 Sol ...
- [LeetCode] Maximum Gap 解题思路
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- 再看C
1. clrscr(void) 清屏 clear screen;gotoxy(x,y); 移动光标至指定位置;
- Apache Hadoop最佳实践和反模式
摘要:本文介绍了在Apache Hadoop上运行应用程序的最佳实践,实际上,我们引入了网格模式(Grid Pattern)的概念,它和设计模式类似,它代表运行在网格(Grid)上的应用程序的可复用解 ...
- Jetty监控线程使用情况的配置
Jetty监控线程使用情况配置 第一步,配置xml文件 jetty-monitor.xml 参数说明: threads: 线程池中的线程 busyThreads: 使用中的线程 idleThreads ...
- springBoot学习
http://blog.csdn.net/xiaoyu411502/article/details/47864969 博客: http://blog.csdn.net/xiaoyu411502/art ...