box2D (javascript)
我刚刚开始使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我正在用 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.
您可以参考此示例。
http://jsbin.com/efequk/5
http://www.jeremyhubble.com/box2d.html
You can take reference this samples..
http://jsbin.com/efequk/5
http://www.jeremyhubble.com/box2d.html