How do I calculate mortar trajectory in a videogame?

I've been playing a lot of Gmod lately, a fantastic sandbox game with unlimited possibilities. There are many addons to Gmod, one of the more common for sandbox servers being Wiremod. There's a specific tool in wiremod called Expression2, which is a language very similar to LUA. I commited to learning E2, and I've come a long way. Being as young as I am, though, complicated math has always been a huge roadblock. I'm in 8th grade, I've never taken trig. I'm currently atempting to make a mortar of sorts, but most of the research I've done has been like an entirely different language to me. As far as I know, there's no drag or air resistance in Gmod, so I'd probably just need to include launch velocity, launch angle, launch position, and end position. All I really need is some pointers on a formula and how to use it. Anyone willing to help me out?

sort by: active | newest | oldest
-max-2 years ago

Well to really understand physics in games, you probably need to know physics in real life! And that means lots of 1st, 3nd, and 3rd semester calculus, a thorough trig knowledge, possibly linear algebra and stuff, definitely vectors and multidimensional /multivariable calc, and lots of other good stuff. I am certainly not there yet, but will be in a few years.

A trajectory can be approximated with a quadratic function, like -x^2 + 4x. Only bother with the positive portion of that function, so in the range of {0 > x > 4}.

If the cannon shot some mortar, at a specific angle, then that angle can be converted to a slope, taking the form of y=mx, m being slope, is calculated (defined) as ∆y/∆x, or slope over run, as you may remember it. Look into and learn about derivatives (like the "limit" definition) on Khanacademy, you will see where I am going with this. Do then you have a initial slope, you can graph that as a point on a graph showing all the slopes of that curve, and it will be linearly accelerating to the ground at 9.8m/s^2, over distance and time, so that just means make a linear equation, using that 9.8 slope, and that one point graphed about the initial slope. Then you need to use trig to figure out initial velocity and separate that into the 'UP' and 'forward' vectors or something like that, and it gets really complicated. (especially if you want to consider air resistance and stuff!) I do not even remember how to do this, and did not do well on physics the many years ago when I took it. I think there are simplified formulas you can use, however.

In the end, I would just use a random realistic-looking quadratic function as the trajectory. Its a game, you can do anything you want in it!

Vyger2 years ago

One over, one under, one on target.

In the absence of airfriction, the path of a mortar in X and Y coordinates is pretty simple.

If the mortar leaves the gun at some velocity V, and an angle A to the horizontal, upwards, then the X co-ordinate at time t is X=Vcos(A) x t

the Y co-ordinate at time t increases as Y=Vsin(A) x t + gxt^2/2 for the time until t= Vsin(A) /g, then it traces the same path down again

Nuurdle (author)  steveastrouk2 years ago

Thanks! What's G though?

technically, its the acceleration due to gravity - any object accelerates to the ground at 9.8 m/sec/sec.

You don't actually need the term here - it could be 1 for all the difference it would make in a game.

Except a magnet through an alloy tube, it takes up to 30x longer to fall!

I did an 'ible on it - you might have see it already but ow I have added a video :)

Gravity applies to the magnet it is just that the falling magnet generates an opposing electromagnetic force.

Yes, it applies but not at 9.8m/ps...

Not correct, despite that it seems that way. Gravity is, essentially, a constant. It is a force causing the magnet to accelerate downwards at 9.81 m/s^2 (32.2 ft/s^s) but the electromagnetic breaking is imparting it's own force, and thus acceleration, on the magnet in the opposite direction. I've attached a free-body diagram of the situation.

Gravity doesn't change, it is just being opposed. If we apply a little more physics we can estimate the force applied by the EM breaking and the how it contributes to the total acceleration of the magnet (See the second image).


Well, it still applies, you are right.

The thing I am saying is the magnet cannot and does not ever get to speeds of 9.8mps.

There's some confusion between velocity and acceleration I think. 9.8 m/s (mps) is the speed component of a velocity and would be achieved with a constant acceleration of 9.8 m/s^s (meters per second squared) after one second, in the direction of the velocity, in a vacuum. The point everyone is making is that everything on the surface of earth is subject to the acceleration of gravity, your magnet just has a resistive force. I can drop my notepad and it will be subject to the same acceleration but will never reach 9.8 m/s.

The calculations I included are very crude and treat the EM force as a constant when in fact it is relative to velocity. I suspect given the right mix of variables (magnet mass, shape, and material, pipe length, thickness, and material) you could get a wide range of velocities.


In fact, for paper-napkin calculations, I often just use 10 m/.sec^2 to get "an idea" of how things are.

Wired_Mist2 years ago

Haha, One of the best Games ever :D

Provided you keep the Physics settings at their default Values (and your not playing something like SB that alters the Physics) the engine uses real word physics to drive the animations (Minus Surface area to Air resistance, but still including object to object Friction. ** Note that Gmod does add some-sort of Air friction but it is not proportional to the objects Surface aira)

After that it's just like what you May be learning (or about to learn) in school right now!

Just screw around with the Speedometer Stool + Wire Debug under the "Wire" tab

If you really want the exact formula the source engine in an open platform. Should be lots of Documentation on Google :)

You may Enjoy a little Page I pulled from Google :)


This page will show you the actual calculations that the Game uses !

iceng2 years ago

You may need to complicate the math when your mortar begins to travel far enough that the curvature of this world distorts the flat-land projectile landing.

Have you heard about the shot that went around the world.

Don't shoot yourself in the back of your head !