我已经编写了以下代码来进行身体跳跃和其他一些事情,但问题是身体只是跳到一定限度而且只有玩家身体不受重力的影响应该是这样的我将重力比例增加到100 请给出跳跃和重力的解决方案
public class Shoot implements ApplicationListener {
private OrthographicCamera camera;
private World world;
private Box2DDebugRenderer renderer;
private short x, y;
private float g;
private SpriteBatch batch;
private ChainShape groundshape;
private BodyDef bdef;
private FixtureDef fdef;
private Body player;
private PolygonShape pshape;
private CircleShape cshape;
private Vector2 v;
private ArrayList<Body> d;
private ArrayList<Body> bullet;
private Sprite bsprite;
private boolean right = true;
private Body enemy;
private byte l = 3;
@Override
public void create() {
d = new ArrayList<Body>();
bullet = new ArrayList<Body>();
dimensions();
camera();
renderer();
world();
batch();
input();
ground();
player();
enemy();
collisions();
bricks(4, -6);
sprites();
}
@Override
public void dispose() {
batch.dispose();
world.dispose();
renderer.dispose();
pshape.dispose();
cshape.dispose();
}
@Override
public void render() {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);
world.step(1 / 60f, 8, 3);
batch.setProjectionMatrix(camera.combined);
batch.begin();
for (int i = 0; i < bullet.size(); i++) {
bsprite.setPosition(
bullet.get(i).getPosition().x - bsprite.getWidth() / 2f,
bullet.get(i).getPosition().y - bsprite.getHeight() / 2f);
if (bullet.get(i).getLinearVelocity().y == 200)
bsprite.rotate90(false);
bsprite.draw(batch);
}
batch.end();
for (int i = 0; i < d.size(); i++) {
world.destroyBody(d.get(i));
d.remove(i);
i -= 1;
}
player.setLinearVelocity(v);
camera.position.x = player.getPosition().x;
camera.update();
renderer.render(world, camera.combined);
}
@Override
public void resize(int width, int height) {
camera.viewportWidth = width / 25f;
camera.viewportHeight = height / 25f;
camera.update();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
private void world() {
world = new World(new Vector2(0, -9.81f), true);
}
private void dimensions() {
x = (short) Gdx.graphics.getWidth();
y = (short) Gdx.graphics.getHeight();
g = (float) ((y * 14 / 720f) * (6 / 7f));
}
private void renderer() {
renderer = new Box2DDebugRenderer();
}
private void camera() {
camera = new OrthographicCamera(x / 25f, y / 25f);
}
private void batch() {
batch = new SpriteBatch();
}
private void input() {
Gdx.input.setInputProcessor(new InputProcessor() {
@Override
public boolean touchUp(int screenX, int screenY, int pointer,
int button) {
return false;
}
@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
return false;
}
@Override
public boolean touchDown(int screenX, int screenY, int pointer,
int button) {
bullet();
return false;
}
@Override
public boolean scrolled(int amount) {
return false;
}
@Override
public boolean mouseMoved(int screenX, int screenY) {
return false;
}
@Override
public boolean keyUp(int keycode) {
if (keycode == Keys.D || keycode == Keys.A) {
v.x = 0;
}
return false;
}
@Override
public boolean keyTyped(char character) {
return false;
}
@Override
public boolean keyDown(int keycode) {
if (keycode == Keys.ESCAPE) {
Gdx.app.exit();
}
if (keycode == Keys.D) {
v.x = 15;
right = true;
}
if (keycode == Keys.A) {
v.x = -15f;
right = false;
}
if (keycode == Keys.SPACE) {
player.applyLinearImpulse(new Vector2(0, 50000),
player.getLocalCenter(), true);
}
return false;
}
});
}
private void ground() {
bdef = new BodyDef();
bdef.type = BodyType.StaticBody;
bdef.allowSleep = true;
groundshape = new ChainShape();
groundshape.createChain(new Vector2[] { new Vector2(-x, -g),
new Vector2(x, -g) });
fdef = new FixtureDef();
fdef.shape = groundshape;
fdef.restitution = 0f;
fdef.friction = 0f;
fdef.density = 0.5f;
world.createBody(bdef).createFixture(fdef);
groundshape.dispose();
}
private void player() {
v = new Vector2(0, 0);
bdef.type = BodyType.DynamicBody;
bdef.allowSleep = true;
bdef.fixedRotation = true;
bdef.gravityScale = 100;
bdef.position.x = 0f;
bdef.position.y = -g + 2f;
pshape = new PolygonShape();
pshape.setAsBox(1, 2);
fdef.shape = pshape;
fdef.friction = 0f;
fdef.restitution = 0f;
fdef.density = 1f;
player = world.createBody(bdef);
player.createFixture(fdef);
cshape = new CircleShape();
cshape.setPosition(new Vector2(0, 3.1f));
cshape.setRadius(1f);
fdef.shape = cshape;
player.createFixture(fdef);
}
private void bricks(float x, float y) {
bdef.type = BodyType.StaticBody;
bdef.allowSleep = true;
bdef.position.x = x;
bdef.position.y = y;
fdef.density = 0.5f;
fdef.restitution = 0f;
pshape.setAsBox(3, 1);
fdef.shape = pshape;
fdef.friction = 0f;
world.createBody(bdef).createFixture(fdef);
}
private void collisions() {
world.setContactListener(new ContactListener() {
@Override
public void preSolve(Contact contact, Manifold oldManifold) {
}
@Override
public void postSolve(Contact contact, ContactImpulse impulse) {
if (contact.getFixtureA().getShape().getType() == cshape
.getType()
|| contact.getFixtureB().getShape().getType() == cshape
.getType()) {
v.y = 0;
}
if (contact.getFixtureA().getBody().isBullet()) {
d.add(contact.getFixtureA().getBody());
bullet.remove(bullet.indexOf(contact.getFixtureA()
.getBody()));
}
if (contact.getFixtureB().getBody().isBullet()) {
d.add(contact.getFixtureB().getBody());
bullet.remove(bullet.indexOf(contact.getFixtureB()
.getBody()));
}
if ((contact.getFixtureA().getBody().equals(enemy) && contact
.getFixtureB().getBody().isBullet())
|| contact.getFixtureB().getBody().equals(enemy)
&& contact.getFixtureA().getBody().isBullet()) {
enemy.setLinearVelocity(0, 0);
l--;
if (l == 0) {
d.add(enemy);
}
}
}
@Override
public void endContact(Contact contact) {
}
@Override
public void beginContact(Contact contact) {
}
});
}
private void bullet() {
bdef.type = BodyType.DynamicBody;
bdef.allowSleep = true;
bdef.bullet = true;
bdef.fixedRotation = true;
bdef.gravityScale = 0;
pshape.setAsBox(0.25f, 0.15f);
if (right && !Gdx.input.isKeyPressed(Keys.W)) {
bdef.position.x = player.getPosition().x + 1.6f;
bdef.position.y = player.getPosition().y + 1f;
} else if (!right && !Gdx.input.isKeyPressed(Keys.W)) {
bdef.position.x = player.getPosition().x - 1.6f;
bdef.position.y = player.getPosition().y + 1f;
} else if (Gdx.input.isKeyPressed(Keys.W)) {
bdef.position.x = player.getPosition().x;
bdef.position.y = player.getPosition().y + 4.5f;
pshape.setAsBox(0.15f, 0.25f);
}
fdef.density = 0.4f;
fdef.shape = pshape;
fdef.restitution = 0f;
fdef.friction = 0f;
bullet.add(world.createBody(bdef));
bullet.get(bullet.size() - 1).createFixture(fdef);
if (right && !Gdx.input.isKeyPressed(Keys.W)) {
bullet.get(bullet.size() - 1).setLinearVelocity(50, 0);
} else if (!right && !Gdx.input.isKeyPressed(Keys.W)) {
bullet.get(bullet.size() - 1).setLinearVelocity(-50, 0);
} else if (Gdx.input.isKeyPressed(Keys.W)) {
bullet.get(bullet.size() - 1).setLinearVelocity(0, 50);
}
}
private void sprites() {
bsprite = new Sprite(new Texture("data/bullet.jpg"));
bsprite.setOrigin(bsprite.getWidth() / 2f, bsprite.getHeight() / 2f);
bsprite.rotate(0);
bsprite.setSize(0.5f, 0.30f);
}
private void enemy() {
bdef.type = BodyType.DynamicBody;
bdef.allowSleep = true;
bdef.fixedRotation = true;
bdef.gravityScale = 100;
bdef.position.x = 7f;
bdef.position.y = -g + 2f;
pshape = new PolygonShape();
pshape.setAsBox(1, 2);
fdef.shape = pshape;
fdef.friction = 0f;
fdef.restitution = 0f;
fdef.density = 1f;
enemy = world.createBody(bdef);
enemy.createFixture(fdef);
cshape = new CircleShape();
cshape.setPosition(new Vector2(0, 3));
cshape.setRadius(1f);
fdef.shape = cshape;
enemy.createFixture(fdef);
}
}
答案 0 :(得分:0)
在渲染方法中使用player.setLinearVelocity()阻碍了身体在y方向设置线速度0的运动