DynamicBody潜入StaticBody

时间:2015-08-19 18:55:40

标签: android libgdx box2d

在我的游戏中,我想让DynamicBody潜入一个StaticBody。我建立了两个机构。目前他们正在相撞。如何让我的DynamicBody潜入水中?是否可以使用此声明:

body.applyImpulse(new Vec2(impulseX, impulseY), body.getWorldCenter());

由于密度更轻,它是否会自行恢复? 最后我希望StaticBody像水一样行动。为了让它感觉更像水,还有什么我应该做的吗?

Gamescreen.java

package com.joelbrun.jetskirider.screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
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.Fixture;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.World;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.utils.Scaling;
import com.badlogic.gdx.utils.viewport.ScalingViewport;
import com.badlogic.gdx.utils.viewport.Viewport;


public class Gamescreen implements Screen {

    private static final float TIMESTEP = 1.0f / 60.0f;
    private static final int VELOCITY_ITERATIONS = 8;
    private static final int POSITION_ITERATIONS = 3;
    public static final int GAMESCREEN_WIDTH = 1920/30;
    public static final int GAMESCREEN_HEIGHT = 720/30;
    public static final int OBSTACLE_TYPES = 5;
    private static final int WATER_HEIGHT = 5;
    private static final float JETSKI_BOX_X = 1.5f;
    private static final float JETSKI_BOX_Y = 1;
    private static final int WATER_WIDTH = 35;
    private World world;
    private Box2DDebugRenderer debugRenderer;
    public Texture jetski, wave;
    public Sprite background;
    public SpriteBatch batch;
    public OrthographicCamera camera;
    public Label score;
    Viewport viewport;

    @Override
    public void show() {
        world = new World(new Vector2(0, -9.81f), true);
        debugRenderer = new Box2DDebugRenderer();
        Texture[] texture = new Texture[OBSTACLE_TYPES];


        //Textures
        for (int i=0; i<OBSTACLE_TYPES; i++){
            texture[i] = new Texture(Gdx.files.internal("game/obstacles/obstacle" + Integer.toString(i+1)+".png"));
        }
        background = new Sprite(new Texture("game/background.png"));
        jetski = new Texture("game/jetski.png");
        wave = new Texture("game/water1.png");
        batch = new SpriteBatch();

        //Camera & Viewport
        camera = new OrthographicCamera();

        viewport = new ScalingViewport(Scaling.fillY, GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT, camera);
        viewport.apply();
        camera.position.set(GAMESCREEN_WIDTH / 2, GAMESCREEN_HEIGHT / 2, 0);

        //Jetski
        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(JETSKI_BOX_X +1, JETSKI_BOX_Y + WATER_HEIGHT*3);
        Body body = world.createBody(bodyDef);

        Body jetskiObject = world.createBody(bodyDef);

        PolygonShape jetskiBox = new PolygonShape();
        jetskiBox.setAsBox(JETSKI_BOX_X , JETSKI_BOX_Y );

        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = jetskiBox;
        fixtureDef.density = 100;
        fixtureDef.friction = 0;
        fixtureDef.restitution = .5f;

        Fixture jetskiFixture = body.createFixture(fixtureDef);

        jetskiBox.dispose();


        //body definition Water
        bodyDef.type = BodyDef.BodyType.StaticBody;
        bodyDef.position.set(WATER_WIDTH, WATER_HEIGHT);
        Body water = world.createBody(bodyDef);


        //shape
        PolygonShape box = new PolygonShape();
        box.setAsBox(WATER_WIDTH , WATER_HEIGHT);

        //fixture definition
        fixtureDef.shape = box;
        fixtureDef.density = 1000;
        fixtureDef.friction = .25f;
        fixtureDef.restitution = .1f;

        Fixture fixture = water.createFixture(fixtureDef);

        box.dispose();

    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        debugRenderer.render(world, camera.combined);

        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        batch.end();
        camera.update();

        world.step(TIMESTEP, VELOCITY_ITERATIONS, POSITION_ITERATIONS);
    }

    @Override
    public void resize(int width, int height) {
        int SCREEN_WIDTH = width;
        int SCREEN_HEIGHT = height;

        viewport.setWorldSize(GAMESCREEN_WIDTH, GAMESCREEN_HEIGHT);
        viewport.update(SCREEN_WIDTH, SCREEN_HEIGHT,true);
        camera.update();
    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {
        dispose();
    }

    @Override
    public void dispose() {
        batch.dispose();
        background.getTexture().dispose();
        jetski.dispose();
        wave.dispose();
        world.dispose();
        debugRenderer.dispose();
    }


}

1 个答案:

答案 0 :(得分:0)

有很多浮力教程和互联网上的源代码你试过吗?

Here is some source code in java hope this will help you