Object in multiple classes

Hi everyone!

I'm working on an assignment for school (I'm remaking the Intro Stage from Mega Man X).
The thing is... I've got a class "Bullet" and a class" Wheel". These aren't 'connected' but I need to get the two connected somehow so I can check if they hit each other (using a Hitregion).
I thought of one solution and that is make the object "Wheel" I made in my Main class public (if that's possible) but I can't seem to figure out how.

Can someone please push me in the right direction?

Thanks in advance!
These classes don't need to know anything about each other.
Rather, there should be a manager class that manages all objects or all objects that have a hitbox. Then you can ask the manager about any collisions, which in turn asks each object whether it collides with the object in question.
Last edited on
Yeah. That's sort of my main class. But the Bullet object gets called in the class which has the information for the player and its movement.
It isn't possible to start this object in the Main class I think because I need the states of the Player (walking, waiting, jumping,...)

Will try your method but I don't think it'll work.

And thanks for the quick response :)
There's no such thing as a "Main class", this isn't Java.

But the Bullet object gets called

You can't call objects (other than functors). You probably mean "created".

Will try your method but I don't think it'll work.

Of course it works. Why do you doubt it?

It isn't possible to start this object in the Main class I think because I need the states of the Player (walking, waiting, jumping,...)

If it is the player that (logically) spawns bullets, then they should be created by the Player class.
Last edited on
Yes. The bullets are created in the Player class.
So... eveything should go back to the class I call Main and be processed there? And the info from the bullets should go through the Player class?
So... eveything should go back to the class I call Main and be processed there?

Not sure what you mean, but probably not. The player class creates new bullet objects. Upon creation, the bullets register themselves with the object manager, so they can be drawn and given processing time in your main loop.
Last edited on
Ok I get it now!
But how should I do the registering? I've never done somehting like that and we haven't learned that in school I think.
It's not something you're taught, it's just a matter of how you design your program so you get the desired results in the easiest way possible. At least for a game like you're describing, this concept works reasonably well.

Basically you have an abstract class called e.g. Object or DrawableObject, which should have at least two abstract functions: draw() and tick(). The first draws the object on a surface/canvas passed to the function, the second one is called every tick to give the object a chance to do something (a bullet has to move and check if it has hit something, for example).

To keep track of all existing objects, there can be a global or per-world container (an instance of the object manager class), where objects register and unregister themselves when they are created and destroyed. The container itself can be a std::list, for example. (Un)registering is simply the process of adding and removing references (raw pointers or smart pointers) from the container.

Your main loop can make use of the managers to draw and tick() all objects in one go.
Ok! Thanks for all the help! I'll try it out!
I'll need to look some things up though but your replies have been a great help already

Thanks again :)
Topic archived. No new replies allowed.