• Advertisement
Sign in to follow this  

Firing from a ship (sprites, C++)

This topic is 3734 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

How would I go about shooting a bullet (sprite) from, say, a ship (sprite)? I currently have a PositionedObject base class with a few other classes that derive from it, including Ship and Bullet classes. I guess I need to find the point on the Ship sprite that the bullet will shoot from, find the unit vector for the direction it's facing, and then send the bullet in that direction...? Is there a better way to do this?

Share this post


Link to post
Share on other sites
Advertisement

Hi,

I think that your method has the minimum required information for shooting a bullet.

Few thing I'd take in consideration:

- you may want your ship to shoot in many directions at once, so consider possibility of having multiple firing points with different directions

- efficient cache of the bullets. You may not want to create hundreds of bullets by allocationg memory from heap and releasing it a second later.

Best regards!

Share this post


Link to post
Share on other sites
Quote:
Original post by jollyjeffers
Moved to for beginners as I don't see anything specific to DirectX here.

hth
Jack


Sorry, this is my first DirectX game and I didn't know whether this would be a DX related problem or not.

Quote:
Original post by kauna
Hi,

I think that your method has the minimum required information for shooting a bullet.

Few thing I'd take in consideration:

- you may want your ship to shoot in many directions at once, so consider possibility of having multiple firing points with different directions

- efficient cache of the bullets. You may not want to create hundreds of bullets by allocationg memory from heap and releasing it a second later.

Best regards!


Yeah, I'm aware of that, but firing off in different directions would be very similar to firing off in one direction, which I'm yet to do. Starting easy :)

I've set up an array of Bullet objects. So once one bullet goes out of focus, its place in the array can be re-used when the user fires again.

Share this post


Link to post
Share on other sites
Use a std::vector or similar container class from the Standard Template Library, instead of an array. They'll make your life much easier. You'll see. ;)

Share this post


Link to post
Share on other sites
Quote:
Original post by Captain P
Use a std::vector or similar container class from the Standard Template Library, instead of an array. They'll make your life much easier. You'll see. ;)


Totally agree, if you're just starting out then taking Captain P's advice will ease things tremendously.

Share this post


Link to post
Share on other sites
Well I was going to use a "visible" bool, so that when a bullet isn't visible, and it's first in the array list, it'll be used when the user hits fire.

Share this post


Link to post
Share on other sites
Quote:
Original post by Side Winder
Well I was going to use a "visible" bool, so that when a bullet isn't visible, and it's first in the array list, it'll be used when the user hits fire.

I did the same basic thing for a trail behind a ship. It worked fine for me. Maybe a more experienced member could explain how to use caching more efficiently?

Share this post


Link to post
Share on other sites
Use a circular array (read std::vector) and a pointer to the next free slot. So as you spawn projectiles you just move the free pointer along one slot. Prevents you from needing to do any searching for a free spot. If the free pointer points to a still active projectile, you've filled your vector.

As for actually firing, you sound like you have the idea down pretty good. Get your ships position (or the position of the gun) get a direction vector that could be the ships facing, velocity, any direction you wish. Spawn the projectile at the position and move it along the direction.

Share this post


Link to post
Share on other sites
Quote:
Original post by instinKt
Use a circular array (read std::vector) and a pointer to the next free slot. So as you spawn projectiles you just move the free pointer along one slot. Prevents you from needing to do any searching for a free spot. If the free pointer points to a still active projectile, you've filled your vector.


That doesn't handle all cases. What if you fill up your array, but some projectiles at the start become available again? You will need to update your free pointer to point at one of the new available spaces. However, the next spot may still be in use, so you'll need to check the whole list anyway to see if there's really no space anymore. This all isn't necessary with vectors anyway, as described below.

Quote:
Original post by Side Winder
Well I was going to use a "visible" bool, so that when a bullet isn't visible, and it's first in the array list, it'll be used when the user hits fire.


With an array, you limit yourself to a predefined number of shots, and you'll need to do additional management like making sure projectiles can be re-used correctly, handling the special situation where there are no unused projectiles available anymore, and so on. It'll work, but it's a bit of a hassle. With a std::vector, you can add a projectile when you need one, and remove it when you don't need it anymore. That's all.

Share this post


Link to post
Share on other sites
I do want to limit the number of projectiles I have on the screen though. With an array I can limit that to 10, 20 or whatever; whereas with a vector I'd still have to create some limit so surely it just requires the same amount of work?

The projectiles will have a certain lifespan. Once that "life" is over, the "visible" bool will go false, which means that point can be used again.

Share this post


Link to post
Share on other sites
Quote:
Original post by Side Winder
I do want to limit the number of projectiles I have on the screen though. With an array I can limit that to 10, 20 or whatever; whereas with a vector I'd still have to create some limit so surely it just requires the same amount of work?


That makes sense, limiting the number of projectiles. However, with a vector, you check if it's length equals the maximum number of projectiles allowed - if it does, you add a new projectile, otherwise you don't. With an array, you'll have to search through the array to see if there's a projectile available, and you have to make sure they're properly reuseable (not forgetting to reset variabeles, etc.).

Either way will work just fine here, but I think a vector will take less work.

Share this post


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

  • Advertisement