How can I incorporate wind into a basic projectile simulation?

In summary, the programmer is trying to create a game where the player can control a projectile that includes gravity and wind. They don't know how to control the wind direction and power, so they are looking for help. They calculate the X and Y position of the bullet by increasing the frameCount by 0.10 each frame, and then add gravity and a force to the X coord to make it appear like there is a lot of wind effecting the projectile.
  • #1
jastanton
4
0
Hey I am making a game with basic a projectile that includes gravity and wind and I don't know how I can control the wind direction and power. Here is what I've got without wind:

Setup:
x = 0 /// x starting position
y = 0 /// y starting position
r = 10 /// radius of projectile
time = 0
g = 9.8 /// Gravity
theta = 45 /// 45 degrees starting angle
v0x = v * cos(theta * Math.PI/180) /// Angle in radians(?) multiplied by velocity
v0y = v * sin(theta * Math.PI/180) /// Angle in radians(?) multiplied by velocity

My method for calculating the X and Y position of the bullet is to increase the frameCount by 0.10 each frame:

BEGIN LOOP
y = y - v0y * time - (1/2 * g * (time^2)) /// "^2" denotes squared
x = x + v0x * time ///
time = time + 0.10 // increase time for the next frame
REPEAT LOOP


=============
So the way I interpret this is that every frame I apply gravity to the Y coord and no forces to the X coord (the force applied to the Y coord is: minus 1/2 * gravity * time squared)

Now I figured out if I apply more gravity and a force to the X coord it will act like wind is effecting the projectile however I don't know the proportion of extra force I should I apply to the X and to the Y to make it appear like there is a lot of wind effecting the projectile in say -45 degree angle.


Can anybody help?
 
Physics news on Phys.org
  • #2
Something like this I think but I could be wayy off base... I'm pretty sleepy.

Code:
struct Projectile
{
	float x;
	float y;

	float velocityX;
	float veloctiyY;

	float mass;
};

struct Wind
{
	float velocityX;
	float velocityY;
};

Projectile	projectile;
Wind		wind;

void RunFrame( float timestep )
{
	// add gravity
	projectile.velocityY += -9.80665f * timestep;

	// add wind
	projectile.velocityX += (wind.velocityX - projectile.velocityX) / projectile.mass * timestep;
	projectile.velocityY += (wind.velocityY - projectile.velocityY) / projectile.mass * timestep;

	// integrate velocity
	projectile.x += 0.5f * projectile.velocityX * timestep * timestep;
	projectile.y += 0.5f * projectile.velocityY * timestep * timestep;

	// draw
}
 
  • #3
Since it's just a game, you can use a simple model for drag in which the drag force is proportional to the wind speed (it's actually proportional to the square of the wind speed, but that makes everything really complicated):
[itex]\vec F_w = \alpha (\vec v_w - \vec v) [/itex]
[itex]\alpha[/itex] is a constant that depends on the shape and size of the object. A very aerodynamic object has a low [itex]\alpha[/itex]. That gives you
[itex]\vec v' = \vec h - k \vec v[/itex] (1)
where [itex]\vec h = -g \hat j + k \vec v_w[/itex] and [itex]k = \frac{\alpha}{m}[/itex]. [itex]\vec h[/itex] is the new effective gravitational field. (1) is a differential equation with the unknown function being [itex]\vec v[/itex]. There is a constant solution. This is the terminal velocity. Call it [itex]\vec c[/itex].
[itex]\vec c = \frac{\vec h}{k}[/itex]
All solutions gradually approach this velocity. The full solution is
[itex]\vec v(t) = (\vec v(0) - \vec c) e^{-kt} + \vec c[/itex].
Integrating this gives you
[itex]\vec r(t) = -\frac{1}{k} (1 - e^{-kt}) (\vec v(0) - \vec c) + \vec c t + \vec r(0)[/itex]

That should get you started.
 
Last edited:
  • #4
James Leighe that really helped me out! However I think there are a few mistakes in your math for example:

Code:
projectile.x += 0.5f * projectile.velocityX * timestep * timestep;
projectile.y += 0.5f * projectile.velocityY * timestep * timestep;
should be


Code:
projectile.x += 0.5f * projectile.velocityX * timestep * timestep;
projectile.y [COLOR="Red"][B]-[/B][/COLOR]= 0.5f * projectile.velocityY * timestep * timestep;
Also the gravity should be subtracting not adding:

Code:
projectile.velocityY [COLOR="red"][B]-[/B][/COLOR]= -9.80665f * timestep;


apart from then I just tweaked the numbers around until it looks good :) Thanks for your help!
 
  • #5
Also to edit your code I took out the time squared, it was giving it weird results:


Code:
projectile.x += 0.5f * projectile.velocityX * timestep;
 
  • #6
Feel free to test out my code:
this includes draw, and collision

Obviously some user interface glitches that I'm not concerned right now.
the wind indicator in top middle shows the direction and power of the wind (from 0 being low to 23 being highest)
the user is stuck at power 20 for the initial velocity of the projectile just for testing purposes

sorry if you have a small screen and it all doesn't fit, when I work on the camera control (pan and zoom) it will fit to your screen.

Also when you refresh it will change the wind direction and speed

https://github.com/JAStanton/worms/tree/fd5d30497ebe5c96ddec7079b6ed8240db3169b7

Currently it's hosted here but I will probably overwrite it soonL
"jastanton.com/experiments/worms"
 
Last edited by a moderator:

What is a basic projectile with wind?

A basic projectile with wind is a type of physics problem in which an object is launched into the air at an angle and is affected by the force of wind during its flight.

How do you calculate the trajectory of a basic projectile with wind?

To calculate the trajectory of a basic projectile with wind, you will need to use the equations of motion, taking into account the initial velocity, angle of launch, and wind speed and direction. The trajectory can be determined using trigonometric functions and the principles of vector addition.

What factors affect the flight of a basic projectile with wind?

The flight of a basic projectile with wind can be affected by various factors, including the initial velocity, angle of launch, air resistance, and the speed and direction of the wind. Additionally, the mass and shape of the object can also play a role in its flight path.

How does the wind affect the range of a basic projectile?

The wind can have a significant impact on the range of a basic projectile. Depending on the direction and speed of the wind, it can either increase or decrease the horizontal distance the object travels. This is because the wind exerts a force on the projectile, altering its trajectory and ultimately affecting its range.

How is a basic projectile with wind different from a basic projectile without wind?

A basic projectile with wind is different from a basic projectile without wind in that it takes into account the added variable of wind force. This makes the calculations more complex, as the wind's direction and speed must be factored in. Additionally, the presence of wind can significantly alter the trajectory and range of the projectile, making it a more challenging problem to solve.

Similar threads

Replies
4
Views
1K
Replies
9
Views
2K
Replies
5
Views
1K
Replies
8
Views
5K
Replies
6
Views
4K
Replies
36
Views
20K
Replies
6
Views
1K
  • Introductory Physics Homework Help
Replies
11
Views
788
  • Introductory Physics Homework Help
2
Replies
53
Views
3K
Back
Top