Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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); 
    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..??

share|improve this question
3  
Please post the relevant source code snippets. –  Marton Jul 9 '12 at 12:55
2  
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
add comment

2 Answers 2

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.

share|improve this answer
    
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
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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