# How to fire a bullet in a specific direction?

I am developing an Android game. I have problem with bullet firing. It's a space ship that has to fire bullets but right now it's firing in a random direction. I have to fire a bullet to the enemy from the only one point on the nose of the ship. Right now the bullets fire sometimes from the tailpart or other. So that's a problem. How do I give a bullet direction and how to fire it from only the head of my space ship?

``````private void fireBullet()
{
float plusX = 16;
float plusY = plusX * mPlayer.getRotationValue();
Bullet mBullet = new Bullet(mPlayer.getX()-plusX, mPlayer.getY()+plusY, 8, 8, this.mGameController.mBulletTextureRegion, this.mPhysicsWorld, mPlayer);
//System.out.println("size is :" + bulletList.size());
mSoundManager.playGunShot();
}
``````

What is defect here?? it does not fire bullet from ship's nose?? What can be done..??

-
Please post the relevant source code snippets. – Marton Jul 9 '12 at 12:55
Basically, what you're asking is for the definition of a Vector and how to normalize the vector. That's it, an answer would/should contain this information coded in a sympathetic programming language.. which is really not how a question should be stated around here. – teodron Jul 9 '12 at 14:19

You would need to have its position start at the ship's nose, and give it an x velocity proportional to the cosine of the angle you want to fire it at and a y velocity proportional to the sine of that same angle. You would multiply those values by the linear velocity that you want the bullet to have.

-
how to find the ship's nose position value.?? – Mike Jul 11 '12 at 7:13
private void fireBullet(){ float plusX = 16; float plusY = plusX * mPlayer.getRotationValue(); Bullet mBullet = new Bullet(mPlayer.getX()-plusX, mPlayer.getY()+plusY, 8, 8, this.mGameController.mBulletTextureRegion, this.mPhysicsWorld, mPlayer); mBulletList.add(mBullet); //System.out.println("size is :" + bulletList.size()); mScene.getTopLayer().addEntity(mBullet.getSprite()); mSoundManager.playGunShot(); } What is defect here?? it does not fire bullet from ship's nose?? What can be done..?? – Mike Jul 11 '12 at 7:38

You can fire the bullet from the ship front face, if you have camera behind the ship than you have to cast a ray in the opposite direction of the camera and the position of the bullet creation is the position of the ship.

you can achieve the position of the bullet by this

``````bullet.position.x = ship.position.x;
``````

You just need a reference point by which you can cast the ray.

-