PhysicsEditor with cocos2d and box2d usecase

本文介绍如何使用 PhysicsEditor 工具配合 Cocos2d 和 Box2d 物理引擎创建游戏。教程涵盖从设置 Box2d 世界到更新精灵位置的全过程,并提供了一个完整的演示项目。

转载自:http://www.physicseditor.de/cocos2d-box2d/

Tutorial: cocos2d + box2d

PhysicsEditor with cocos2d and box2d

This is a short tutorial how to use PhysicsEditor together with cocos2d and box2d as physics engine. The project is bases on the box2d template project which comes with cocos2d. The project is complete – it contains cocos2d and everything you need for a quick start.

Structure:

  • Classes – example classes to for the demo
  • libs – cocos2d, box2d and other sources
  • PhysicsEditorSources – the plist loader for box2d
  • Resources – the sprites

The main folder contains the shapedefs.pes file which is the file for PhysicsEditor. You can open this to experiment with parameters and add new sprites to the project.

Setting up box2d

First you need to create the b2World object which runs the complete simulation:

// Define the gravity vector.
b2Vec2 gravity;
gravity.Set (0.0f,  -10.0f );

// Do we want to let bodies sleep?
// This will speed up the physics simulation
bool doSleep  =  true;

// Construct a world object, which will hold
// and simulate the rigid bodies.
world  = new b2World (gravity, doSleep );
world ->SetContinuousPhysics ( true );

In the demo I also add some floor shapes which are created programatically (not shown here).

Preparing the GB2ShapeCache

Next we need to set up the GB2ShapeCache and load the plist file with the shapes created with PhysicsEditor.

[ [GB2ShapeCache sharedShapeCache ]
addShapesWithFile : @ "shapedefs.plist" ];

Make sure the plist file is in your resources.

Create a CCSprite and attach it to a b2Body

With that done we can create a CCSprite and add it to the current scene:

CCSprite  *sprite  =  [CCSprite spriteWithFile : @ "object.png" ];
[self addChild :sprite ];

(I did not use CCBatchNode in this example because I wanted to keep the demo simple and independent from <a href="http://www.texturepacker.com/" "="" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; outline-width: 0px; outline-style: initial; outline-color: initial; font-size: 14px; vertical-align: baseline; background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: transparent; color: rgb(131, 180, 65); text-decoration: none; background-position: initial initial; background-repeat: initial initial; ">TexturePacker.)

Next is to create a b2Body object. We create a b2_dynamicBody which means that the object will be controlled by box2d. We also set the position and which is very important we also set the userData field to the CCSprite we created in the step before:

b2BodyDef bodyDef;
bodyDef.type  = b2_dynamicBody;

bodyDef.position.Set (p.x /PTM_RATIO, p.y /PTM_RATIO );
bodyDef.userData  = sprite;
b2Body  *body  = world -&gt;CreateBody ( &amp;bodyDef );

With that done we own a b2Body which has no fixtures or shape yet. We now use the GB2ShapeCache to attach the fixtures created with PhysicsEditor. If you simply dragged theobject.png onto PhysicsEditor it’s name in the plist file will be object.

Create the body, then add the shape to the body:

[ [GB2ShapeCache sharedShapeCache ]
addFixturesToBody :body forShapeName : "object" ];

The last step is to set the anchor point for the CCSprite. PhysicsEditor allows you to set and edit anchor points. If you don’t do this the b2Body and the sprite displayed might have some offset which results in a strange behavior.

[sprite setAnchorPoint : [
[GB2ShapeCache sharedShapeCache ]
anchorPointForShape : "object" ] ];

Simulating the b2World

Now we have the physics in place but also need to update sprites by setting them to the position of the b2Body object and adjust rotation. We do this in the tick routine which we set up to be called from cocos2d’s scheduler for each frame (during init)

[self schedule :  @selector (tick : ) ];

In the tick we need to simulate the world. box2d does several iteration simulating small time steps and moving the bodies for each of them. Adjusting the values might give you better collision detection and physics behavior but might also result in longer calculation times.

