• Advertisement
Sign in to follow this  

Multiple projectile behavior handling

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

Hi, first post I make here! I've really tried for a long time now, but I cannot seem to find any good way of handling multiple projectiles with different behavior. My first attempt have been to make a base class an inherit the different behaviors I need and use virtual functions. I simply just want the player and enemies to be able to shoot projectiles in patterns, but to make some of the patters the bullets cannot just go straight forward. Does anyone have any better suggestions? I'm completely stuck here...

Share this post


Link to post
Share on other sites
Advertisement
Hey Wkter!

To be of better assistance to you, we need to know what language and libraries you are using. I'll try to help with what I have, but I'm making a stab in the dark. [smile] Bullets are obviously something you can expect to be flying around the scene, often in great numbers. Calling virtual functions for each one will put your game in an awful bottleneck. Try applying properties to bullets via a bitfield. I'm using C++.

struct bullet
{
enum properties
{
BULLET_EXPLOSIVE = 0x01,
BULLET_ARMOR_PIERCING = 0x02,
BULLET_SCATTER_SHOT = 0x04,
BULLET_RICOCHET = 0x08,
};

unsigned m_bfProperties : 4;
unsigned short X, Y, XSpeed, YSpeed;

void processBullet(); // <- Determine different behaviors here.
};


This is a minimalistic example, but hopefully you get the idea. Another thing we may want to know is how your game scene is laid out. Is it a top down shooter, a side scroller or 3D? All of these can have different ways of processing patterns in shots.

Cheers!
-Zyro

Share this post


Link to post
Share on other sites
Thanks for the answer!

It's a 2D top down shooter. I just need simple bullets that if hit a player, kills. What I need to do is to make the bullets sometimes do a little more than move along the X and Y in a fixed direction.

I want to try to make as much of this game myself, so I can learn as much programming in general as possible and will have code and engines for reuse later, therefore I only use the standard, including DirectX. In C++, of course.

Thanks!

Share this post


Link to post
Share on other sites
I don't think a few virtual calls would be so bad for performance it would kill the game. What was wrong with this method?

I would make 1 class per path type. For example, StraightProjectile, CircularProjectile, PathProjectile, etc. When you create your projectiles, you set some parameters for that class, like a direction and speed for StraightProjectile, a radius for CircularProjectile or a point list for PathProjectile. The update function then uses these parameters to determine the next position.

Share this post


Link to post
Share on other sites
Or use a strategy pattern kind of approach. i.e. Have all Projectile objects contain a member deriving from type ProjectilePathStrategy, that implements a MoveProjectile(Projectile p) method. Then when you call Move() on the projectile it can defer the actual behaviour to it's strategy class. Any time you need a new path behaviour you just derive a new class from ProjectilePathStrategy and snap an instance of it in place without having to touch any of the projectile code.

Using this sort of pattern you can compose game entities from any combination of behaviours, for instance you could have separate behaviours for movement, shooting, dancing, romancing... whatever you need. Then you can build up an entity that moves in a circle and shoots once per second by snapping in the relevant strategies. If you want an object to remain stationary you can either not supply a movement strategy and rely on a null check at runtime, or create a default no-operation strategy that can be called safely but does nothing.

Share this post


Link to post
Share on other sites
By patterns, do you mean like a shotgun blast compared to a sniper shot? The weapons that fire the projectiles might be the best place to handle those differences.

In pseudo code:

Shotgun.Fire
- obtain 20 projectiles
- for each projectile
---- set projectile properties (speed, graphic, etc)
---- set projectile heading to weapon heading
---- alter projectile heading slightly (shotgun effect)
---- fire projectile

or

SniperRifle.Fire
- obtain 1 projectile
- set projectile properties
- set projectile heading to weapon heading
- fire projectile

Share this post


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

  • Advertisement