What's new

[XNA 4.0][Series] Projectiles

  • Thread starter GoldBl4d3
  • Start date
  • Views 1,476
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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:
sfbip4.png



#3.1 Scale Rocket Ship


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

24e2owm.png







#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.

2u5z5lu.png



#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.

pal20.png



#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.

2exrp75.png



#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.

2zznmh4.png



Final Results:


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

2zf4ign.png



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.
 
Zorg93

NayJames123

Read Art
Fabled Veteran Modder Programmer
Messages
4,273
Reaction score
3,475
Points
650
Sin$
0
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:
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
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));

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.
 
GoldBl4d3

GoldBl4d3

VIP
VIP
Retired
Programmer Mythical Veteran Legendary Veteran
Messages
3,785
Reaction score
1,452
Points
600
Sin$
0
Under the Projectile class, change it to public class Projectile. If you look at my code closely you will notice that I have that.
 
Top Bottom
Login
Register