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:
To make the bullets render from the correct place
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!