Tracing a bullet's path?

Started by
6 comments, last by JohnBolton 17 years ago
Hello everyone! Here goes a physics question for you peoples. Based on fire power and angle of the gun shooting, how can I trace a smooth path for the bullet to travel? Easy? Hard? In the middle? Thanks!
Advertisement
Very easy,

http://en.wikipedia.org/wiki/Trajectory_of_a_projectile
I don't wanna sound like a complete noob, but isn't there something easier than that? I don't understand most of the symbols and variables and stuff...

I could be programmer all you want, but I'm a complete noob at advanced math. [wow]

Do you know of anything easier? Thanks!
Rasterman's link is sound.

what is it exactly that you want? if you want the direction or 'slope' of the bullet then that should be simple even with minimal math skills.


otherwise, you'll have to be more specific. for example, if you're wanting to know what the bullet will strike first, it is entirely engine dependant.
Quote:Original post by Leo28C
I don't wanna sound like a complete noob, but isn't there something easier than that? I don't understand most of the symbols and variables and stuff...


Right, which is why "the symbols and variables and stuff" are explained in the article. Now if you actually have a specific question about the article and/or implementation, we'd love to help, but unfortunately there is no magic solution.
Well, what I don't get is how to implement those formulas in my program. I have this:

	if bullet.active then		bullet.x = bullet.x + 1				bullet.speed = math.abs(math.sqrt(math.pow(bullet.speed,			2) - 2 * 9.81 * bullet.x * math.tan(tanks[1].angle) +			math.pow((9.81 * bullet.x) / bullet.speed *			math.cos(tanks[1].angle), 2)))				bullet.y = bullet.x * math.tan(tanks[1].angle) -			(9.81 * math.pow(bullet.x, 2)) / math.pow(2 *			(bullet.speed * math.cos(tanks[1].angle)), 2) +			bullet.y	end


However the bullet goes blazing fast to the top of the screen and my program crashes. I can't even see the bullet; I put a sleep(1000) at the end of that code and it increased by, say, 75 pixels per go...

I'm pretty sure I copied the formula right. Except;

1. Is 'v' the power of the launch?
2. Do I have to update 'yo' after each go, or does it stay at the position of the gun?
3. How do I do 'd'?

I don't get in what order I do everything neither.

Thanks all for helping me with this!
Hmm... I don't know if this has any drawbacks; but it seems to be working:

	if bullet.active then		bullet.x = bullet.x + bullet.xSpeed		bullet.y = bullet.y - bullet.ySpeed				bullet.xSpeed = bullet.xSpeed - .000001		bullet.ySpeed = bullet.ySpeed - .075	end
Quote:Original post by Leo28C
I'm pretty sure I copied the formula right. Except;

1. Is 'v' the power of the launch?
2. Do I have to update 'yo' after each go, or does it stay at the position of the gun?
3. How do I do 'd'?

I don't get in what order I do everything neither.

Thanks all for helping me with this!


The wikipedia article is poorly written, so don't feel bad if you find it confusing. The formula you are using will work, but your implementation has some errors. First, to answer your questions:
1. v is the initial speed of the bullet. Its value never changes.
2. y0 is the initial height of the bullet. Its value never changes.
3. d is the distance the bullet has traveled. It is the same as your bullet.x, but it is not used in the equation you are using so don't worry about it.

Now, the problems in your code:
First, 'v' is initial speed of the bullet, its value is not supposed to change. The good news is this will simplify your code.
Second, make sure your angles are in the correct units. A common mistake is to use degrees when you should be using radians.

Now here is the bad news.
9.81 is the acceleration due to gravity in terms of meters and seconds, but you are using different units -- pixels and frames. Since you are not using meters and seconds as your units, 9.81 is not the right value for 'g' and you must figure out what the right value is. In order to get the value right, you need to figure out the conversions between pixels and meters and seconds and frames.

A better idea would to change over to using meters and seconds, but that might be a lot of work. A quicker way to get it working would be to simply adjust the 9.81 to a different value until the path looks the way you want it to look. With the angle set to 45°, try starting with 'g' = 0.01 instead of 9.81, and adjust it until path looks good.

Edit: Forgot something

In your code, "bullet.x = bullet.x + 1" means that the speed of the bullet in the X direction is 1 pixel per frame. The initial speed of the bullet is then:
     v = 1 / math.cos(tanks[1].angle) 


[Edited by - JohnBolton on April 8, 2007 1:32:53 PM]
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement