5.5 C
New York
Saturday, March 15, 2025

libgdx – The best way to drag present physique to a different place?


You could set the remodel of the Physique to maneuver it to the required coordinates:

physique.setTransform(place.x, place.y, physique.getAngle());

Doing that can snap the Physique to the coordinates and provides you with an correct method of dragging the Physique, however beware that the simulation could be unstable in some eventualities as you’re transferring issues explicity as a substitute of utilizing forces and velocities (normally it is advantageous although).

The complete supply for the instance above is:

public class SandboxGame extends ApplicationAdapter {

    non-public OrthographicCamera digital camera;
    non-public World world;
    non-public Box2DDebugRenderer debugRenderer;
    non-public Physique physique;
    non-public Physique kinematicBody;
    non-public Physique floor;

    non-public Vector3 unprojectVector = new Vector3();
    non-public Vector2 worldTouchPosition = new Vector2();

    @Override
    public void create () {
        float aspectRatio = (float)Gdx.graphics.getWidth() / Gdx.graphics.getHeight();
        float w = 100.0f;
        digital camera = new OrthographicCamera(w, w / aspectRatio);
        world = new World(new Vector2(0, -80), false);
        debugRenderer = new Box2DDebugRenderer();

        // Create physique
        {
            PolygonShape form = new PolygonShape();
            form.setAsBox(4, 4);
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.form = form;
            fixtureDef.density = 1.0f;
            BodyDef bodyDef = new BodyDef();
            bodyDef.kind = BodyDef.BodyType.DynamicBody;
            physique = world.createBody(bodyDef);
            physique.createFixture(fixtureDef);
            form.dispose();
        }

        // Create kinematic physique
        {
            PolygonShape form = new PolygonShape();
            form.setAsBox(4, 2);
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.form = form;
            fixtureDef.density = 1.0f;
            BodyDef bodyDef = new BodyDef();
            bodyDef.kind = BodyDef.BodyType.KinematicBody;
            kinematicBody = world.createBody(bodyDef);
            kinematicBody.createFixture(fixtureDef);
            kinematicBody.setTransform(-20, 0, 0);
            form.dispose();
        }

        // Create floor
        {
            PolygonShape form = new PolygonShape();
            form.setAsBox(100, 2);
            FixtureDef fixtureDef = new FixtureDef();
            fixtureDef.form = form;
            fixtureDef.density = 1.0f;
            BodyDef bodyDef = new BodyDef();
            bodyDef.kind = BodyDef.BodyType.StaticBody;
            floor = world.createBody(bodyDef);
            floor.createFixture(fixtureDef);
            floor.setTransform(0, -20, 0);
            form.dispose();
        }

        Gdx.enter.setInputProcessor(new InputAdapter() {
            public boolean touchDragged(int screenX, int screenY, int pointer) {
                Vector2 place = unproject(screenX, screenY);
                kinematicBody.setTransform(place.x, place.y, kinematicBody.getAngle());
                return false;
            }
        });
    }

    @Override
    public void render () {
        world.step(Gdx.graphics.getDeltaTime(), 6, 6);
        digital camera.replace();
        ScreenUtils.clear(Shade.BLACK);
        debugRenderer.render(world, digital camera.mixed);
    }

    non-public Vector2 unproject(int screenX, int screenY) {
        digital camera.unproject(unprojectVector.set(screenX, screenY, 1));
        worldTouchPosition.set(unprojectVector.x, unprojectVector.y);
        return worldTouchPosition;
    }
}

Related Articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles