How can I create two new dynamic objects of my class?

  c++, class, new-operator

Im working on a simple asteroids game using sfml libraries. I have my SpaceObject class which creates an object with its own radius, location on the screen, velocity, and angle. Currently, when asteroids on the screen are hit by the ships bullets they are deleted. My last goal for this project is to cause the asteroids to split into two smaller asteroids when hit by the spaceships bullets. I created a function that creates two new class objects if the asteroid that was hit has a radius greater than 5. However, this function does not seem to create new object classes when I call it in my for loop which deletes the asteroid objects when hit. Asteroids and torpedoes are stored in an array of pointers. Without having to post the entirety of my code, these are the two sections which I am having problems with.

    //initializes asteroid and photon pointer arrays
    //when photon collides with asteroids they are deleted
    for (int x = 0; x < MAX_PHOTONS; x++) {
        for (int i = 0; i < MAX_ASTEROIDS; i++) {
            if (torpedo[x] != NULL && asteroid[i] != NULL) {
                if (objectsIntersect(asteroid[i], torpedo[x])) {
                    splitAsteroids(asteroid[i]); //call to splitAsteroids
                    delete torpedo[x];
                    delete asteroid[i];
                    asteroid[i] = 0;
                    torpedo[x] = 0;
                }
            }
        }
    }
   
//if radius > 5, create 2 new spaceObject classes with half radius and similar location, 
// random velocity, random angle   
bool splitAsteroids(SpaceObject* asteroid) {

double asteroidRadius = asteroid->getRadius(); //retrieves asteroid radius
if (asteroid != NULL) {
    if (asteroidRadius > 5) {
        asteroid = new SpaceObject(ASTEROID, asteroidRadius / 2, asteroid->getLocation(), asteroid->getRandomVelocity(), asteroid->getAngle());
        asteroid = new SpaceObject(ASTEROID, asteroidRadius / 2, asteroid->getLocation(), asteroid->getRandomVelocity(), asteroid->getAngle());

        return true;
    }
}


return false;

}

Source: Windows Questions C++

LEAVE A COMMENT