• 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.
Sign in to follow this  
Followers 0

Need help calculating imprecise bullet trajectories in a 2D plane

2 posts in this topic



I'm programming the A.I. for a 2D soldier that has to shoot another soldier. I use ray casting to calculate the bullet trajectory. For example in diagram A the shooter is at (10,13) and the target is at (5,1) so the bullet will be traveling at -0.4167 units per tick on the X axis and -1.000 units per tick on the Y axis. The algorithm I use to get those number is as follows:

// get the distance on each axis
tx = abs(pta.x - ptb.x)
ty = abs(pta.y - ptb.y)

// make the longer axis 1.0 and the shorter axis a smaller number
if (tx == ty)
	tx = ty = 1.0;
elseif (tx > ty)
	tx = 1.0;
	ty = ty / tx;
	ty = 1.0;
	tx = tx / ty;

// invert any axis that is negative
if (pta.x > ptb.x) tx = -tx;
if (pta.y > ptb.y) ty = -ty;

This is fine, but now I want to skew the soldier's accuracy a little so that each bullet is fired at a slightly different angle, as shown in diagram B. I have two questions:


1) I probably need to change the angle by a few hundredths or so every shot to simulate inaccuracy, but my technique is based on a grid instead of angles. What would be the easiest way to modify the firing angle in this way?


2) How can I retrieve a list of the pink tiles in diagram B? I could run the algorithm on every possible angle but that could be slow and my program needs to be as fast as possible. I need the list because, as I stated earlier, this is an A.I. and the guy needs to know if there is risk of friendly fire.


Thanks in advance. By the way, in case you haven't noticed already, I suck at math. Just saying.

Edited by Nüb?ek Pænus

Share this post

Link to post
Share on other sites

This would be much easier working in angles rather than vectors. Use atan2 to convert vector to angle, and sin/cos to convert back to vector.


Sounds like all you need is to add a small random value to the angle of the shot. And for the AI safety, compare the angle between the AI position and the player position with the AI's facing direction. If the difference is greater than the range of the random value you add for inaccuracy, then there's no chance of the player being hit.

Edited by DekuTree64

Share this post

Link to post
Share on other sites

I recommend using complex numbers for these computations.


1) You can add a little bit of jittering to a vector by rotating it by some random amount.


2) Why would you need the list of pink tiles? All you need is a check to see if something is in the pink area.

#include <complex>
#include <cstdlib>

typedef std::complex<double> Complex;

double uniform_random(double a, double b) {
  return a + std::rand() * (b - a) / RAND_MAX;

Complex jitter(Complex direction, double max_angle) {
  return direction * std::exp(Complex(0.0, uniform_random(-max_angle, +max_angle)));
bool is_in_danger(Complex subject_direction, Complex fire_direction, double max_angle) {
  // Normalize input. You can skip this if you adopt the convention                                                         
  // that all directions need to be normalized.                                                                             
  subject_direction /= std::abs(subject_direction);
  fire_direction /= std::abs(fire_direction);

  return std::real(subject_direction / fire_direction) >= std::cos(max_angle);

Share this post

Link to post
Share on other sites

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
Sign in to follow this  
Followers 0