Worm - LIke Game

Started by
7 comments, last by Stef13 22 years ago
Greetz, I would like to make a game kinda like Worms but don''t have a clue how to implement the effects of Gravity and wind on the cannonballs or whatever object Any help would come in handy bye Greetings from the one who knows so little
Greetings from the one who knows so little
Advertisement
All i remember is thers a trig info on the balistics of a cannon ball.

i dont remember where to find it but look in some trig books for balistics thats where i found some info when i remade scorched earth (tanks instead of worms )

about the wind if i remember right its like gravity but left or right.

Sorry if im no help!!!!!


The best RPG has yet to come into being (only ''''cause I cant do it all
The best RPG has yet to come into being (only ''cause I cant do it all :)
OOPS Check The spellin sorry
The best RPG has yet to come into being (only ''cause I cant do it all :)
Just ot let you know: inertia would be another good thing to implement so they don'' just go top speed when you first push the arrow key and don''t just stop when you stop...
Gravity is a constant downward acceleration. The easiest way to deal with this is to use simple Euler integration. Here's how:

First, create a vector representing the initial velocity of the cannonball.

I'm assuming you don't know what a vector is. A vector is a direction and a magnitude, something like "200 mph southeast." Mathematically, they are usually expressed in component form . Think of it this way: If you shoot a bullet and look at it in a 2d overhead view, what is its x value at one second? At two seconds? At three? What are its y values at each? You will find that the x value increases by a constant amount each frame, as does the y value. If it moves by 2 units in the x axis each second and by 5 in the y axis, then a vector could be written (2, 5) . This concept can be extended to 3d, with x, y, and z axes. I will refer you to the articles here on gamedev.

Now, you ask, "how do I find this cannonball velocity vector?" Well, let's look at this in 2d - now from the side.

     /               The cannonball is fired from point p     /                upwards at a speed of 60 meters per second.   /_                The angle at which it is fired is Theta.  /   \ <-- ThetaP/____|______  


Using simple trig, you can tell that the cannonball will move upward 60sin(theta) meters each second (without gravity) and 60cos(theta) meters forward each second.

Now, we need to factor in gravity. gravity is a constant downward acceleration. Now think of what acceleration is: a change in velocity . Velocity is a vector quantity; it represents both direction and speed (magnitude). So is acceleration. So, to accelerate something, you add an acceleration vector to the velocity vector.

Gravity for our purposes (since we're near the surface of the earth) is a constant acceleration vector. It is 9.81 m/sec2(0, -9.81) .

Hopefully you now have a basic idea of how to proceed. I'll help you out though by giving you some basic pseudocode:


    //PSEUDOCODEball.velocity.x = speed * cos(theta);ball.velocity.y = speed * sin(theta);for(each frame){    //Add velocity to position    ball.position.x += ball.velocity.x; // x component    ball.position.y += ball.velocity.y; // y component    //The x component of gravity    ball.velocity.x += 0;        //This can obviously be removed    ball.velocity.y -= 9.81;     //This is the important part}    


After a while, continuously subtracting 9.81 will make the y component of the velocity zero (at which point it will hover motionless for the smallest instant at the top of its arc), and the subsequent subtractions will make it negative, and it will begin to fall back to earth, getting faster and faster.

Note that my number, 9.81, is in meters per second on earth. This number may not fit your game scale, and you may also want your gave to have lower or higher gravity than earth. So you can change it.

Also remember, of course, that all units are units of length per second . This means that if you want to keep a constant behavior at all framerates, you'll need to multiply all constants by the amount of time it took to render the frame (eg, divide by FPS).

This is the end of super-basic physics 101. You shoudl be able to get started now. Good luck in your game!

[edited by - TerranFury on April 19, 2002 5:50:44 PM]
That's got to be the best explanation of a vector and simple projectile motion I've seen. Kudos to you TerranFury!

[edited by - UBC_Wiskatos on April 19, 2002 8:46:51 PM]
[email=ubc_wiskatos@hotmail.com" target="_blank" style="width: 10px; height: 10px; background: #fe7a21; overflow: hidden; display: block; margin-bottom: 2px;][/email]Wiskatosxp
Wow, I have to agree, that explanation was great - much better than any book I''ve seen! TerranFury, do you know of any books with that kind of detail? Andre LaMothe''s books that contain a physics chapter always suck That post was really good, I''m saving it
"I am governed by none other than the Laws of the Universe."
Thanx for the detailed explanation, I''ll try it out (as soon as I find the time for it)

Very much thank you !!

Greetings from the one who knows so little
Greetings from the one who knows so little
The explanation above was not correct. Close, tho. The physical modelling of a projectile is counted by the following formula
x = cos(a) * v * t
y = sin(a) * v * t - ((1/2) * g * t * t)
where
x is the horizontal distance travelled (in meters)
y is the vertical " " "
a is the angle where the projectile was shot
v is the initial velocity in meters/second
t is the time the projectile has travelled so far (in seconds)
g is the gravity in meters/second^2

the gravity of earth is 9.81 meters per second squared. the same number for moon is 1.62 m/s^2, for jupiter it is 25.9 m/s^2 and for saturn it''s 11.3 m/s^2. I implemented different gravities in I once coded a simple artillery duel (with a real retro feel''n''all) where the I had different gravities of planets. gravities lower than moon and higher than saturn made the game impossible to play.

modelling the wind ACCURATELY is a tougher one. the effect of wind changes as by the shape of the projectile. the most simple way is to calculate x like this
x = sin(a) * v * t + wind * (delta)t
where
x,a,v and t are the same as above.
wind is the speed of the wind (in meters/second)
(delta)t is the time passed since the last calculation.

i''d suggest you have a 25 m/s maximum for the wind and 50-100 m/s maximum for the initial velocity to make a nice feel to the game. This gives an average shot of 100-500 meters, so make your playing area at least 1 kilometer long.

you can try to find an university-level physics book with more detailed explanation of the wind and air viscosity and stuff like that. I would stick to these as they are accurate enough for a game.

Hope this helps, Richardo

---------------------------for(goodness_sake()){  do  {    something();  } while(you_can());}

This topic is closed to new replies.

Advertisement