box2D (javascript)

发布于 2024-12-08 22:24:51 字数 1116 浏览 0 评论 0原文

我刚刚开始使用 box2d。我能够创建一些对象并创建一个使用这些对象的世界。例如,创建一个盒子(取自: http://box2d-js.sourceforge.net/ index2.html)我可以这样做:

    elements = getElementsByClass("box2d");
      for ( var i = 0; i < elements.length; i ++ ) {
        properties[i] = getElementProperties( elements[i] );
        bodyDef.type = b2Body.b2_dynamicBody;
        var data = { element: elements[i]};
        bodyDef.userData = data;
        fixDef.shape = new b2PolygonShape;
        fixDef.shape.SetAsBox(2 //half width ,  2);
        bodyDef.position.x = Math.random() * 10;
        bodyDef.position.y = Math.random() * 10;
        world.CreateBody(bodyDef).CreateFixture(fixDef);
      }

我想做的是采用带有文本的div并将这些带有文本的div转换为形状并在box2D中使用它们。我真的不知道从哪里开始。我现在正在使用 userData 属性,认为这至少是一个开始。

我在这里找到了一个很好的例子:http://mrdoob.com/projects/chromeexperiments/google_gravity /

定义了 getElementProperties 和 getElementsByClass。我正在考虑使用 getElementProperties 来设置形状尺寸。

I am just getting started with box2d. I am able to create some objects and create a world in which to use those objects. For example, to create a box (taken from: http://box2d-js.sourceforge.net/index2.html) I can do so like this:

    elements = getElementsByClass("box2d");
      for ( var i = 0; i < elements.length; i ++ ) {
        properties[i] = getElementProperties( elements[i] );
        bodyDef.type = b2Body.b2_dynamicBody;
        var data = { element: elements[i]};
        bodyDef.userData = data;
        fixDef.shape = new b2PolygonShape;
        fixDef.shape.SetAsBox(2 //half width ,  2);
        bodyDef.position.x = Math.random() * 10;
        bodyDef.position.y = Math.random() * 10;
        world.CreateBody(bodyDef).CreateFixture(fixDef);
      }

What I would like to do is take divs with text and turn those divs with text into shapes and use them in box2D. I really don't know where to start with this. I am using the userData property now thinking that this is at least a start.

I found a great example of this in action here: http://mrdoob.com/projects/chromeexperiments/google_gravity/

getElementProperties and getElementsByClass are defined. I was thinking about using getElementProperties to set the shape dimensions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

若水般的淡然安静女子 2024-12-15 22:24:52

我正在用 iOS 做类似的事情,但一个好的方法是根据 div 中保存的信息制作图像。然后,您可以用该图像制作一个精灵,并将其添加为 box2d 主体的 userData。当您更新(勾选:iOS 中的方法,我不确定 JavaScript)时,您将迭代所有主体并调整精灵位置。 box2d 世界应该为你做所有其他事情。

I am doing something similar with iOS, but a good way to approach it is to make images out of the information held in the div. Then you can make a sprite out of that image and add it as the userData for your box2d body. When you update (tick: method in iOS, I am not sure for javascript) then you iterate over all the bodies and adjust the sprite location. The box2d world should do everything else for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文