box2d + scene2d strange behavior

Anything libgdx related goes here!

box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 2:56 pm

I'm trying to make my player appear at the coordinate point of a finger click
but I get unexpected behavior: the Y coordinate is inverted relative to the middle. That is, I click at the top of the screen, and my player appears at the bottom.


Code: Select all
public class GameView extends GameState implements Screen, GestureDetector.GestureListener, InputProcessor {

    private float CAMERA_WIDTH;
    private float CAMERA_HEIGHT;
    private float ASPECT_RATIO;
    private Viewport viewPort;

    private long toched;

    private World world;
    private Player player;
    private Ball ball;

    private Body rect;
    private Box2DDebugRenderer rend;

    private Stage stage;

    public GameView(GameStateManager gsm) {
        super(gsm);
        CAMERA_WIDTH = game.getWeight();
        CAMERA_HEIGHT = game.getHeight();
        ASPECT_RATIO = gsm.getWeight() / gsm.getHeight();
        viewPort = new FillViewport(CAMERA_WIDTH,CAMERA_HEIGHT);
        this.camera.setToOrtho(true, CAMERA_WIDTH, CAMERA_WIDTH / ASPECT_RATIO);
        this.camera.position.set(new Vector3(CAMERA_WIDTH / 2f, (CAMERA_WIDTH / ASPECT_RATIO) / 2f, 0));
    }

    @Override//вызывается при переключении на этот экран
    public void show() {

        world = new World(new Vector2(0, -100), true);
        player = new Player(world);
        ball = new Ball(world);
        rend =  new  Box2DDebugRenderer ();

        stage = new Stage(viewPort);
        stage.addActor(player);
        stage.addActor(player);
        stage.setDebugAll(true);


        InputMultiplexer im = new InputMultiplexer();
        GestureDetector gd = new GestureDetector(this);
        im.addProcessor(stage);
        im.addProcessor(gd);
        im.addProcessor(this);
        Gdx.input.setInputProcessor(im);
    }

    @Override// delta - время между кадрами в секунду
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        world.step(1/60f, 6,2);//в идеале делить на FPS
        stage.act(delta);
        rend.render(world,stage.getCamera().combined);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewPort.update(width, height,true);
    }
    @Override
    public void pause() {

    }
    @Override
    public void resume() {

    }
    @Override
    public void hide() {

    }
    @Override
    public void dispose() {
        world.dispose();
        stage.dispose();
    }



    @Override
    public boolean keyDown(int keycode) {
        return false;
    }
    @Override
    public boolean keyUp(int keycode) {
        return false;
    }
    @Override
    public boolean keyTyped(char character) {
        return false;
    }
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        if (toched + 200 > System.currentTimeMillis()) {
            gsm.setState(0);
        } else {
            toched = System.currentTimeMillis();
        }
        return true;
    }

    @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 mouseMoved(int screenX, int screenY) {
        return false;
    }
    @Override
    public boolean scrolled(int amount) {
        return false;
    }
    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        return false;
    }
    @Override
    public boolean tap(float x, float y, int count, int button) {
        player.setPosition(x, y);
        return true;
    }
    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        return false;
    }
    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        return false;
    }
    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        return false;
    }
    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }
    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }
    @Override
    public void pinchStop() {

    }

}


Code: Select all
public class Player extends ActorObject {


    public Player(World world) {
        super(world);
        setPosition(320/2 - this.getWidth()/2, 480/3);
        PolygonShape shape = new PolygonShape();
        shape.setAsBox(64, 8);
        createBody(shape, BodyDef.BodyType.KinematicBody);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        body.setTransform(getX(), getY(), 0);
        super.draw(batch, parentAlpha);
    }
}


Code: Select all
public class ActorObject extends Actor {

    protected Sprite sprite;
    protected Body body;
    private World world;

    public ActorObject(World world){
        this.world = world;
    }

    protected void createBody(Shape shape, BodyDef.BodyType type){
        BodyDef bDef = new BodyDef();
        bDef.position.set(getX(), getY());
        bDef.type = type;

        world.createBody(bDef);
        body = world.createBody(bDef);

        FixtureDef fDef = new FixtureDef();
        fDef.shape = shape;
        fDef.restitution = 1;
        fDef.density = 500; //килограм

        body.createFixture(fDef);
    }

}
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d + scene2d strange behavior

Postby shatterblast » Thu Aug 29, 2019 3:30 pm

In your Player class, does the following work normally if you manually put in X and Y stuff?
Code: Select all
setPosition();


And in your ActorObject class, does the following also work the way it should if you put in X and Y stuff yourself?
Code: Select all
bDef.position.set();
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 3:55 pm

at the beginning of the game, my actor appears where I expect
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 4:02 pm

I add the line player.setPosition (player.getX (), player.getY () + delta); in the render method and my player moves smoothly upwards I cannot understand
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d + scene2d strange behavior

Postby shatterblast » Thu Aug 29, 2019 4:09 pm

I assume that means createBody() in your ActorObject class works fine then.

If you have any Scene2D things, like Button, do those respond fine?
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 4:20 pm

I have bounds on the ball object, the object behaves as expected, it just falls from a height. the circle is inscribed in a square everything is as it should

Code: Select all
public class Ball extends ActorObject {


    public Ball(World world) {
        super(world);
        setBounds(320/2 - this.getWidth()/2, 450, 32, 32);
        CircleShape shape = new CircleShape();
        shape.setRadius(16);
        createBody(shape, BodyDef.BodyType.DynamicBody);

    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        setPosition(body.getPosition().x-16,body.getPosition().y-16);
    }
}
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d + scene2d strange behavior

Postby shatterblast » Thu Aug 29, 2019 4:33 pm

