• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
  • entries
    68
  • comments
    294
  • views
    117722

Projectile Motion

Sign in to follow this  
Followers 0
slayemin

4416 views

YEAH!!! I got it! I got my math down for projectile motion in 3D space and it works!

Alright, so, let me explain what I was trying to do and how I figured it out.

Problem description: I am a wizard standing at any given position in 3D space. This position is given by the coordinates X, Y, Z. I am launching a fireball projectile at another position in 3D space. My fireball *must* pass through this point. Fireballs are a bit magical as well, so when I launch the fireball, it should always have the same speed for every fireball. I also want to launch my fireball at an arbitrary angle, but for now I am using 45 degrees (pi/4). If I wish, I should be able to increase or decrease the speed of my fireball and still get a perfect firing solution.
Shot_Part1.png

Extra problems: The target may be hiding behind a hill. If this is the case, (to borrow an artillery term) we need to perform a high angle fire mission to hit the target in defilade. Somehow, we will need to detect this situation.

Bonus points: We want to minimize the time of flight for our projectile by choosing the firing angle which has the shortest flight time (ie, a direct shot vs. a long arching shot).

Solution:
I am going with basic, newtonian projectile motion in euclidean space. I am going to ignore the effects of air resistance, wind direction, air density, temperature, etc. The only force which will act on our projectile in flight is going to be gravity. Now, gravity is typically a constant for projectile motion equations. However, since our angle and projectile speed are already constant, we're going to have to change gravity in order to actually hit our target. If we left everything alone, our projectile would follow the same path, regardless of where we want it to go.

So, let's represent our projectile with a 3D coordinate which indicates its position in coordinate space by using a Vector3. Let's also remember from our calculus that velocity is just a change in position over time (note: velocity is a speed and direction). Acceleration is just a change in velocity over time. Here are some select snippets of code to illustrate how I represent physical objects:public abstract class Physical : GenericGameObject, IRenderable{protected Vector3 m_position;protected Vector3 m_lastPosition;/// /// note that velocity may be distinct from orientation (ie. facing forwards but moving backwards)/// protected Vector3 m_velocity;protected Vector3 m_acceleration;public virtual void Update(GameTime gameTime) { m_lastPosition = m_position; m_velocity += m_acceleration * (float)(gameTime.ElapsedGameTime.TotalSeconds); m_position += m_velocity * (float)(gameTime.ElapsedGameTime.TotalSeconds); m_boundingSphere.Center = m_position; }}
This is all we need to move the physical object in an interesting way. If we give the object a velocity, it will move along that velocity vector at the magnitude of that vector. If we give it an acceleration value (such as a gravity force), the velocity will change. Lastly, we keep track of the last position of the object before we update its current position. This is so that we can do collision checks (a really fast moving bullet might pass through a thin object without being detected, or a ball may move into the ground before you bounce it back, etc).

Anyways, let's get back to the mathematics. I found this super handy website at Georgia State University about trajectory motion with a few physics equations. Just as a sanity check, I checked other sites and they used the same math equations (so, we can be sure that we won't get bugs from flawed math & physics, just flawed code). Here is the physics equation for projectile motion:
Shot_Part2.png

So, this equation will tell us the X and Y position of a projectile at any point in time. That's handy. However, we already know where we want or projectile to be. We want it to be on our target! We know the X and Y position of our target and we know our initial velocity, so we have a few knowns! If we can solve for time, we can solve for our gravity constant! Here is the math for it:
Shot_Part3.png

All we have to do is try to solve for the knowns and we can figure out the necessary gravity constant. Here is an illustration I made with some arbitrarily picked values:
Shot_Part4.png

