how to OOP-code bullets? [SOLVED]

The Partridge Family were neither partridges nor a family. Discuss.
Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

how to OOP-code bullets? [SOLVED]

Post by Clodi » April 4th, 2015, 11:35 pm

Hello guys,

Happy Easter everyone!
Has anybody got a fair amount of experience in making video games with bullets in them? I have two characters. One arena and the only way to victory is to shoot the opponent. what is (in your opinion) the most natural / safe / less-painful OOP way of coding bullets and weaponry?

I was thinking:

class player inherits from class entity.
class item inherits from class entity and one instance of it is contained within player.
class bullet inherits from class entity and (let's say 10) instances of it are contained within item.

Is this anything like something you woud personally do?

Bye,

Code: Select all

Clodi
:D
Last edited by Clodi on April 8th, 2015, 7:28 pm, edited 1 time in total.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: how to code bullets?

Post by cameron » April 4th, 2015, 11:42 pm

Its really up to you, there are hundreds of ways of doing this. I would have a vector of bullets with a add, remove functions. From there you can access the bullets and call bullets.Collides(object) and bullets.SetVelocity(vel) or something along those lines. It is really up to you though.
Computer too slow? Consider running a VM on your toaster.

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: how to code bullets?

Post by Clodi » April 4th, 2015, 11:46 pm

Nice one, thanks a lot mate.

I was wondering about the "collision" function as well actually..

Would a Game class handling the collisions between objects be better?
something like "bool collision( obj1, obj2 );" ? or should subclasses themselves have collision-handling functions such as "entity.collide( obj );" in my case, following on to what you wrote?

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: how to code bullets?

Post by cameron » April 5th, 2015, 4:39 am

Depends if it was oop or not. In oop you would have most, if not all things inheriting from entity and in entity you would create an interface for your other classes. Although, I must stress that your initial setup depends on your coding style.
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: how to code bullets?

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

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: how to code bullets?

Post by Clodi » April 5th, 2015, 9:08 am

OK, my question is:

Imagine this set-up:

ENTITY <--- PLAYER <--- PLAYER1

ENTITY <--- ITEM

Can you have a thing such as "Item.collide( player1 )" ? or "Entity.collide( Entity )" ?

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: how to code bullets?

Post by Clodi » April 5th, 2015, 9:10 am

Because the alternative would be to have a function in GAME called "collision( Entity, Entity )" which is trivial, messy and I have already implemented..

..or even "collision()" with no parameters whatsoever would work since the GAME knows about all its objects within it.

cameron
Posts: 794
Joined: June 26th, 2012, 5:38 pm
Location: USA

Re: how to code bullets?

Post by cameron » April 5th, 2015, 3:01 pm

If all entities were collideable that would be a good setup. Also, yes you can have "Item.collide( player1 ) or Entity.collide( Entity ) with a setup like that.
Computer too slow? Consider running a VM on your toaster.

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: how to code bullets?

Post by albinopapa » April 5th, 2015, 6:37 pm

Entity::Collide(Entity *) or
Entity::Collide(Entity &)

The function wouldn't have to be defined in Entity, but at least declared there as a virtual function, then declared and defined in the child classes.
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

Clodi
Posts: 175
Joined: November 18th, 2012, 7:47 pm

Re: how to code bullets?

Post by Clodi » April 6th, 2015, 3:49 pm

Thank you so much guys. :) I have always been thinking: "you can't have a a function in Entity that somehow has something to do with another Entity otherwise the compiler would try to understand what Entity is, it would go through all of Entity's functions and realise that there is a reference to a thing called Entity, so it would basically loop and loop reaching a dead end but apparently there's a few things about programming that I still can't understand :D

Post Reply