Jump to content
  • Advertisement
Sign in to follow this  

2d Projectile

This topic is 4751 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Im working on a 2d scrolling shooter dubbed Mario Slug at the moment. It's been a while since I've worked with it but I've finally come back and am stuck at the problem I was at when I left it. I can't get projectiles to fire and animate across the screen o.O Man Im just having a brainfart right now. So, throw out some suggestions on how to go about writing the fire function and animating the bullets from shooter to target please! I need a little push to get my brain moving again lol Thanks everyone!

Share this post

Link to post
Share on other sites
For a really simple system, you can just track the coordinates of the bullet projectile on-screen. Each frame, update the position of the projectile, and draw it in its new location. You could use a list of all currently "live" projectiles, and update each one every frame before drawing. Your "fire" code then simply needs to add a projectile to the list, set up its direction and initial position, and let the other code do the work.

If you'd like more details on any of that, a little more information on your game would be very handy - what have you written and gotten working up until now? What language and graphics rendering tools/API are you using? That sort of stuff.

Hope that gets the juices flowing again [smile]

Share this post

Link to post
Share on other sites
im glad to hear that you are still working on your project. i remember reading about it awhile ago but havent heard much lately. but this what i would do if i were you (i put some thought into this because im creating a game similar to you hehe).

create a vector that would hold an object called 'bullet'. and in this object, have the following things:

int x;
int y;
bool alive;
int bullet_type;

and each time you fire your weapon, add the object into the vector. then upon refreshing your screen, have code that will scan through the vector and update any bullets based on its bullet_type. the bullet_type will allow you to have bullets that act differently,such as a regular bullet with straight fire or a heat seaking rocket. of course, upon hitting something, make it dead. also, if it goes out of bounds (off the screen), also make it dead and remove it from the vector.

hope that helps. GOODLUCK!

got any screens? :)

Share this post

Link to post
Share on other sites
One suggestion I have for you is to try to implement a fly wheel pattern for it. That is if all the bullets of a single type look exactly the same, only have one instance of the texture loaded for all of them, and when you go to draw just have it drawn everwhere needed. It saves memory and time because of not having to switch textures. I used the pattern in the 2D space shooter I made, Galaxy Gladiator its in the showcase, if you would like to see some of the actual code behind the bullets let me knwo adn I will dig it out of my source archives, its somewhere on my portable HD, just message or email me about it.

Also for the moving of the bullet itself, if you have the bullet as its own class you can have functions inside it so that it handles everything on its own you just have to call it.

struct Position
float x = 0.0f;
float y = 0.0f;
//float z = 0.0f; This can be used to have some depth control for
// background firing but is other wise totely not needed
// Variables
Position m_Position;
float m_direction; // You could use a 2D vector for this using a float
// requires using a bit of math to get to work but isnt to hard, Pi is your
// friend here
Target m_target; // This is self explanitory
// Functions
bool init( float x, float y, float direction, char type ) // Initialize
// everything here
// try not ot have too many public variables bool m_isAlive is acceptable tho
// Function
Bullet(){ init( 0.0f, 0.0f, 1.0f, 0 );
Bullet( float X, float Y, float direction, char type ) { init(X, Y, direction, type ); }
turn(); // for the homing bullets

Now I had a seperate private function initiate the variable mainly because there are cases where maybe there is more to initializing the object then just setting the members and rather then clutter up the C~Tor I group my functionality. Also with this example the C~Tor with everything specified would most likly need a 5th variable where you assing its target so it knows what to seek out. Another approach would be to have a C~Tor where you specify the target and not need to specify the type in any of them its just know by which C~tor is called.
Now before you draw the bullet each time this way you just make sure Bullet->Move() is called first and if needed turn() or incorperate turn into move, and only have it execute if the bullet is a seeker.

Share this post

Link to post
Share on other sites
ApochPiQ: That helps get things rollin again =)
Oh and I've written code to get bmps blitted on to the screen and code for movement.
The characters can move left and right and duck/look up in the direction they are currently facing. Im working on a jump function to add to the movement.
It's written in C++ using SDL for graphics.

Tont: I didn't realize I actually had people interested in my project! Im very glad to hear this =) Thank you for the suggestions. Im getting back on track finally hehe.

Downer: Now that sounds really interesting. Memory saving is always a good thing lol. I like the idea of making the bullet a class so it can handle itself.

These are all excellent ideas and suggestions guys! Thanks a ton for the help =)

Share this post

Link to post
Share on other sites
Hey, I don't know if you need help in the maths department but here is some:

For the bullets I would use a 2d math vector (not to be confused with the c++ dynamic array). Split up the vector components so you use two variables (one for the vectors x component and one for the y component)



//COLLISION 1 for no engery loss, use decimal if you want energy loss

Something along thoose lines should work ;) (I have done such systems in the past)....

Taking an OOP based approach you would have your bullet as an object and have a vector of bullet objects. Your bullet would have a constructor where you would place the code for the angle and the base x and y. Then you would have a move method which would apply the gravity (set in the constructor) and move the bullet. You could also have a draw method that draws the object to the screen. Lastly you should have a collision method for when the object collides (You could implement some kind of normal detection and what have you, I am sure there are resources for this on the net). If you want your bullets to destroy on impact then you would erase them from the vector (If using STL then use the erase method heh).

That should atleast give you some ideas :). Good luck!

Share this post

Link to post
Share on other sites
my turn to help you ^_^

well basically when i did my projectiles (fruit), i used an array of a structure (or a dyanamic array); it held the image, velocity, position, damage dealt, etc

then you just add your velocity to your position each time you update your frame; also don't foget to check your boundaries and if you want to make your projectiles have a certain "live" time, after that get them off screen (and outta memory); i dunno, probably this is pretty inefficient, but i'll be fixing that in my next game!

hope that helped...maybe i was just saying what others have already said

[Edited by - -justin- on February 12, 2006 5:59:03 PM]

Share this post

Link to post
Share on other sites
Shadowisadog: Thank you very much for the math help =) I haven't worked on the code today since its a really hot day and I can't think when Im too busy trying to cool off lol =)
I think Im only going to have gravity affecting the players to keep them to the ground. Bullets won't be affected and will be going in a straight line in the direction they're shot. Just like in Metal Slug.

The directions will be:
Straight Up
Straight Left/Right
45 degree Left/Right
Straight Down

I think this is like the gameplay is in Metal Slug. Ill have to replay the game and pay attention to how the bullets move hehe.

-justin-: Thanks for the input. Im not sure if I would give the bullets a set life as the actual visible screen's size won't allow for the bullet to be around too long before moving off the screen and being deleted. Nice idea though =) Would be good for timed projectiles like a gernade.

And I just have to ask: fruit? lol =)

Share this post

Link to post
Share on other sites
I like your screens, please make luigi a big deal in your game, he is so under sold in mario games. anyway back to suggestions. With my suggestion of fly wheeling the texture and render parts I also made a class for the textures one for each look i wanted and had a pointer of that "BulletGraphic" class in the bullet. now the bullet.Draw() would mainly do the translation for its instance of bullet and then call BulletGraphic.Render() ... I found the actual code but dont know how to get code to show in a little text window of thier own, how do you do this and I can post my Bullet.h & Bullet.cpp which are prolly not the cleanest Ive done as Galaxy Gladitators its in the showcase, which Im sure I mentioned, I can be a shameless self promoter some times. http://members.gamedev.net/Downer_Space/GG2D/2D.zip is the link to the zip for the game

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!