对象坐标未出现在正确的位置

时间:2020-08-16 23:18:44

标签: java libgdx box2d game-physics

我必须调整场景坐标才能找到有用的重力。 因此,我决定减少屏幕坐标,以使物体上的重力行为正常。 但是我现在有一个问题,就是当我键入sprite.setPosition(0,0)时,精灵似乎没有出现在正确的位置。

这是我的代码:

package com.mygdx.game;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.World;

public class MyGdxGame extends ApplicationAdapter {
    public OrthographicCamera camera;
    Sound sound;

    BitmapFont font,font2;
    SpriteBatch batch;
    Texture img1,img2,img3,img4,img5,img6;
    Sprite sprite1,sprite2,sprite3;
    World world;
    Body solKose,sagKose,ustKose,altKose,body1,body2,body3;
    int width=0;
    float axes=0;

    private int enemyGoalCounter, playerGoalCounter=0;
    Sphere _player;
    Sphere _enemyPlayer;
    Sphere ball;

    Vector2 directionVector;
    Vector3 touchCoordinate;

    int direction=-1;
    @Override
    public void create () {
        camera = new OrthographicCamera(Gdx.graphics.getWidth(),Gdx.graphics.getHeight());
        camera.viewportWidth = Gdx.graphics.getWidth()/100;
        camera.viewportHeight = Gdx.graphics.getHeight()/100;
        camera.update();

        font = new BitmapFont();
        font2 = new BitmapFont();
        font.setColor(Color.RED);
        font2.setColor(Color.BLUE);
        touchCoordinate = new Vector3(0,0,0);
        sound = Gdx.audio.newSound(Gdx.files.internal("taktak.wav"));

        batch = new SpriteBatch();
        img1 = new Texture("sphere2.png");
        img2 = new Texture("sphere2.png");
        img3 = new Texture("ball.png");

        sprite1 = new Sprite(img1);
        sprite2 = new Sprite(img2);
        sprite3 = new Sprite(img3);
        sprite1.setPosition(0,0);
        sprite2.setPosition(0,0);

        world = new World(new Vector2(0, 0),true);

        BodyDef bodyDef = new BodyDef();
        bodyDef.type = BodyDef.BodyType.DynamicBody;
        bodyDef.position.set(sprite1.getX()/2,sprite1.getY()/2);

        body1 = world.createBody(bodyDef);
        CircleShape shape = new CircleShape();
        shape.setRadius(1f);
        FixtureDef fixtureDef = new FixtureDef();
        fixtureDef.shape = shape;
        fixtureDef.restitution=0.6f;
        body1.createFixture(fixtureDef);
        shape.dispose();

        BodyDef bodyDef2 = new BodyDef();
        bodyDef2.type = BodyDef.BodyType.DynamicBody;
        bodyDef2.position.set(sprite2.getX()/2,sprite2.getY()/2);

        body2 = world.createBody(bodyDef2);
        CircleShape shape2 = new CircleShape();
        shape2.setRadius(1f);
        FixtureDef fixtureDef2 = new FixtureDef();
        fixtureDef2.shape = shape2;
        fixtureDef.restitution=0.6f;
        body2.createFixture(fixtureDef2);
        shape2.dispose();
    }

