简体   繁体   中英

LibGdx Why does sprite draw() method change my X and Y

I have been debugging my LibGdx game for the last couple of hours trying to figure out why some sprites were moving around and some not. I basically have a LittleMan class that is the player class and the sprite moves along nice when i move my player. Then i have another class Bullet that represents bullets but the bullets i shoot don't render correctly. I try to draw them starting from the position of the player but it always draws them from another fixed point on the map. (starting position of the player)

Now here comes the weird part; i debugged my app to try to understand where the movement of the player was coming from and strangely the X and Y coords change every time i call super.draw(batch). I don't understand how or why this happens. Its fine because it works but i want to know how it works so i can use it for my bullets.

Here is some code to clarify:

LittleMan.java :

private Texture littleMan;
private TextureRegion stand;
public LittleMan(Texture littleMan, World world) {
    super(new Sprite(littleMan));
    speed = 1;
    pv=50;
    stamina = 100;
    power = 1;
    defense = 1;
    this.world = world;
    defineMario();

    this.littleMan = littleMan;
    stand = new TextureRegion(getTexture(), 39, 21);
    setBounds(0,0,39,21);
    setRegion(stand);
}

public void update(float dt){

    setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2);
}

public void defineMario() {
    BodyDef bdef = new BodyDef();
    bdef.position.set(32/ FacultyWars.PPM, 32/ FacultyWars.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(30/ FacultyWars.PPM);

    fdef.shape = shape;
    b2body.createFixture(fdef);
}


@Override
public void draw(Batch batch){
    update(Gdx.graphics.getDeltaTime());
    super.draw(batch); //this is the line the coords change, tested with debugger and getY() and getX()

}

Bullet.java:

 private int bulletSpd;//speed
private int bulletRng;//range
private int dmg;//damage
//texture est déja dans sprite
private float rotation;
public World world;
public Body b2body;


public Bullet(Sprite sprite, int speed,int range, int dmg,float x, float y ,float rotation, World world){
    super(sprite);
    bulletRng=range;
    bulletSpd=speed;
    this.dmg=dmg;
    this.rotation=rotation;
    this.world = world;
    defineBullet(x,y);

}

public void defineBullet(float x, float y) {
    BodyDef bdef = new BodyDef();
    bdef.position.set(x/ FacultyWars.PPM, y/ FacultyWars.PPM);
    bdef.type = BodyDef.BodyType.DynamicBody;
    b2body = world.createBody(bdef);

    FixtureDef fdef = new FixtureDef();
    CircleShape shape = new CircleShape();
    shape.setRadius(30/ FacultyWars.PPM);

    fdef.shape = shape;
    b2body.createFixture(fdef);
}

@Override
public void draw(Batch batch){
    update(Gdx.graphics.getDeltaTime());
    super.draw(batch);
}

public void update(float dt){
    setPosition(b2body.getPosition().x - getWidth()/2, b2body.getPosition().y - getHeight()/2);
}

A Bullet object is created using this code:

    float rotation = littleMan.getRotation();
    float x =littleMan.b2body.getPosition().x;
    float y =littleMan.b2body.getPosition().y;
    Bullet bullet = new Bullet(new Sprite(new Texture("bullet.png")), 30, 500, 1, x, y, rotation,world);

The LittleMan class was made by a fellow student but he can't explain to me how it works either so i tried copying it the best i could but i really cannot figure out how:

  1. To make the bullets render from the correct place

  2. Why on earth the draw() method changes the x and y coords

Can anyone help me and explain what is going on or what i am doing wrong?

All help is greatly appreciated!

  1. To make the bullets render from the correct place

What is the "correct place"? You explicitly set it to littleman's position:

float x = littleMan.b2body.getPosition().x;
float y = littleMan.b2body.getPosition().y;

I try to draw them starting from the position of the player but it always draws them from another fixed point on the map. (starting position of the player)

You want them on the players position but they are rendered on the players position?

  1. Why on earth the draw() method changes the x and y coords

In your update method, you set the position to the world position. To answer that question you have to check if the world coordinates of your object alters. I guess that the following happens: You create two objects at the same place (bullet and littleman), they collide in the world and are pushed apart. Because of this they slowly drift away from each other.

Maybe the solution would be to filter out collisions between your littleman and his bullets using collisionBits or collisionGroups.

I think there is problem in conversion (Pixel to meter)

float x = littleMan.b2body.getPosition().x*FacultyWars.PPM;
float y = littleMan.b2body.getPosition().y*FacultyWars.PPM;

Bullet bullet = new Bullet(new Sprite(new Texture("bullet.png")), 30, 500, 1, x, y, rotation,world);

And update() of Bullet should be

public void update(float dt){
    setPosition(b2body.getPosition().x*FacultyWars.PPM - getWidth()/2, b2body.getPosition().y*FacultyWars.PPM - getHeight()/2);
}

And similarly update() of LittleMan should be

public void update(float dt){

    setPosition(b2body.getPosition().x*FacultyWars.PPM - getWidth()/2, b2body.getPosition().y*FacultyWars.PPM - getHeight()/2);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM