Sign in to follow this  
jbosch

Trigonometry question

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this