9RIA.com天地会 - Flash论坛

返回列表 发帖

[新闻资讯] 暂停Box2D模拟

  • 资讯类型: 翻译
  • 来源页面: 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的,我将用鼠标单击停止模拟,然后再次单击鼠标以继续。
    1. package {
    2.         import flash.display.Sprite;
    3.         import flash.events.Event;
    4.         import flash.utils.Timer;
    5.         import flash.events.TimerEvent;
    6.         import flash.events.MouseEvent;
    7.         import Box2D.Dynamics.*;
    8.         import Box2D.Collision.*;
    9.         import Box2D.Collision.Shapes.*;
    10.         import Box2D.Common.Math.*;
    11.         public class demo extends Sprite {
    12.                 var the_world:b2World;
    13.                 var time_count:Timer=new Timer(1500);
    14.                 var timestep:Number=1/30;
    15.                 public function demo() {
    16.                         var environment:b2AABB = new b2AABB();
    17.                         environment.lowerBound.Set(-100.0, -100.0);
    18.                         environment.upperBound.Set(100.0, 100.0);
    19.                         var gravity:b2Vec2=new b2Vec2(0.0,10.0);
    20.                         the_world=new b2World(environment,gravity,true);
    21.                         var final_body:b2Body;
    22.                         var the_body:b2BodyDef;
    23.                         var the_box:b2PolygonDef;
    24.                         the_body = new b2BodyDef();
    25.                         the_body.position.Set(8.5, 14);
    26.                         the_box = new b2PolygonDef();
    27.                         the_box.SetAsBox(8.5, 0.5);
    28.                         the_box.friction=0.3;
    29.                         the_box.density=0;
    30.                         final_body=the_world.CreateBody(the_body);
    31.                         final_body.CreateShape(the_box);
    32.                         final_body.SetMassFromShapes();
    33.                         addEventListener(Event.ENTER_FRAME, on_enter_frame);
    34.                         stage.addEventListener(MouseEvent.CLICK,on_click);
    35.                         time_count.addEventListener(TimerEvent.TIMER, on_time);
    36.                         time_count.start();
    37.                 }
    38.                 public function on_click(e:MouseEvent) {
    39.                         if (timestep) {
    40.                                 timestep=0;
    41.                         } else {
    42.                                 timestep=1/30;
    43.                         }
    44.                 }
    45.                 public function on_time(e:Event) {
    46.                         if (timestep) {
    47.                                 var final_body:b2Body;
    48.                                 var the_body:b2BodyDef;
    49.                                 var the_box:b2PolygonDef;
    50.                                 var box_width=Math.random()+0.1;
    51.                                 var box_height=Math.random()+0.1;
    52.                                 the_body = new b2BodyDef();
    53.                                 the_body.position.Set(Math.random()*10+2, 0);
    54.                                 the_body.userData = new cubeface();
    55.                                 the_body.userData.width=box_width*60;
    56.                                 the_body.userData.height=box_height*60;
    57.                                 the_box = new b2PolygonDef();
    58.                                 the_box.SetAsBox(box_width,box_height);
    59.                                 the_box.friction=0.3;
    60.                                 the_box.density=1;
    61.                                 final_body=the_world.CreateBody(the_body);
    62.                                 final_body.CreateShape(the_box);
    63.                                 final_body.SetMassFromShapes();
    64.                                 addChild(the_body.userData);
    65.                         }
    66.                 }
    67.                 public function on_enter_frame(e:Event) {
    68.                         the_world.Step(timestep, 10);
    69.                         for (var bb:b2Body = the_world.m_bodyList; bb; bb = bb.m_next) {
    70.                                 if (bb.m_userData is Sprite) {
    71.                                         bb.m_userData.x=bb.GetPosition().x*30;
    72.                                         bb.m_userData.y=bb.GetPosition().y*30;
    73.                                         bb.m_userData.rotation = bb.GetAngle() * (180/Math.PI);
    74.                                         if (bb.IsSleeping()) {
    75.                                                 bb.m_userData.gotoAndStop(2);
    76.                                         } else {
    77.                                                 bb.m_userData.gotoAndStop(1);
    78.                                         }
    79.                                 }
    80.                         }
    81.                 }
    82.         }
    83. }
    复制代码
    让我们看看这些有趣的行。
    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

    评分人数

    [f]笛里番腔,街头戏鼓,不是歌声。[/f]

    RIA 天地行 北京站(2010·03·28)
    很帅的效果哦
    http://www.jianhualee.com

    TOP

    天地人才库
    这!!!!!!!!

    TOP

    很帅的效果

    TOP

    确实很不错

    TOP

    返回列表