Java Libgdx汽车弹跳
我已经用libgdx创建了一个基本的汽车,现在我正在尝试创建一个地面,让它在上面行驶。起初,我只是用一个链状的东西做了一个静态的固定装置,在两个方向上都有两个远离屏幕的点。下面是有效的代码Java Libgdx汽车弹跳,java,android,libgdx,box2d,Java,Android,Libgdx,Box2d,我已经用libgdx创建了一个基本的汽车,现在我正在尝试创建一个地面,让它在上面行驶。起初,我只是用一个链状的东西做了一个静态的固定装置,在两个方向上都有两个远离屏幕的点。下面是有效的代码 ChainShape groundShapetest = new ChainShape(); groundShapetest.createChain(new float[]{-100, 0.1f , 10000, 0.1f}); environment.createFixture(groundShapetes
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;
}
while(last.x好的,我找到了解决方案。这是我使用的修复程序的链接。这有点像黑客,我希望Box2d能解决这个问题,但球似乎与线段的边缘发生碰撞,尽管它们的高度完全相同。
这对我有用。
当您指定0时,您会说固定装置密度将为0,但固定装置也有一个恢复属性。如果您尝试将其设置为0,可能也会发生这种情况?如果我正确理解了问题,这并不是一个真正的错误。这里的更多信息通常是,您会使用一个链形状将地形创建为一条长线,而不是一堆正方形我们这样做有点违背了链状形状的点,并最终导致了与多边形相同的问题。
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();
}
}