1

I had to adjust my scene coordinates to find useful gravity for my objects. So I decided to reduce the screen coordinates so that the gravity behavior on my objects was getting ok. But I have a problem right now, is that when I type sprite.setPosition(0,0), the sprite seems doesn't appear in the right place.

Here is my code:

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:

Image:

Oksygen
  • 11
  • 3

1 Answers1

0

I think the issue is the camera and viewport not a rendered position issue. Your code to set the camera and viewport can be changed. If you are using viewports -You- decide the coordinate range for rendering, its arbitary. This rendering is -then- scaled as you like to fit the screen. In your case you get the width and height of the device (which is not constant across devices even aspect ratio changes) before setting your own viewport parameters? So try instead

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

Read this as well

How Camera works in Libgdx and together with Viewport

(Also you didn't add the offset to y here.

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

)

londonBadger
  • 611
  • 2
  • 5
  • 5