Trigonometry question

Started by
5 comments, last by alvaro 13 years, 6 months ago
Hi,

I have a game object (spaceship) that fires 3 bullets, as you can see in the picture. (the draw has been made with paint, but we can supose that the angle of the 2 outside butllets is the same respect the spaceship).

How do I define a bullet trajectory at specific angle like that?

http://yfrog.com/nebulletsjp
Advertisement
To create a direction vector corresponding to an angle (assuming the usual convention):
direction.x = cos(angle);direction.y = sin(angle);
To compute the 'left' and 'right' direction vectors, you should be able to do the same thing, but simply add/subtract an appropriate delta to/from the angle. (There shouldn't be any problems with periodicity in this case.)
If you have a vector pointing in the direction of the middle bullet, you can find the other two by rotating this vector a fixed amount in both directions. These rotations also require computing sine and cosine, but since the angle between the bullets is probably fixed, you only need to compute them once.

You don't even have to use sin/cos to calculate these vectors. You can use the coordinate swapping trick to get two perpendicular vectors if it's 2D, then scale them with a fixed value (0...1.0 means angles between 0°and 45°).

perp_dir.x = -dir.y;
perp_dir.y = dir.x;

So:
new_dir_1.x = dir.x + perp_dir.x * CONSTANT;
new_dir_1.y = dir.y + perp_dir.y * CONSTANT;

new_dir_2.x = dir.x - perp_dir.x * CONSTANT;
new_dir_2.y = dir.y - perp_dir.y * CONSTANT;

EDIT: it's good for small angles, since the speed of the two bullets will be a bit bigger, that the middle one's speed.
Quote:Original post by szecs
You don't even have to use sin/cos to calculate these vectors. You can use the coordinate swapping trick to get two perpendicular vectors if it's 2D, then scale them with a fixed value (0...1.0 means angles between 0°and 45°).

perp_dir.x = -dir.y;
perp_dir.y = dir.x;

So:
new_dir_1.x = dir.x + perp_dir.x * CONSTANT;
new_dir_1.y = dir.y + perp_dir.y * CONSTANT;

new_dir_2.x = dir.x - perp_dir.x * CONSTANT;
new_dir_2.y = dir.y - perp_dir.y * CONSTANT;

EDIT: it's good for small angles, since the speed of the two bullets will be a bit bigger, that the middle one's speed.


curious solution, do you propose this because of it is processed faster?

thanks everyone
I don't propose anything, it's just another possibility, but it has a small issue, as I stated it after the "EDIT" label.
Quote:Original post by szecs
EDIT: it's good for small angles, since the speed of the two bullets will be a bit bigger, that the middle one's speed.


What you suggested is using 1 instead of the cosine of the angle, and setting the sine by hand. And yes, the approximation cos(alpha)=1 is good for small angles.

This topic is closed to new replies.

Advertisement