After stepping the world we need to adjust each CCSprite according to the b2Body. We do this by iterating over all bodies in the world. Remember that we stored the CCSprite’s pointer in the userdata field of the b2Body. This is how we can adjust them:

- ( void ) tick :  (ccTime ) dt
{
int32 velocityIterations  =  8;
int32 positionIterations  =  1;
world -&gt;Step (dt, velocityIterations, positionIterations );

for  (b2Body * b  = world -&gt;GetBodyList ( ); b; b  = b -&gt;GetNext ( ) )
{
if  (b -&gt;GetUserData ( )  !=  NULL )
{
CCSprite  *myActor  =  (CCSprite * )b -&gt;GetUserData ( );
myActor.position  = CGPointMake (
b -&gt;GetPosition ( ).x  * PTM_RATIO,
b -&gt;GetPosition ( ).y  * PTM_RATIO  );
myActor.rotation  =  - 1  * CC_RADIANS_TO_DEGREES (b -&gt;GetAngle ( ) );
}
}
}

What next

Start playing with PhysicsEditor. E.g. adjust the parameters like bounce, friction to see the sprites bounce and jump.

The complete demo project including resouces and all you need to get started can be found in the Examples folder in the dmg file.

Download

代码转载自:https://pan.quark.cn/s/7f503284aed9 Hibernate的核心组件总数达到五个,具体包括:Session、SessionFactory、Transaction、Query以及Configuration。 这五个核心组件在各类开发项目中都具有普遍的应用性。 借助这些组件,不仅可以高效地进行持久化对象的读取与存储,还能够实现事务管理功能。 接下来将通过图形化的方式,逐一阐述这五个核心组件的具体细节。 依据所提供的文件内容,可以总结出以下几个关键知识点:### 1. SSH框架详细架构图尽管标题提及“SSH框架详细架构图”,但在描述部分并未直接呈现关于SSH的详细内容,而是转向介绍了Hibernate的核心接口。 然而,在此我们可以简要概述SSH框架(涵盖Spring、Struts、Hibernate)的核心理念及其在Java开发中的具体作用。 #### Spring框架- **定义**:Spring框架是一个开源架构,其设计目标在于简化企业级应用的开发流程。 - **特点**: - **分层结构**:该框架允许开发者根据实际需求选择性地采纳部分组件,而非强制使用全部功能。 - **可复用性**:Spring框架支持创建可在不同开发环境中重复利用的业务逻辑和数据访问组件。 - **核心构成**: - **核心容器**:该部分包含了Spring框架的基础功能,其核心在于`BeanFactory`,该组件通过工厂模式运作,并借助控制反转(IoC)理念,将配置和依赖管理与具体的应用代码进行有效分离。 - **Spring上下文**:提供一个配置文件,其中整合了诸如JNDI、EJB、邮件服务、国际化支持等企业级服务。 - **Spring AO...
下载前必看:https://pan.quark.cn/s/7de013c82358 在当代工作场所中,采用多显示器配置已成为增强工作效能的关键手段。 对于配备单个图形处理单元的个人用户而言,构建双屏系统并不构成挑战,只需遵循正确的操作流程即可达成目标。 以下是一份详尽的教程,指导用户如何借助电脑内建的单一显卡实现双屏操作。 首先确认必备的物理设备:一台搭载单显卡的计算机系统,以及至少两台可用的显示设备。 每台显示设备均需通过图形处理单元的输出端口(例如HDMI、VGA、DVI或DisplayPort)进行连接。 务必核实所有连接线缆均已稳固接入,且显示设备已开启并处于待机模式。 随后进入软件配置阶段:1. **系统配置界面**: - 在Windows操作系统环境中,通过在桌面上右键单击并选择“显示配置”(Display Configuration)。 系统将自动识别所有已连接的显示设备,并在界面上呈现相应的预览图像。 - 在MacOS操作系统环境中,需进入“系统参数设置”(System Parameter Settings),随后点击“显示设备”(Display Devices)。 2. **显示设备布局**: - 在“显示配置”界面中,用户可观察到屏幕的预览图像。 通过拖拽这些预览,依照实际的物理摆放顺序来排列显示设备。 此举可确保鼠标指针在屏幕间移动时呈现流畅自然的过渡效果。 3. **扩展显示功能**: - 在“显示配置”界面中找到“多显示支持”(Multi-Display Support)或“布局排列”(Layout Arrangement)选项。 选择“扩展这些显示设备”(Extend These Displays)功能。 该选项将使桌面界面能够跨越两个显示设备,从而提供更广...
已经博主授权,源码转载自 https://pan.quark.cn/s/fc0481c31b6c 身体质量指数(BMI)是衡量个体体重与身高比例的简明指标,它被广泛采纳用于健康状况的评估。 在“基础BMI计算器范例”中,展示了一个小型软件应用,该应用不仅能够计算用户的BMI数值,还能将计算结果记录在数据库内,从而协助用户监测其健康状况的动态变化。 此外,该应用借助AChartEngine库来绘制与BMI相关的图表,实现数据的可视化展示。 下面将具体阐述BMI的计算方法:BMI = 体重(千克) / 身高(米)的平方。 这一公式能够指导我们判定个体是否处于健康的体重区间。 通常情况下,BMI值低于18.5被视为偏瘦,18.5到24.9属于正常范围,25至29.9为超重,而30及以上则归类为肥胖。 这个软件范例对于初学者而言是一份极佳的学习资源,因为它涵盖了以下几个核心概念要点:1. **数据管理**:将BMI数值存储至数据库的操作暗示该程序可能采用了SQLite技术,作为Android系统自带的轻量级数据库,用于管理结构化信息。 掌握如何构建数据库表、执行数据插入、查询及更新等操作,是Android程序设计的基础技能。 2. **用户数据获取**:程序需要收集用户的体重与身高数据。 这涉及到用户界面(UI)的设计环节,包括运用EditText组件接收文本输入,并通过按钮激活计算流程。 熟悉Android的事件监听机制,如OnClickListener,对于构建交互式应用程序至关重要。 3. **计算处理**:完成BMI的计算过程需编写Java代码。 在此过程中,可以学习在Android开发环境中如何运用基础的数学运算和条件判断语句,例如if-else结构,以确定BMI的分类标准。 4. **结果展示*...
标题JSPM自行车个性化改装推荐系统研究AI更换标题第1章引言介绍自行车个性化改装推荐系统的研究背景、意义及国内外研究现状。1.1研究背景与意义阐述自行车个性化改装需求增长及推荐系统的重要性。1.2国内外研究现状分析国内外自行车改装推荐系统的研究进展及不足。1.3研究方法及创新点概述JSPM系统的设计方法及相较于其他系统的创新点。第2章相关理论介绍与自行车个性化改装推荐系统相关的理论基础。2.1个性化推荐理论阐述个性化推荐的基本原理和常用算法。2.2自行车改装知识介绍自行车结构、部件及改装选项等基础知识。2.3用户偏好分析理论讨论如何分析用户偏好以实现精准推荐。第3章JSPM系统设计详细介绍JSPM自行车个性化改装推荐系统的设计方案。3.1系统架构设计阐述系统的整体架构、模块划分及功能。3.2数据库设计介绍系统数据库的设计思路、表结构及关系。3.3推荐算法设计详细介绍基于用户偏好的推荐算法实现过程。第4章系统实现与测试介绍JSPM系统的实现过程及测试方法。4.1系统开发环境与工具说明系统开发所使用的环境、工具及技术栈。4.2系统实现过程阐述系统从设计到实现的具体步骤和关键代码。4.3系统测试与优化介绍系统的测试方法、测试结果及优化措施。第5章研究结果与分析展示JSPM系统的实验分析结果并进行讨论。5.1实验数据与指标介绍实验所采用的数据集、评估指标及实验环境。5.2实验结果展示通过图表等形式展示实验结果,包括推荐准确率等。5.3结果分析与讨论对实验结果进行详细分析,讨论系统的优缺点及改进方向。第6章结论与展望总结JSPM自行车个性化改装推荐系统的研究成果并展望未来。6.1研究结论概括本文的主要研究成果,包括系统设计、实现及实验结果。6.2展望指出系统存在的不足,提出未来研究的方向和改进措施。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值