Post
by albinopapa » April 5th, 2015, 6:52 am
You could also have a separate class called Collidable that has functions that calculate collision between different shapes, and your entity class would inherit from it or any objects that have collision would inherit from it. Say you had some objects that weren't collidable, you wouldn't necessarily want to have collision tests done on them, if there weren't collidable.
Or even like what chili did in the advanced series, CollidableCircle was inherited by a Ship class that had a circular shield. Like cameron said, there are lots of ways to handle collision.
As far as your bullet situation, what you have sounds fine. My advice moving forward, is to think about what each object will have in common. I sometimes make an Entity class to hold, get and set position. A Movable that inherits from Entity that will store, get and set velocity. Then a Bullet class that inherits from Moveable. Item would be a parent class, while Gun/Weapon would inherit from Item, and Weapon would hold a vector for bullets or an array of bullets matching it's magazine size if implemented. Player would inherit from Moveable, and contain a Weapon object and call the Fire function from weapon with the direction and position the player was facing. That information would be passed to the bullets.
The biggest problem I have with this much abstraction is where to update the bullets and how to detect collision. I suppose you could have a function in the player class that gets the bullet vector from the weapon class so you could update and detect collision from the main game loop. That would probably be the easiest. Or, you pass objects into a player class function that passes them to the bullets of the bullet vector in a loop.
Another way you might think about, is having the bullet vector initialized in Game, then passing the vector by reference to the player during initialization that passes it to weapon as reference to weapon, that way the weapon can still use the fire function to launch the bullets, by setting position and velocity and adding it to the vector and still be able to use the vector in the main game loop to check collision with rest of the objects in game.
The biggest problem you might have with that last one is you have to initialize the weapon and player with the ref to a ref, and that means you can't initialize something like Player() or Weapon(). Which won't be a problem if you only have one player or initializing Player classes individually and not in an array. Same is true for Weapon, if only one weapon then no problem. If you are going to have more than one weapon, you might consider using a std::vector<Bullet> * instead of std::vector<Bullet> &.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com