    @Override
    public void render () {
        System.out.println();
        camera.update();
        play();
        inputController(); // A function for keyboard input controlling
        math(); // For Collision controlling
        world.step(1f/60f, 6, 2);
        sprite1.setPosition(body1.getPosition().x,body1.getPosition().y);
        sprite2.setPosition(body2.getPosition().x,body2.getPosition().y);
        Gdx.gl.glClearColor(0, 0, 0, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        batch.setProjectionMatrix(camera.combined);

        batch.begin();
        batch.draw(sprite1,sprite1.getX(),sprite1.getY(),2,2);
        batch.draw(sprite2,sprite2.getX(),sprite2.getY(),2,2);
        batch.end();
    }

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

    void play() {

    }

    void inputController() {
        if(Gdx.input.isTouched()){
            Vector2 directionVector = new Vector2(0,0);
            touchCoordinate.x = Gdx.input.getX();
            touchCoordinate.y = Gdx.input.getY();
            camera.unproject(touchCoordinate);

            double distance = Math.pow(sprite1.getWidth()/2,2)-(Math.pow((sprite1.getOriginX()-touchCoordinate.x),2)+Math.pow((sprite1.getOriginY()-touchCoordinate.y),2));
            System.out.println(touchCoordinate);
            if(distance>0){
                body1.applyForceToCenter(20,20,true);
            }else if(distance==0){
                body1.applyForceToCenter(20,20,true);
            }else{
                ////
            }
        }
        //_player.setCenterPosition(touchCoordinate.x,touchCoordinate.y);
    }

    void math() {
        goalSystem();
    }

    void restartPositions(){
    }


    void goalSystem() {
        /*
        if(!goal) {
            if(ball.getCenterPosition(ball.position.x, ball.position.y).y < 20) {
                if((ball.getCenterPosition(ball.position.x, ball.position.y).x >= Gdx.graphics.getWidth()/2-325/2) && (ball.getCenterPosition(ball.position.x, ball.position.y).x <= Gdx.graphics.getWidth()/2+325/2)) {
                    goal=true;
                    enemyGoalCounter++;
                    restartPositions();
                }
            }
            if(ball.getCenterPosition(ball.position.x, ball.position.y).y > Gdx.graphics.getHeight()-20) {
                if((ball.getCenterPosition(ball.position.x, ball.position.y).x >= Gdx.graphics.getWidth()/2-325/2) && (ball.getCenterPosition(ball.position.x, ball.position.y).x <= Gdx.graphics.getWidth()/2+325/2)) {
                    goal=true;
                    playerGoalCounter++;
                    restartPositions();
                }
            }
        }
        */
    }
    public class Sphere{
        Vector2 firstPos,firstDirection;
        float firstSpeed;
        Vector2 position, direction,centerPosition;
        float x,y,width,height;
        float radius;
        float xCenter,yCenter;
        float speed;
        float velocity;
        public Sphere(float x, float y, float radius, float width, float height) {
            this.x = x;
            this.y = y;
            this.radius = radius;
            this.width = width;
            this.height = height;
            this.position = new Vector2(x,y);
            this.direction = new Vector2(x,y);
            this.speed = 0;
            this.firstSpeed = speed;

            this.firstPos = new Vector2(x,y);
            this.firstDirection = new Vector2(x,y);
        }
        Vector2 getCenterPosition(float x, float y) {
            centerPosition = new Vector2(x+width/2,y);
            return centerPosition;
        }
        void setCenterPosition(float x, float y){
            position.x = x-width/2;
            position.y = y-height/2;
        }
        void setSpeed(float speed) {
            this.speed = speed;
        }
        float getSpeed() {
            return this.speed;
        }
    }
}

图片:

Image:

1 个答案:

答案 0 :(得分:0)

我认为问题是相机和视口,而不是渲染位置问题。设置摄像机和视口的代码可以更改。如果使用视口,请-确定渲染的坐标范围,该坐标范围是任意的。然后根据需要缩放此渲染以适合屏幕。在您自己的情况下,您在设置自己的视口参数之前获得了设备的宽度和高度(即使纵横比变化,跨设备也不是恒定的)? 所以试试吧

camera = new OrthographicCamera(100, 500); //You pick as you like 
camera.position.set(camera.viewportWidth / 2, camera.viewportHeight / 2, 0); //centre camera
viewport = new FitViewport(camera.viewportWidth, camera.viewportHeight, camera);//stretch proportionally

也请阅读

How Camera works in Libgdx and together with Viewport

(而且您也没有在y处添加偏移量。

Vector2 getCenterPosition(float x, float y) {
    centerPosition = new Vector2(x+width/2,y);
    ....

相关问题