在 libgdx 中,如何向 box2d Body 对象添加摩擦力?

In libgdx, how do I add friction to a box2d Body object?(在 libgdx 中,如何向 box2d Body 对象添加摩擦力?)
本文介绍了在 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 对象添加摩擦力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

quot;Char cannot be dereferencedquot; error(“Char 不能被取消引用错误)
Java Switch Statement - Is quot;orquot;/quot;andquot; possible?(Java Switch 语句 - 是“或/“和可能的?)
Java Replace Character At Specific Position Of String?(Java替换字符串特定位置的字符?)
What is the type of a ternary expression with int and char operands?(具有 int 和 char 操作数的三元表达式的类型是什么?)
Read a text file and store every single character occurrence(读取文本文件并存储出现的每个字符)
Why do I need to explicitly cast char primitives on byte and short?(为什么我需要在 byte 和 short 上显式转换 char 原语?)