我有一个围绕其中心点旋转的精灵(矩形)。我用过:
setOriginCenter();
setRotation(angleDegrees);
工作正常。
我使用CircleShape定义了一个DynamicBody(box2d)。
在精灵的更新方法中,我想将精灵(旋转的精灵)居中与DynamicBody的中心。 我尝试了几种方法,但我无法得到解决方案:
这不起作用
Rectangle rect = myRotatedSprite.getBoundingRectangle();
myRotatedSprite.setPosition(b2body.getPosition().x - rect.getWidth() / 2, b2body.getPosition().y - rect.getHeight() / 2);
这不起作用
myRotatedSprite.setPosition(b2body.getPosition().x - myRotatedSprite.getWidth() / 2, b2body.getPosition().y - myRotatedSprite.getHeight() / 2);
这不起作用
float rot = myRotatedSprite.getRotation();
myRotatedSprite.setRotation(0);
Rectangle rect = getBoundingRectangle();
myRotatedSprite.setPosition(b2body.getPosition().x - rect.width / 2,
b2body.getPosition().y - rect.height / 2);
myRotatedSprite.setRotation(rot);
myRotatedSprite.getBoundingRectangle();
我做错了什么?在我看来,当精灵旋转时getBoundingRectangle()
不正确......
这是我的完整代码:
public class HeroBullet extends Weapon {
private static final String TAG = HeroBullet.class.getName();
private float stateTimer;
private Animation heroBulletAnimation;
private Vector2 tmp; // Temp GC friendly vector
public HeroBullet(PlayScreen screen, float x, float y, float width, float height, float circleShapeRadius, float angle, Animation animation) {
super(screen, x, y, circleShapeRadius > 0 ? circleShapeRadius : Constants.HEROBULLET_CIRCLESHAPE_RADIUS_METERS);
setOriginCenter();
width = width > 0 ? width : Constants.HEROBULLET_WIDTH_METERS;
height = height > 0 ? height : Constants.HEROBULLET_HEIGHT_METERS;
setBounds(getX(), getY(), width, height);
velocity = new Vector2(Constants.HEROBULLET_VELOCITY_X, Constants.HEROBULLET_VELOCITY_Y);
if (angle > 0) {
velocity.rotate(angle);
setRotation(angle);
}
if (animation != null) {
heroBulletAnimation = animation;
} else {
heroBulletAnimation = Assets.instance.heroBullet.heroBulletAnimation;
}
stateTimer = 0;
currentState = State.SHOT;
AudioManager.instance.play(Assets.instance.sounds.heroShoot, 0.2f, MathUtils.random(1.0f, 1.1f));
tmp = new Vector2();
}
@Override
protected void defineWeapon() {
BodyDef bdef = new BodyDef();
bdef.position.set(getX(), getY()); // In b2box the origin is at the center of the body
bdef.type = BodyDef.BodyType.DynamicBody;
b2body = world.createBody(bdef);
FixtureDef fdef = new FixtureDef();
CircleShape shape = new CircleShape();
shape.setRadius(circleShapeRadius);
fdef.filter.categoryBits = Constants.HERO_WEAPON_BIT; // Depicts what this fixture is
fdef.filter.maskBits = Constants.BORDERS_BIT |
Constants.OBSTACLE_BIT |
Constants.POWERBOX_BIT |
Constants.FINAL_ENEMY_LEVEL_ONE_BIT |
Constants.ENEMY_BIT; // Depicts what this Fixture can collide with
fdef.shape = shape;
b2body.createFixture(fdef).setUserData(this);
}
@Override
public void update(float dt) {
switch (currentState) {
case SHOT:
stateShot(dt);
break;
case ONTARGET:
stateOnTarget();
break;
case FINISHED:
break;
default:
break;
}
super.checkBoundaries();
}
private void stateShot(float dt) {
b2body.setLinearVelocity(velocity);
/* ***********
Here I want to center the sprite (rotated sprite) with the center of the DynamicBody, but I don't know how
******** */
setRegion((TextureRegion) heroBulletAnimation.getKeyFrame(stateTimer, true));
stateTimer += dt;
}
private void stateOnTarget() {
world.destroyBody(b2body);
currentState = State.FINISHED;
}
@Override
public void renderDebug(ShapeRenderer shapeRenderer) {
shapeRenderer.rect(getBoundingRectangle().x, getBoundingRectangle().y, getBoundingRectangle().width, getBoundingRectangle().height);
}
@Override
public void onTarget() {
currentState = State.ONTARGET;
}
public void draw(Batch batch) {
if (currentState == State.SHOT) {
super.draw(batch);
}
}
}