java - libgdx box2d, texture/body not in correct place when created at an angle -
when create body , draw texture body is, if angle set 0.0f, appears expected. upright , @ centre x , y of player (the black circle). when angle changes, x , y of texture appear off. in fact tend off screen can see them visibly fall gravity.
here's method create new bullet:
private void shoot(final float delta) { gametime += delta; final float timesincelastshot = gametime - lastbulletshottime; //5 bullets second, kerpow if (timesincelastshot > 0.2f) { lastbulletshottime = gametime; shot.play(); final float shotx = player.getx() + ((player.getwidth() / 2) - (bullettexture.getwidth() / 2)); final float shoty = player.gety() + ((player.getheight() / 2) - (bullettexture.getheight() / 2)); bodydef bodydef = new bodydef(); bodydef.type = bodydef.bodytype.dynamicbody; bodydef.position.set(shotx, shoty); body body = world.createbody(bodydef); float angle = player.getrotation() * mathutils.degreestoradians; body.settransform(body.getposition().x, body.getposition().y, angle); system.out.println("angle rad: " + angle); bullets.add(body); polygonshape shape = new polygonshape(); shape.setasbox(bullettexture.getwidth() / 2, bullettexture.getheight() / 2); fixturedef fixturedef = new fixturedef(); fixturedef.shape = shape; fixturedef.density = 1f; fixture fixture = body.createfixture(fixturedef); shape.dispose(); } } and here's part of render method loop , draw bullets:
for (body bullet : bullets) { final float x = bullet.getposition().x; final float y = bullet.getposition().y; final float originx = x + (bullettexture.getwidth() / 2); final float originy = y + (bullettexture.getheight() / 2); final float width = bullettexture.getwidth(); final float height = bullettexture.getheight(); final float scalex = 1.0f; final float scaley = 1.0f; final float rotation = bullet.getangle() * mathutils.radianstodegrees; final int sourcex = 0; final int sourcey = 0; final int sourcewidth = bullettexture.gettexturedata().getwidth(); final int sourceheight = bullettexture.gettexturedata().getheight(); final boolean flipx = false; final boolean flipy = false; batch.draw(bullettexture, x, y, originx, originy, width, height, scalex, scaley, rotation, sourcex, sourcey, sourcewidth, sourceheight, flipx, flipy); } any tips i'm doing wrong? want bullets start @ centre of player circle, @ correct angle. intend add velocity 'shoot'.
the full code can found here on github https://github.com/samrufflecoles/playercircle/blob/master/core/src/es/rufflecol/sam/playercircle/playercircle.java
i've attached screenshot, has fallen little gravity between shooting , taking this, bottom left bullets appeared in right place initially, while others have fallen off screen (some others 'shot' never visible).
my mistake originx , originy. corrected below, works:
final float originx = bullettexture.getwidth() / 2; final float originy = bullettexture.getheight() / 2; 
Comments
Post a Comment