Just like the title says. I'm working on a shoot em up for a personal project, and I can't seem to get this to work. I don't have any code for it, but I do have the code to make my ship shoot, but that's pretty straight forward mumbo jumbo. I have seen people use the Math function for this, but any help would be appreciated.

  • \$\begingroup\$ This a top down or side scrolling shoot em up? \$\endgroup\$ – Seta Apr 10 '15 at 1:16
  • \$\begingroup\$ This is a top down shoot em up. My ship is at the bottom of the screen. I was thinking of doing this a simple way, and just add to the x velocity of the bullet, while the y velocity is at a constant speed(going up). But anything would be appreciated. \$\endgroup\$ – Martin Apr 10 '15 at 1:23

I have answered this before, not that difficult, although this method is only as accurate in relation to how many points, perfect accuracy is when the number of points you use reaches infinity.

have a vector that is the flying bullet, it starts at exit of muzzle with a direction and speed.

have a vector of gravity, a vector of air resistance. have a force of gravity

decide how many points you want based on how many seconds you want to have between location checks.

Then for every point move object direction it was heading from its last vector using speed and time between points and movement vector of object.

Then for the next point Change its speed based on air resist by applying that vector Change its directional vector(or altitude? see what works best) by the gravity vector. And change the gravity vector(so it is accurate for next calculation) by using the next level derivative of acceleration that gravity has. Gravity accelerates so each step it is a larger vector then step before. (then if you expect fast falling objects apply air resistance to gravity vector also so things like terminal velocity will have a emulated effect)

Then find that next point, and next, and next, by iterating over all the points until collision detection hits another ship or water. This is also how ships can calculate firing angles before shooting a shell for them to land at a specific target.

You also can precompute where at some angle, and some shell mass and speed, where that shell will land, then use a look up to a table of precomputed landing location at same altitude. Table can also include a set of points for animation.

The only thing that changes the impact location (if not calculating with windage effects) is starting velocity, air resistance effect(based on shell mass) and starting speed. So if you are using the same shells in computation, a table of points will always be the same for every angle.

if your using many different speeds like how far you pull back a bow, you could make a table with entries for angle and power(^2 table size though), and then have to interpolate between any two points in the stored array to find accurate points, since there are so many more entries precision of each entry could be less, or table would be much larger.

| improve this answer | |

Since this is a top down shooter, and you have the basic shooting done, here is a 5 minute piece of (untested) incomplete code that allows for the creation of bullets at different angles.

public class Bullet
    protected Texture2D texture;
    protected Vector2 position;
    protected Vector2 angularVelocity;

    /// <summary>
    /// Defines a bullet.
    /// </summary>
    /// <param name="bulletTexture">Texture for the bullet.</param>
    /// <param name="bulletOrigin">Position the bullet will originate from.</param>
    /// <param name="bulletSpeed">Speed of the bullet.</param>
    /// <param name="bulletAngle">Angle of the bullet (in radians).</param>
    public Bullet(Texture2D bulletTexture, Vector2 bulletOrigin, float bulletSpeed, float bulletAngle)
        texture = bulletTexture;
        position = bulletOrigin;
        // create our angle from the passed in angle
        Vector2 angleVector = new Vector2((float)Math.Cos(bulletAngle), -(float)Math.Sin(bulletAngle));
        // multiply the angle vector by the bullet to get its angular velocity (velocity on some angle*)
        angularVelocity = angleVector * bulletSpeed;

    public void Update(GameTime gameTime)
        // time since last update
        float delta = (float)gameTime.ElapsedGameTime.TotalSeconds;
        // multiply the angular velocity by the time elapsed for delta correction (unstable framerate)
        // then add it to the position
        position += angularVelocity * delta;

Its not terribly complex (and probably isn't accurate), but basically you create a bullet with a specified texture, origin, speed and angle and update it every turn (possibly with a manager of some sorts).

I've commented the code, so hopefully its enough to demonstrate how to go about firing bullets off on an angle for that wonderful 7 barreled, 135 degree cannon that shoots EVERYTHING on the screen, except for the 225 degree blind spot to your sides and behind you (You do have one of those, right?)

* I could have given this variable a better name, but then it would have taken more than 5 minutes

| improve this answer | |

Here's the simplest math function for velocity based objects utilizing parametrics.

x += cos(theta);
y += sin(theta);

The end. It's really quite simple because speed is a scalar, but we want speed and direction, in essence, a vector -VELOCITY, thus we end up splitting our speed vector into x and y components as needed by a dimensional object.

You can apply this to a wide range of physics based equations too!

DisplacementY = .5 * acceleration * time^2 + initialYVelocity
DisplacementX = velocity * time

P.S. if you use the above equation for gravity, I wouldn't accelerate too much or it'll make collisions kind of buggy (that's why fast moving objects in game sometimes end up in a wall or going straight through them).

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.