We already know our gun position and the target position, so it's pretty easy to gather the X and Y values. The Y value is just going to be the difference in elevation between the gun and the target. The X value is going to be the length of the line on the X/Z plane between the gun and the target. The position of the projectile will travel along this line at a constant lateral speed. This is super simple to find, we just find the direction vector from the gun to the target like so:Vector3 FireVector = Target.Position - Gun.Position;
Since this is in 3D space, and our physics representations are in 2D space, we're going to want to work in 2D space. We want to use the X/Z plane as our X axis, so we just find the length of our X/Z portion of the firing vector by applying the distance formula:float tgtDist = (float)Math.Sqrt(FireVector.X * FireVector.X + FireVector.Z * FireVector.Z);
We're done with the firing vector now, so now we want to use it for actually moving the projectile along it. First, we just have to normalize the vector, then I apply an elevation angle of 45 degrees (pi/4) and then I scale the magnitude of the vector by the speed I want to give my projectile.FireVector.Normalize();FireVector.Y = MathHelper.PiOver4;FireVector *= 10;
Oh look, now we have our initial firing velocity! It has a direction and speed, exactly what we need for the equations above. Well, we can figure out the speed we're moving along the X axis by taking a distance of the velocity projected onto the X/Z plane by measuring its distance:float Vx = (float)Math.Sqrt(FireVector.X * FireVector.X + FireVector.Z * FireVector.Z);float Vy = FireVector.Y;
Now we have enough known values to plug into our equations above:float T = tgtDist / Vx;float Y = Target.Position.Y - Gun.Position.Y;float G = (2.0f * (Y - Vy * T)) / (T * T);
And there we have it! We now know how much "gravity" to apply to a projectile in flight so that it hits its target! The rest is trivial:fireball.Accelleration = new Vector3(0, G, 0);fireball.Spawn(Gun.Position, FireVector);fireball.Effect = Gun.Effect;m_sceneDB.AddPhysical(fireball);//etc, etc
Now, we can send a projectile to hit any target and use a constant initial velocity, such that shooting at nearby targets and distant targets don't create a wild variation! An archer looses an arrow at the same speed, they just adjust their elevation to hit more distant targets. A catapult works the same way. Both have range limitations, so they can use the standard gravitational constant. However, a fireball is a "magical" projectile which isn't influenced by real gravity. We just want to create a flying projectile which has a constant speed and always gets to it's target.

Extra problem: Well, what if the target is in defilade, such as behind a hill or wall? To detect this problem, we try to shoot an intersection ray between the gun and the target. We already know the distance between the gun and the target, so if the intersection ray hits something closer than this distance, we know that we've got an object in the way. Now, we've got a real problem! We have to get our shot over that obstacle by increasing our elevation. There's also a potential chance that there simply isn't a firing solution to hit the target (ie, it could be in a cave!). From my artillery days, our technique was to just use high angle fire and adjust onto target. Can that work here? I haven't tried it, but the idea is that you use a "best guess" for your elevation angle, fire a virtual shot and see where it lands. Does it land before or after the target? Measure its position on the X/Z line, then fire a second, adjusted shot with the intention of getting on the other side of the target, but as close as you can. Record the shot landing position relative to the first shot and the target. The line between the two shots is your range line and the distance between the target and either of those two positions is the proportion of your adjusted amount you need to correct by. Simply perform a linear interpolation with this adjusted amount and try again. I *think* you'll hit the target every time on your third shot. If you don't, the target is probably in a cave and no firing solution is possible.

Demo: Here is a very short youtube video demonstration to show the action in 3D space.

5
Sign in to follow this  
Followers 0


4 Comments


Very interesting -- I think this would make a great article if you have time to submit it to the system!  Thanks for sharing! :)

1

Share this comment


Link to comment

Thanks, I'll keep that in mind. I think I'd have to spend a bit more time and effort editing for clarity and content if I was going to submit it as an article. This topic also seems a bit too simplistic? I'm not sure.

0

Share this comment


Link to comment

Remember that what seems simple to you may be exactly what a beginner or simply someone inexperienced with projectile motion is looking for! :)

1

Share this comment


Link to comment

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