Right. Your stuff works programmatically, but I wonder if your general device input is off for your game. Scene2D interaction is just one suggestion for testing your game's input.
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 5:00 pm

don't understand your thoughts
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Re: box2d + scene2d strange behavior

Postby shatterblast » Thu Aug 29, 2019 5:20 pm

I wonder if the input does not align properly with the "stage" variable in your GameView class. If not already, you might add a Button through addActor() to "stage" and see if you can click it. You should still see the Button animate when pressed if your input aligns correctly, even without a listener attached to the Button itself.

If you do have a problem, then the issue is probably with your Camera, and if found true, then I think the ASPECT_RATIO variable in your GameView class might make your device input come inaccurately. I suggest temporarily removing the ASPECT_RATIO variable from your Camera code to see what happens in that case.
Last edited by shatterblast on Thu Aug 29, 2019 6:01 pm, edited 1 time in total.
shatterblast
 
Posts: 414
Joined: Sun Jul 06, 2014 1:14 pm

Re: box2d + scene2d strange behavior

Postby TruePlayer » Thu Aug 29, 2019 6:01 pm

I added a button to the code, and I have to say everything works as it should, the button is located in the coordinates, and the listener hitbox is equal to the size of the button
but the player’s body is still moving along mirror Y


I set the yDown camera settings in both states, this is not a result
Code: Select all
public class GameView extends GameState implements Screen, GestureDetector.GestureListener, InputProcessor {

    private float CAMERA_WIDTH;
    private float CAMERA_HEIGHT;
    private float ASPECT_RATIO;
    private Viewport viewPort;

    private long toched;
    private TextButton testB;
    private Skin skin;

    private World world;
    private Player player;
    private Ball ball;

    private Body rect;
    private Box2DDebugRenderer rend;

    private Stage stage;

    public GameView(GameStateManager gsm) {
        super(gsm);
        CAMERA_WIDTH = game.getWeight();
        CAMERA_HEIGHT = game.getHeight();
        ASPECT_RATIO = gsm.getWeight() / gsm.getHeight();
        viewPort = new FillViewport(CAMERA_WIDTH,CAMERA_HEIGHT);
        this.camera.setToOrtho(true, CAMERA_WIDTH, CAMERA_WIDTH / ASPECT_RATIO);
        this.camera.position.set(new Vector3(CAMERA_WIDTH / 2f, (CAMERA_WIDTH / ASPECT_RATIO) / 2f, 0));
    }

    @Override//вызывается при переключении на этот экран
    public void show() {

        world = new World(new Vector2(0, -100), true);
        player = new Player(world);
        ball = new Ball(world);
        rend =  new  Box2DDebugRenderer ();

        skin = new Skin(Gdx.files.internal("skin/star-soldier-ui.json"));

        testB = new TextButton("GAME",skin, "default");
        testB.setBounds(50, 50, 150, 32);
        testB.addListener(new ClickListener(){
            @Override
            public void clicked(InputEvent event, float x, float y){
                gsm.setState(0);
                dispose();
            }
        });

        stage = new Stage(viewPort);
        stage.addActor(testB);
        stage.addActor(player);
        stage.addActor(ball);
        stage.setDebugAll(true);


        InputMultiplexer im = new InputMultiplexer();
        GestureDetector gd = new GestureDetector(this);
        im.addProcessor(stage);
        im.addProcessor(gd);
        im.addProcessor(this);
        Gdx.input.setInputProcessor(im);
    }

    @Override// delta - время между кадрами в секунду
    public void render(float delta) {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        world.step(1/60f, 6,2);//в идеале делить на FPS
        stage.act(delta);
        rend.render(world,viewPort.getCamera().combined);
        stage.draw();
    }

    @Override
    public void resize(int width, int height) {
        viewPort.update(width, height,true);
    }
    @Override
    public void pause() {

    }
    @Override
    public void resume() {

    }
    @Override
    public void hide() {

    }
    @Override
    public void dispose() {
        world.dispose();
        stage.dispose();
    }



    @Override
    public boolean keyDown(int keycode) {
        return false;
    }
    @Override
    public boolean keyUp(int keycode) {
        return false;
    }
    @Override
    public boolean keyTyped(char character) {
        return false;
    }
    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        player.setPosition((float)screenX, (float)screenY);
        if (toched + 200 > System.currentTimeMillis()) {
            gsm.setState(0);
        } else {
            toched = System.currentTimeMillis();
        }
        return true;
    }

    @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 mouseMoved(int screenX, int screenY) {
        return false;
    }
    @Override
    public boolean scrolled(int amount) {
        return false;
    }
    @Override
    public boolean touchDown(float x, float y, int pointer, int button) {
        return false;
    }
    @Override
    public boolean tap(float x, float y, int count, int button) {
        return false;
    }
    @Override
    public boolean longPress(float x, float y) {
        return false;
    }
    @Override
    public boolean fling(float velocityX, float velocityY, int button) {
        return false;
    }
    @Override
    public boolean pan(float x, float y, float deltaX, float deltaY) {
        return false;
    }
    @Override
    public boolean panStop(float x, float y, int pointer, int button) {
        return false;
    }
    @Override
    public boolean zoom(float initialDistance, float distance) {
        return false;
    }
    @Override
    public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
        return false;
    }
    @Override
    public void pinchStop() {

    }
}
Last edited by TruePlayer on Thu Aug 29, 2019 6:14 pm, edited 2 times in total.
TruePlayer
 
Posts: 103
Joined: Sun Jul 28, 2019 12:22 pm

Next

Return to Libgdx

Who is online

Users browsing this forum: diazndc, Google [Bot] and 1 guest