Side-scrollers
In most side scrollers, the player has a projectile weapon. How do most games implement this? With a class? I'm not sure how to implement it.
I allocate an array of structs called "bullet."
Then I have a function that calculates the new position of each bullet, a function that calculates if it hits someone/something (and does something about it if it does), and a function that renders bullets, all depending on whether or not the bullet is 'active.'
Then I have a function that calculates the new position of each bullet, a function that calculates if it hits someone/something (and does something about it if it does), and a function that renders bullets, all depending on whether or not the bullet is 'active.'
A common method is to employ a linked-list - basically a dynamic way to store objects where the number of instances will change at runtime. Google it or search on Gamedev.net. There is also the Standard Template Library (STL – C++ ) which provides a stable set of templated classes to handle dynamic containers (such as a list) – very easy to use and understand.
Good luck,
Jackson Allan
Good luck,
Jackson Allan
Remember that bullets usually move very fast (unless you deliberately make some slowed down effect). On slow computers (slow = low FPS in this case), a standard point in box collision would not always manage to see a collision. This is because if a projectile moves 20 pixels in one single fram any object in between will be missed if it is small enough.
Rather, the collision detection you *ought* to do for projectiles would be to make a line interesection test, and treat the distance passed in that particular frame as the vector. This can be quite easy if the movement of the bullet's movement also is parallel to either the vertical or horizontal axes.
Rather, the collision detection you *ought* to do for projectiles would be to make a line interesection test, and treat the distance passed in that particular frame as the vector. This can be quite easy if the movement of the bullet's movement also is parallel to either the vertical or horizontal axes.
I implement a class hierarchy as such:
That's a very simplified version of the hierarchy but the point is a character would contain a pointer to a weapon that contains a pointer to a projectile. When the character fires, the weapon invokes a member function that informs the engine that a new projectile of a given type needs to be spawned.
And then everything just has it's own virtual functions for performing updates and whatnot. Projectiles of different types (hitscan vs slow moving, big vs point-sized, etc.) just need to be defined as a new class and some stuff implemented.
CTarget+CThing +CWeapon +CProjectile +CCharacter
That's a very simplified version of the hierarchy but the point is a character would contain a pointer to a weapon that contains a pointer to a projectile. When the character fires, the weapon invokes a member function that informs the engine that a new projectile of a given type needs to be spawned.
And then everything just has it's own virtual functions for performing updates and whatnot. Projectiles of different types (hitscan vs slow moving, big vs point-sized, etc.) just need to be defined as a new class and some stuff implemented.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement