在libgdx中,如何为box2d Body对象添加摩擦力? [英] In libgdx, how do I add friction to a box2d Body object?

查看:516
本文介绍了在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文档建议我在最初定义对象时应该在fixture 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对象添加摩擦力?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