我用libgdx制作了一辆基本车,现在我正在尝试为它开车创造一个地面。起初我只是使用一个带有2个点的链形状的静态夹具,两个方向都远离屏幕。这是有效的代码。
ChainShape groundShapetest = new ChainShape();
groundShapetest.createChain(new float[]{-100, 0.1f , 10000, 0.1f});
environment.createFixture(groundShapetest, 0);
groundShapetest.dispose();
这很好,汽车按预期顺利移动(没有弹跳)。接下来,我想添加更多有趣的地形,并尝试从背靠背的许多小型Chainshapes建造地面。
while (last.x <= rightEdge)
{
next = GenerateTerrainContour(h, last.x);//, w/(float)num_sections);
EdgeShape groundShapetest = new EdgeShape();
groundShapetest.set(last, next);
environment.createFixture(groundShapetest, 0);
groundShapetest.dispose();
last = next;
}
GenerateTerrainContour()只返回随机轮廓的下一个点。 当我这样做并启用汽车电机时,汽车开始随机弹跳,好像它正在撞击某物或越过粗糙的地面。 Ground是完全平滑的,即使我将GenerateTerrainContour()的y分量设置为始终返回0,它仍然可以执行。
我甚至不确定如何调试这个,因为显然会有一些影响汽车的力量,但我不知道它来自哪里。任何帮助将非常感激。 如果有帮助,我可以发布更多代码吗?
如果有人想尝试模拟这个,我创造了一个只有一个轮子的简单项目并给它一个扭矩。它在我创造的地面上做了同样的弹跳。您所要做的就是将此代码复制到libgdx项目中,您将看到我在说什么。我必须遗漏一些简单的东西但我不知道的是什么。
package com.mygdx.game;
import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.Box2DDebugRenderer;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
public class groundTest extends ApplicationAdapter {
private static final float WORLD_TO_BOX = 0.01f;
private static final float BOX_TO_WORLD = 100f;
private static final float TIMESTEP = 1 / 60f;;
private static final int VELOCITYITERATIONS = 8;
private static final int POSITIONITERATIONS = 3;
private static final String TITLE = "GND_TEST";
private static final boolean DEBUG = true;
private static final int PPM = 50;
private World world;
private Box2DDebugRenderer debugRenderer;
private OrthographicCamera camera;
private Car car;
Body lw;
@Override
public void create () {
float w = Gdx.graphics.getWidth()/PPM;
float h = Gdx.graphics.getHeight()/PPM;
world = new World(new Vector2(0, -9.81f), true);
camera = new OrthographicCamera(w, h);
camera.update();
createGND();
//initCar(w,h);
makeWheel();
debugRenderer = new Box2DDebugRenderer();
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width/PPM;
camera.viewportHeight = height/PPM;
camera.update();
// camera.position.set(camera.viewportWidth/2f, camera.viewportHeight/2f, 0);
}
@Override
public void render () {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
Gdx.graphics.setTitle(TITLE + " -- FPS: " + Gdx.graphics.getFramesPerSecond());
// camera.position.set(car.getChassis().getPosition().x + ((float)Gdx.graphics.getWidth() / PPM)/4.0f, camera.viewportHeight/3.0f, 0);
camera.position.set(lw.getPosition().x, 0, 0);
camera.update();
if(DEBUG)
{
//Matrix4 cameraCopy = camera.combined.cpy();
//debugRenderer.render(world, cameraCopy.scl(BOX_TO_WORLD));
debugRenderer.render(world, camera.combined);
}
world.step(TIMESTEP, VELOCITYITERATIONS, POSITIONITERATIONS);
}
private void createGND()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.StaticBody;
Body ground = world.createBody(bodyDef);
//Uncomment the next 4 lines to get gronud that causes no bouncing
//ChainShape groundNoBounce = new ChainShape();
//groundNoBounce.createChain(new float[]{-10f, 0, 1000f, 0});
//ground.createFixture(groundNoBounce, 0.0f).setRestitution(0);
//groundNoBounce.dispose();
// comment this loop out if above uncommented
for(int x = -10; x < 1000; x+=1)
{
ChainShape groundBox = new ChainShape();
groundBox.createLoop(new float[]{(float)x,-3.0f,
(float)x+1.0001f, -3.0f,
(float)x+1.0001f,0.0f,
(float)x, 0.0f});
ground.createFixture(groundBox, 0.0f).setRestitution(0);
groundBox.dispose();
}
}
private void makeWheel()
{
BodyDef bodyDef = new BodyDef();
bodyDef.type = BodyType.DynamicBody;
bodyDef.position.set(0, 10);
FixtureDef wheelFixtureDef = new FixtureDef();
wheelFixtureDef.density = 2f;
wheelFixtureDef.friction = 50;//0.8f;
wheelFixtureDef.restitution = 0.0f;//25f;//0.5f;
//
CircleShape wheelShape = new CircleShape();
wheelShape.setRadius(0.5f);
wheelFixtureDef.shape = wheelShape;
lw = world.createBody(bodyDef);
lw.createFixture(wheelFixtureDef).setRestitution(0.0f);
lw.applyTorque(-3000f, true);
camera.zoom = .25f;
camera.update();
}
}
答案 0 :(得分:0)
好的,我找到了解决方案。这是我使用的修复程序的链接。这有点像黑客,我希望Box2d可以解决这个问题,但是看起来球正在与段的边缘碰撞,即使它们都是完全相同的高度。 这对我有用。 http://www.box2d.org/forum/viewtopic.php?f=8&t=7917