I created a racing game in andengine.There is a player which is size 100x30px,like a rectangle.And there is fire() function to shoot bullet, code is below.It works fine.but problem is when player is rotating.player is a car image.so they rotating by directionButton().When I shoot a bullet,it starting 0x0px of car.But i want it must be start to move in front of car like 100x15px of car.How can I do?
public void fire() {
float startBulletX=player.getX();
float startBulletY=player.getY();
Sprite bullet=new Sprite(startBulletX,startBulletY,bulletRegion);
final float xComp = (float) Math.cos(MathUtils.degToRad(this.player.getRotation() + 90));
final float yComp = (float) Math.sin(MathUtils.degToRad(this.player.getRotation() + 90));
final Vector2 velocity = Vector2Pool.obtain(xComp * 10, yComp * 10);
bullet.setRotation(this.player.getRotation());
final FixtureDef bulletFixtureDef1 = PhysicsFactory.createFixtureDef(0, 0, 0);
this.mBulletBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, bullet, BodyType.KinematicBody, bulletFixtureDef1);
this.mBulletBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(bullet, this.mBulletBody, true, false));
this.scene.attachChild(bullet);
}
//on screen control button
public void addDirectionButton() {
player = new AnimatedSprite(centerX, centerY, this.playerTextureRegion);
final AnalogOnScreenControl analogOnScreenControl = new AnalogOnScreenControl(0, CAMERA_HEIGHT - this.mOnScreenControlBaseTextureRegion.getHeight(), this.mBoundChaseCamera, this.mOnScreenControlBaseTextureRegion, this.mOnScreenControlKnobTextureRegion, 0.1f, 200, new IAnalogOnScreenControlListener() {
public void onControlChange(final BaseOnScreenControl pBaseOnScreenControl, final float pValueX, final float pValueY) {
final Body carBody = DeffenseActivity.this.mCarBody;
final Vector2 velocity = Vector2Pool.obtain(pValueX * 5, pValueY * 5);
carBody.setLinearVelocity(velocity);
Vector2Pool.recycle(velocity);
final float rotationInRad = (float)Math.atan2(-pValueX, pValueY);
player.setRotation(MathUtils.radToDeg(rotationInRad));
}
public void onControlClick(final AnalogOnScreenControl pAnalogOnScreenControl) {
}
});
analogOnScreenControl.getControlBase().setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
analogOnScreenControl.getControlBase().setAlpha(0.5f);
analogOnScreenControl.getControlBase().setScaleCenter(0, 128);
analogOnScreenControl.getControlBase().setScale(1.25f);
analogOnScreenControl.getControlKnob().setScale(1.25f);
analogOnScreenControl.refreshControlKnobPosition();
this.scene.setChildScene(analogOnScreenControl);
this.mBoundChaseCamera.setChaseEntity(player);
a=new Sprite(0,0,bulletRegion);
player.attachChild(a);
final FixtureDef carFixtureDef = PhysicsFactory.createFixtureDef(1, 0.5f, 0.5f);
this.mCarBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, player, BodyType.DynamicBody, carFixtureDef);
this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(player, this.mCarBody, true, false));
this.scene.attachChild(player);
}