Sign in to follow this  
DevLiquidKnight

Asteroids Bullet Trajectory?

Recommended Posts

I was wondering I have been thinking of making a clone of asteroids. It's simple, I know but I am not quite sure how you go about calculating the bullets movement. I can do movement that is north, south, east, west, northeast, southeast, northwest, southwest. I am not sure how you go about doing the movement for angles in between though. Could someone enlighten me? I haven't worked with 2D in awhile so yeah. If anyone could give me some examples, resources articles. It would be greatly appreciated.

Share this post


Link to post
Share on other sites
How well do you know your trigonometry?

What you have is the heading of your ship, and basically you want to make that into the direction of your velocity, but the length of the velocity vector would be the speed of the bullet.

What language are you using?

In c++, you can use cos(), sin(), atan() and sqrt() to do most of this.

Though it should be noted that the c++ trig functions take as arguments and return radians.

Searching for all of the above should help you on your way.

Share this post


Link to post
Share on other sites
I have never had trignometry, [wink] as a result stuff like that is hard, I tend to have pushed myself pretty far though. I am using C++ though, I know the trigonometry functions just not sure of the calculations/formulas that are used to cause the bullet to go through the trajectory given an angle.

Share this post


Link to post
Share on other sites
Quote:
Original post by DevLiquidKnight
I have never had trignometry, [wink] as a result stuff like that is hard, I tend to have pushed myself pretty far though. I am using C++ though, I know the trigonometry functions just not sure of the calculations/formulas that are used to cause the bullet to go through the trajectory given an angle.


There are several ways to approach this. The one I would use is vector math. Since you haven't used Trig, I will try to explain.

Two vectors
One is the ships position for example( though not a specific vector more like a coordinate) (x,y)

(300, 200)

The second is the ships Direction: (We will call this vector v)
(3,5) In your game this would mean that you are moving 3 x pixels and 5 y pixels every second.

The next thing you want to learn is magnitude which is the length from the start point(in this case is 300, 200)

magnitude = sqrt(3^2+5^2)
It is usually represented as |v|

In an above post it is mentioned that C++ uses radians, and that is correct. 360 degrees is equal to 2(pi) when your working in normalized vectors.

To normalize a vector divide each component of the vector by its magnitude.
So we would have N = v/|v|. It is really easy once you understand why.

Now lets see what we've learned.
Starting poing(300,200)
vector(3,5)
Magnitude = sqrt(3^2 +5^2)=5.83095
So: |v| = 5.83095
Lets normalize the vector
3/5.83095 = .514495
5/5.83095 = .857493

Now we know that N = (0.514495, 0.857493)

So how do you calculate the angle or direction the ship is heading?
Use the right triangle method.

sin = opposite/hypotenuse
cos = adjacent/hypotenuse
tan = opposite/adjacent

I use the saying "Oscar has a hairy old arm" to remember the order.

You know that you have a right triangle with a base of 3 and a height of 5.
To calculate the angle from the starting point. We know that the hypotenuse is equal to the magnitude, but why work with decimals when we don't have to.

Take the tan inverse of (5/3). Although since the vector is normalized N just do this.
tan inverse of (Ny, Nx) returns the number of radians.

Now if you work better in degrees take the 1.03037 radians * (180/PI) which gives you 59.036 degrees.

It is important to learn how to normalize vectors and to work in radians.

The bullet can be created using the normalized vector. Create a line that calculates the bullets position each updated cycle. The next thing is to create a list or queue to store multiple bullets.

Hope this helps, Ask if you need more explaining on anything.
Normailizing really comes in handy when you are using the dot product on two vectors. Which gives the radians between two vectors.




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