微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

由于摩擦而快速击中地面时,jBox2D球在x轴处停止

如何解决由于摩擦而快速击中地面时,jBox2D球在x轴处停止

我正在尝试创建一个游戏,您可以在其中移动球,并且其中会有摩擦,但是如果我添加摩擦力,则球在跳高后撞到地面时会停止水平移动(可能是因为正常当球高速下落时,力较高,因此摩擦力更大)。 当我除去摩擦力(将其设置为0)时可以,但是我想要摩擦力。

这是用于创建球和地板的主体的代码

BodyDef ballDef = new BodyDef();
ballDef.type = BodyType.DYNAMIC;
CircleShape golfShape = new CircleShape();
golfShape.seTradius(blockSize / 6.0f / jBox2DScale);
ballBody = world.createBody(ballDef);
ballBody.setFixedRotation(true);
FixtureDef ballFixture = new FixtureDef();
ballFixture.density = 1f;
//ballFixture.restitution = 0.9f;
ballFixture.friction = 0.1f;
ballFixture.shape = golfShape;
ballBody.createFixture(ballFixture);
ballBody.setTransform(new Vec2(width / 2.0f / jBox2DScale,height / 2.0f / jBox2DScale),0.0f);
BodyDef blockDef = new BodyDef();
blockDef.type = BodyType.STATIC;
Body body = world.createBody(blockDef);
FixtureDef blockFixture = new FixtureDef();
blockFixture.density = 100;
blockFixture.restitution = 0.75f;
blockFixture.friction = 0.15f;
EdgeShape sideShape = new EdgeShape();
sideShape.set(new Vec2(0.0f,0.0f),new Vec2(width,0.0f));
sideShape.m_vertex0.set(-width,0.0f);
sideShape.m_vertex3.set(width * 2,0.0f);
sideShape.m_hasvertex0 = true;
sideShape.m_hasvertex3 = true;
sideShape.seTradius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(width,height));
sideShape.m_vertex0.set(width,-height);
sideShape.m_vertex3.set(width,height * 2);
sideShape.m_hasvertex0 = true;
sideShape.m_hasvertex3 = true;
sideShape.seTradius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(width,height),new Vec2(0,height));
sideShape.m_vertex0.set(width * 2,height);
sideShape.m_vertex3.set(-width,height);
sideShape.m_hasvertex0 = true;
sideShape.m_hasvertex3 = true;
sideShape.seTradius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
sideShape = new EdgeShape();
sideShape.set(new Vec2(0.0f,new Vec2(0.0f,0.0f));
sideShape.m_vertex0.set(0.0f,height * 2);
sideShape.m_vertex3.set(0.0f,-height);
sideShape.m_hasvertex0 = true;
sideShape.m_hasvertex3 = true;
sideShape.seTradius(0.01f);
blockFixture.shape = sideShape;
body.createFixture(blockFixture);
body.setTransform(new Vec2(x,y),0.0f);

发生的情况的图像:

enter image description here

有人知道如何解决吗?

(编辑) 我取消了球上的固定旋转并添加了角度阻尼,这是固定它的最佳简便方法

解决方法

您可以通过ballBody.setFixedRotation(false);来解决此问题 或完全删除该语句。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。