Firing from a ship (sprites, C++)

Started by
10 comments, last by Captain P 16 years, 5 months ago
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?
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!
Moved to for beginners as I don't see anything specific to DirectX here.

hth
Jack

<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

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.

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. ;)
Create-ivity - a game development blog Mouseover for more information.
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.
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.
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?
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.
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.
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement