资讯类型: 翻译
来源页面: http://www.emanueleferonato.com/2010/01/06/pausing-a-box2d-simulation/
资讯原标题: Pausing a Box2D simulation
资讯原作者: Emanuele Feronato
我的评论:
对这篇文你有啥看法,跟贴说说吧!欢迎口水和板砖,哈哈。欢迎大家和我们一同分享更多资讯。
Box2D有一个功能能够很简单的执行且可以添加的很有趣的游戏播放,但是我还没在任何地方发现过解释:这个东西是pause。
我想在我单击鼠标的时候暂停Box2D模拟,当再次点击的时候它又继续。
此功能的核心函数是Step。
正常的都是这样调用的:
b2World.Step(timestep, iterations)
timestep是模拟的时间(正常的是1/30或者1/60秒),然后重复约束方案使用的重复次数。
所以,用好timestep是成功的关键。在这个脚本当中,都是复制/粘贴自A smart way to manage sleeping objects with Box2D的,我将用鼠标单击停止模拟,然后再次单击鼠标以继续。- package {
- import flash.display.Sprite;
- import flash.events.Event;
- import flash.utils.Timer;
- import flash.events.TimerEvent;
- import flash.events.MouseEvent;
- import Box2D.Dynamics.*;
- import Box2D.Collision.*;
- import Box2D.Collision.Shapes.*;
- import Box2D.Common.Math.*;
- public class demo extends Sprite {
- var the_world:b2World;
- var time_count:Timer=new Timer(1500);
- var timestep:Number=1/30;
- public function demo() {
- var environment:b2AABB = new b2AABB();
- environment.lowerBound.Set(-100.0, -100.0);
- environment.upperBound.Set(100.0, 100.0);
- var gravity:b2Vec2=new b2Vec2(0.0,10.0);
- the_world=new b2World(environment,gravity,true);
- var final_body:b2Body;
- var the_body:b2BodyDef;
- var the_box:b2PolygonDef;
- the_body = new b2BodyDef();
- the_body.position.Set(8.5, 14);
- the_box = new b2PolygonDef();
- the_box.SetAsBox(8.5, 0.5);
- the_box.friction=0.3;
- the_box.density=0;
- final_body=the_world.CreateBody(the_body);
- final_body.CreateShape(the_box);
- final_body.SetMassFromShapes();
- addEventListener(Event.ENTER_FRAME, on_enter_frame);
- stage.addEventListener(MouseEvent.CLICK,on_click);
- time_count.addEventListener(TimerEvent.TIMER, on_time);
- time_count.start();
- }
- public function on_click(e:MouseEvent) {
- if (timestep) {
- timestep=0;
- } else {
- timestep=1/30;
- }
- }
- public function on_time(e:Event) {
- if (timestep) {
- var final_body:b2Body;
- var the_body:b2BodyDef;
- var the_box:b2PolygonDef;
- var box_width=Math.random()+0.1;
- var box_height=Math.random()+0.1;
- the_body = new b2BodyDef();
- the_body.position.Set(Math.random()*10+2, 0);
- the_body.userData = new cubeface();
- the_body.userData.width=box_width*60;
- the_body.userData.height=box_height*60;
- the_box = new b2PolygonDef();
- the_box.SetAsBox(box_width,box_height);
- the_box.friction=0.3;
- the_box.density=1;
- final_body=the_world.CreateBody(the_body);
- final_body.CreateShape(the_box);
- final_body.SetMassFromShapes();
- addChild(the_body.userData);
- }
- }
- public function on_enter_frame(e:Event) {
- the_world.Step(timestep, 10);
- for (var bb:b2Body = the_world.m_bodyList; bb; bb = bb.m_next) {
- if (bb.m_userData is Sprite) {
- bb.m_userData.x=bb.GetPosition().x*30;
- bb.m_userData.y=bb.GetPosition().y*30;
- bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
- if (bb.IsSleeping()) {
- bb.m_userData.gotoAndStop(2);
- } else {
- bb.m_userData.gotoAndStop(1);
- }
- }
- }
- }
- }
- }
复制代码 让我们看看这些有趣的行。
69 行: Step 函数用一个在第15行声明设置为1/30的timestep变量开始执行。
35 行: 添加一个鼠标监听器等待点击和调用on_click函数。
39-45 行: on_click函数 在0和1/30之间切换timestep变量。这个方法我们可以停止和恢复模拟。
第47行:timestep的值检查是用来避免在模拟暂停的时候生成新盒子。
以下是结果:
单击鼠标按钮暂停/播放模拟,注意看非休眠对象在暂停的时候是如何不休眠的。
无需下载任何东西,只要在A smart way to manage sleeping objects with Box2D的范例中复制/粘贴代码。 |
-
2
评分人数
-