# Help! Bullet angle's calculation

This topic is 2891 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I want to make the character shoot to the enemy automatically. Now, I am calculating the bullet position. I think to use sin-cos to calculate but it is not work well. the angle base on my position(hero) and enemy's position.
bullet[0].x += cos(D3DXToRadian(hero.x-enemy[0].x)) * 5 ;
bullet[0].y += sin(D3DXToRadian(hero.y-enemy[0].y)) * 5 ;


base on DirectX programme that D3DXToRadian(angle) will change the angle to radian. the bullet must move automatically towards to enemy like missle. can anybody help?

##### Share on other sites
Hi,

you don't need to use radians or any trigonometry. I'd suggest implementing a simple 2d vector class first. Then the rest will be piece of cake.

bullet.position = hero.position;bullet.delta = enemy.position - hero.position; //calculate the direction to the enemynormalize(delta); //to make the delta a unit vectorbullet.delta *= speed; //multiply the delta vector by desired projectile speed.//and now for every frame you can update the bullets position with the direction vector scaled with delta time between this and the previous update.bullet.position += bullet.delta * frametimedelta;

I'd really look for the 2d vector class since it makes your life much easier.

Good luck!

##### Share on other sites
Quote:
 Original post by kaunaHi,you don't need to use radians or any trigonometry. I'd suggest implementing a simple 2d vector class first. Then the rest will be piece of cake.*** Source Snippet Removed ***I'd really look for the 2d vector class since it makes your life much easier. Good luck!

but i don't understand this function.

"normalize(delta);"

how can you get it??
2d vector class??
what the name of the header should i use?

##### Share on other sites
Quote:
 how can you get it??2d vector class??what the name of the header should i use?
You have to write the code yourself, or use an existing math library.

Since you're using DirectX, one option would be to use the DX math library and just ignore z.

If you're unfamiliar with vector math in general, try Googling 'vector math tutorial'.

##### Share on other sites

Hi,

You can well do without a 2d vector class (as I did for a long time). It was just a tip to make your code more readable and possibly error free.

//if the vector2d class looks something like thisclass Vector2d{public:   float x,y;};//then the normalize function would be:void normalize(Vector2d &vec){   float Length = sqrtf(vec.x * vec.x + vec.y * vec.y); //calculate the vector length   if(Length > 0.00000001f)   {      vec.x /= Length;      vec.y /= Length;   }}//after normalizing the vector the length of the vector is 1, which makes it more useful for calculations.

##### Share on other sites
Quote:
 Original post by kaunaHi,You can well do without a 2d vector class (as I did for a long time). It was just a tip to make your code more readable and possibly error free. *** Source Snippet Removed ***

however, i don't get it.
i try to search on internet and i still confuse
i am not sure that 2d vector is what i want or not
void AutoBullet(void){	bullet[0].position = enemy[0].position - hero.position;	normalize(&bullet[0].position);	bullet[0].position += bullet[0].position*5;}void normalize(D3DXVECTOR3 *vec){   float Length = sqrtf(vec->x * vec->x + vec->y * vec->y); //calculate the vector length   if(Length > 0.00000001f)   {      vec->x /= Length;      vec->y /= Length;   }}

i just want the bullet point to and fire at wherever enemy is.

##### Share on other sites
Quote:
 Original post by kaunaHi,You can well do without a 2d vector class (as I did for a long time). It was just a tip to make your code more readable and possibly error free. *** Source Snippet Removed ***

however, i don't get it.
i try to search on internet and i still confuse
i am not sure that 2d vector is what i want or not
void AutoBullet(void){	bullet[0].position = enemy[0].position - hero.position;	normalize(&bullet[0].position);	bullet[0].position += bullet[0].position*5;}void normalize(D3DXVECTOR3 *vec){   float Length = sqrtf(vec->x * vec->x + vec->y * vec->y); //calculate the vector length   if(Length > 0.00000001f)   {      vec->x /= Length;      vec->y /= Length;   }}

i just want the bullet point to and fire at wherever enemy is.

##### Share on other sites
Quote:
 thanks for your advice.however, i don't get it.i try to search on internet and i still confusei am not sure that 2d vector is what i want or not*** Source Snippet Removed ***
The DX math library already provides a 'normalize' function; you don't have to write it yourself. (Also, your function treats the 3-d input vector as a 2-d vector, which may lead to incorrect results.)

And yes, a 2-d vector is what you want. Although you can do this sort of thing without using vectors explicitly, there's no reason to do so; it just makes everything more difficult than it would be otherwise.

Did you try Googling for 'vector math tutorial'? Here is the first hit; I haven't read it, but it looks fairly promising. I'm sure you can find other similar tutorials online as well.
Quote:
 i just want the bullet point to and fire at wherever enemy is.
Check out kauna's first reply; it include pseudocode that shows how to do this.