【发布时间】:2014-04-28 21:34:58
【问题描述】:
我已经用 libgdx 创建了一辆基本的汽车,现在我正在尝试创建一个地面让它继续行驶。起初,我只是使用链形制作了一个静态夹具,其中 2 个点在两个方向上都远离屏幕。这是有效的代码。
ChainShape groundShapetest = new ChainShape();
groundShapetest.createChain(new float[]{-100, 0.1f , 10000, 0.1f});
environment.createFixture(groundShapetest, 0);
groundShapetest.dispose();
这运行良好,汽车按预期平稳移动(没有弹跳)。接下来,我想添加更多有趣的地形,并尝试使用许多背靠背设置的小链形状来建造地面。
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() 只返回随机轮廓的下一个点。 当我这样做并启用汽车电机时,汽车开始随机弹跳,就好像它正在撞到什么东西或越过粗糙的地面一样。地面是完全光滑的,即使我将 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,但灯具也具有恢复属性。也许如果您也尝试将其设置为 0? LibGdx Fixture Restitution