0
\$\begingroup\$

I'm trying to make a sprite object that has physics, but once the game runs the physics body is drawn at an unknown offset from the sprite, which varies based on the size of the sprites texture. Screen cap from the game

Physics Sprite class:

public class PhysicsSprite extends Sprite {
World world;
public Body body;

public PhysicsSprite(Texture texture, World PhysicsWorld, float x, float y){
    super(texture);
    this.world = PhysicsWorld;
    setPosition(x,y);
    setOrigin(getX(),getY());
    InitializeBody();
}

private void InitializeBody(){
    BodyDef bd = new BodyDef();
    bd.position.set((getX())/GameMain.PPM,(getX())/GameMain.PPM);
    bd.type = BodyDef.BodyType.DynamicBody;

    body = world.createBody(bd);

    PolygonShape shape = new PolygonShape();
    shape.setAsBox(getWidth()/2f/GameMain.PPM,getHeight()/2f/GameMain.PPM);

    FixtureDef fd = new FixtureDef();
    fd.density = 1;
    fd.shape = shape;
    Fixture fixture = body.createFixture(fd);

    System.out.println(body);
}

private void updateSprite(){
    setPosition(body.getPosition().x*GameMain.PPM,body.getPosition().y*GameMain.PPM);
    setRotation((float)Math.toDegrees((double) body.getAngle()));
}

@Override
public void draw(Batch batch){
    updateSprite();
    super.draw(batch);
}
}

physics world/rendering scene class:

public class TestLevel implements Screen {
World world = new World(new Vector2(0f,0f),true);

OrthographicCamera camera = new OrthographicCamera(GameMain.WIDTH/GameMain.PPM, GameMain.HEIGHT/GameMain.PPM);
Box2DDebugRenderer debugR = new Box2DDebugRenderer();
PhysicsSprite s;
GameMain game;
SpriteBatch batch;

public TestLevel(GameMain g){
    game = g;
    batch = g.batch;
    s = new PhysicsSprite(new Texture("badlogic.jpg"),world,300,500);
}
@Override
public void show() {

}

@Override
public void render(float delta) {

    Gdx.gl.glClearColor(.4f,0.4f,.4f,1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.begin();
    s.draw(batch);
    batch.end();

    debugR.render(world,camera.combined);
    world.step(Gdx.graphics.getDeltaTime(),6,2);
}

@Override
public void resize(int width, int height) {

}

@Override
public void pause() {

}

@Override
public void resume() {

}

@Override
public void hide() {

}

@Override
public void dispose() {

}
}

I've followed this tutorial and he mentions the sprite being offset but nothing occurs to him like whats happening here. I've tried to find someone with a similar issue but failed. Can someone help me out?

\$\endgroup\$
0
\$\begingroup\$
 BodyDef bodyDef = new BodyDef();

float offsetX =  ((getX() +   ( getWidth() / 2 ) ) / GameInfo.PPM ) ;
float offsetY =  ((getY()  ) / GameInfo.PPM ) ;

bodyDef.position.set( offsetX ,offsetY) ;
// bodyDef.fixedRotation = true ;

bodyDef.type = BodyDef.BodyType.KinematicBody ;
body = game.getWorld().createBody(bodyDef) ;


PolygonShape shape = new PolygonShape();

float boxWidth = ((getWidth() / 2) / GameInfo.PPM ) ;
float boxHeight = (((getHeight() ) / 2)  / GameInfo.PPM ) ;

shape.setAsBox(boxWidth ,boxHeight );
//shape.setRadius(0.5f);

FixtureDef fixtureDef = new FixtureDef();
fixtureDef.density  =2f ;
fixtureDef.friction  =0.5f ;
fixtureDef.shape = shape ;
fixtureDef.isSensor =true;
Fixture fixture =  body.createFixture(fixtureDef) ;
fixture.setUserData(this);
shape.dispose();
\$\endgroup\$
2
  • \$\begingroup\$ the problem is differences between coordinate systems for libgdx & box2d libgdx 0,0 object origin is bottom right, the box2d 0,0 is the middle of object . \$\endgroup\$
    – Realbitt
    May 10 '18 at 19:07
  • 1
    \$\begingroup\$ Welcome to the site Realbitt. While the code you have given might solve the problem in the question, providing some context would be better for future visitors. \$\endgroup\$ May 10 '18 at 19:37
0
\$\begingroup\$

I’ve figured it out. I needed to offset the body position down by half the screen height + half the sprite height and left by half the screen width + half the sprite width. And then do the opposite to the sprite position in the update sprite method.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.