[XNA 4.0][Series] Projectiles

Discussion in 'Game Development' started by GoldBl4d3, Dec 24, 2012 with 4 replies and 1,174 views.

  1. GoldBl4d3

    GoldBl4d3 VIP VIP Retired

    Messages:
    3,778
    Ratings:
    1,345
    Projectiles

    In this tutorial we will learn how to shoot a projectile and incorporate the game mechanics required in order to operate the weapons and flow. This tutorial is for absolute beginners. However, experienced programmers may comment and help others.
    Series Part 1: http://www.se7ensins.com/forums/threads/xna-4-0-series-the-project-setup.836045/
    Series Part 2: http://www.se7ensins.com/forums/threads/xna-4-0-series-content-input-draw-movement.836830/

    Skill Level: Easy
    Time to Complete: 20 Minutes

    Art Assets Used:
    [​IMG]


    #3.1 Scale Rocket Ship


    First we need to scale down the size of our ship to 35%.

    [​IMG]






    #3.2 Projectile Class


    Now we need to create a new class in our game project. Call it Projectile. This class will be used to store the position and velocity of our projectile.

    [​IMG]


    #3.3 Establishing and Loading Projectiles


    First lets declare a new list of live projectiles. This list will contain all projectiles that are bouncing off of the walls. When we add to that list, projectiles will appear in game. When we remove them they will no longer be present in the live game.
    Then we must load our projectile content. Add the ball from above to your content project. Follow along to load it in to memory.

    [​IMG]


    #3.4 Shooting Projectiles


    Now we will add a new projectile per frame as long as the space bar is down. We set the projectiles position to the ships. We set the velocity of the projectile based on the ships rotation and a set speed. After we do this, we run through the list of live projectiles and update their positions based on the velocity. Take note that we subtract 46 degrees from our rotation. This is because our ships texture isn't default to zero degrees. So we must fix our angle.

    [​IMG]


    #3.5 Drawing our Projectiles


    Finally, we must draw our projectile. We draw based on our projectiles position. We want to set the origin of the ball to its center. Then we must scale it down to 20%.
    Please take note that we draw our projectiles first, then we draw our ship. The reason for this is because our textures draw in the order we call them. So if I draw the ship first, the projectiles will appear to be on top of the ship, instead of coming out the tip.

    [​IMG]


    Final Results:


    Now debug your game and hit the space bar. Your rocket ship will now shoot.

    [​IMG]


    Thank you for reading and following along. If you have any questions please post below. Please watch out for the next parts in the series. This ends the three part series on getting started in XNA. More tutorials will be released.
     
    • Like Like x 1
  2. Zorg93

    Zorg93 Read Art

    Messages:
    4,276
    Ratings:
    2,834
    after the line
    Code:
    p.Position += p.Velocity;
    why not include something like
    Code:
    p.Velocity.Y -= 0.098;//play around with that value
    that would be necessary to give the projectiles a more realistic gravitational effect
    EDIT: and maybe to help out on memory usage, delete projectiles when they leave the screen
    Code:
    if (p.Position.X < 0 || p.Position.X > graphics.graphicsdevice.viewport.Width || p.Position.Y < 0 || p.Position.Y > graphics.graphicsdevice.viewport.Height) //check to see when the projectile leaves the visible part of the screen
    LiveProjectile.RemoveAt(LiveProjectile.IndexOf(p));
     
    Last edited: Dec 26, 2012
  3. OP
    GoldBl4d3

    GoldBl4d3 VIP VIP Retired

    Messages:
    3,778
    Ratings:
    1,345
    Yes, all great things that you can add. I stuck to the very basics because this is for very new beginners. But good points, gravitational effects and removing projectiles off screen are must haves.
     
  4. coolbunnymodz

    coolbunnymodz The Original Bunny

    Messages:
    4,384
    Ratings:
    1,263
  5. OP
    GoldBl4d3

    GoldBl4d3 VIP VIP Retired

    Messages:
    3,778
    Ratings:
    1,345
    Under the Projectile class, change it to public class Projectile. If you look at my code closely you will notice that I have that.
     

Share This Page