In libgdx, how do I add friction to a box2d Body object?(在 libgdx 中,如何向 box2d Body 对象添加摩擦力?)
问题描述
我对 libgdx 非常满意,喜欢使用它.但是,我是 box2d 的新手,所以我希望比我更聪明的人能帮助我.
I'm having an absolute blast with libgdx, love using it. However, I am a newbie with box2d, so I was hoping someone smarter than me could help me out.
我有一个简单的测试屏幕,其中一堆静态方形瓷砖组成了一个地板,一个动态的身体(一个圆圈)在周围反弹.
I have a simple test screen, where a bunch of static square tiles make up a floor, and a dynamic body (a circle) bounces around.
我想做的是增加地板的摩擦力,这样球就不会滚动得那么厉害.就像地板是草而不是木头.
What I'm trying to do is to increase the friction of the floor so the ball doesn't roll as much. Like the floor was grass instead of wood.
我在网上找到了一些东西,但似乎都不起作用.我尝试过的最有希望的事情是:
I found a couple of things online, but neither seem to work. The most promising thing I tried is this:
tileBody.getFixtureList().get(0).setFriction(0.9f);
但它似乎什么也没做.
阅读 box2d 文档建议我在最初定义对象时应该在夹具 def 上设置摩擦:
Reading the box2d docs suggests that I should set friction on the fixture def when I define the object originally:
FixtureDef fdef = new FixtureDef();
fdef.shape = wallshape;
fdef.density = 1.0f;
fdef.friction = 0.9f;
但是,这似乎也无法阻止球滚动.
However, this doesn't seem to stop the ball rolling much either.
有没有更好的方法来做到这一点?我可以减少恢复原状,但这只会阻止它反弹,对吗?
Is there a better way of doing this? I can reduce the restitution, but that'll just stop it bouncing as much right?
推荐答案
Body groundBody = world.createBody(groundBodyDef);
PolygonShape groundshape = new PolygonShape();
groundshape.setAsBox(30, 1.0f);
FixtureDef groundFixture = new FixtureDef();
groundFixture.density=0.0f;
groundFixture.shape = groundshape;
groundFixture.restitution = .5f;
groundFixture.friction=0f;
groundBody.createFixture(groundFixture);
groundshape.dispose();
这对我有用.如果沿着地面移动的物体有摩擦并且不是由暗物质构成,它们应该会减速.
This worked for me. Provided the things moving along the ground have friction and aren't made of dark matter, they should slow down.
这篇关于在 libgdx 中,如何向 box2d Body 对象添加摩擦力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 libgdx 中,如何向 box2d Body 对象添加摩擦力?
基础教程推荐
- 如何对 HashSet 进行排序? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01