#Creating multiple instances of an object

17 messages · Page 1 of 1 (latest)

west pivot
#

So i have this code :
https://github.com/Itex34/2d-Shooting-Game/blob/main/Gun.cpp

and i want to create multiple instances of the pellet object with different velocity directions.

I'm kind of stumped and i feel like my code is kind of messy so it's hard to figure a solution out.

The lines that calculate the velocity are these two :
pelletVelX = pelletSpeed * cos((initialRotationAngle - 47) * M_PI / 180);
pelletVelY = pelletSpeed * sin((initialRotationAngle - 47) * M_PI / 180);

Lines 39 and 40 in my code.

The thing is the pellet math is done in the gun.cpp file and not the pellet.cpp file.
Maybe I should move the math to the pellet.cpp file?

GitHub

A 2d shooting game i'm working on. Contribute to Itex34/2d-Shooting-Game development by creating an account on GitHub.

night sailBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

west pivot
#

Should i create a vector of pellets?

uncut jasper
#

If you want multiple pellet objects you just replace your single global pellet by passing in the pellet you want to update or render, or you change it to std::vector<Pellet> pellets; if you have a reason to keep it global.

#

The first would be better.

#

There are multiple ways to organize this. Currently the gun renders pellets which is weird. Pellets rendering themselves makes more sense.

#

I also recommend you not to use new. It will make the whole code a lot easier.

west pivot
uncut jasper
#

Banning new from your codebase is a good use to fix memory leaks.

#

Smart pointers are more of an emergency measure.

#

Values and references will get you most of the way there.

#

There is no good reason why pellet has to be a pointer. Make it a value.

#

If it must be optional use std::optional<Pellet>

west pivot
#

i think it was some kind of sdl memory allocation issue