BillyMays
BillyMays

Reputation: 99

How to change an object's properties in another class' function in C++?

I have a class called Object which has the following attributes: xPos and yPos. It also has getters and setters for these attributes. I have another class called CollisionDetector which has a function called detect that takes in two objects and detects if they have collided. If they have, then it pushes them away from each other using the setXPos() and setYPos() functions. However, it doesn't change the values and I'm trying to figure out why?

Object::Object(){

}

Object::Object(float x, float y) {  //constructor
    xPos = x;
    yPos = y;
}

float Object::getXPos(){
    return xPos;
}

float Object::getYPos(){
    return yPos;
}

void Object::setXPos(float temp){
    xPos = temp;
}

void Object::setYPos(float temp){
    yPos = temp;
}

char Object::getType(){
return type;
}

bool CollisionDetector::detect(Object a, Object b){
    double size = 45; //Set size
    double xPosition = a.getXPos() - b.getXPos();
    double yPosition = a.getYPos() - b.getYPos();
    double sumRadius = (size/2 + 1) + (size/2 + 1);
    double radiusSquared = sumRadius * sumRadius;
    double distanceSquared = (xPosition * xPosition) + (yPosition * yPosition); //square the distances

    if (distanceSquared <= radiusSquared)
    {
        //Check for collisions between tanks
        if(a.getType() == 't' && b.getType() == 't'){
            if(a.getXPos() > b.getXPos()){
                a.setXPos(a.getXPos() + 10);
            }
            else if(a.getXPos() < b.getXPos()){
                a.setXPos(a.getXPos() - 10);
            }
            //Same for y direction
            if(a.getYPos() > b.getYPos()){
                a.setYPos(a.getYPos() + 10);
            }
            else if(a.getYPos() < b.getYPos()){
                a.setYPos(a.getYPos() - 10);
            }
        }
        return true;
    }
    return false;

}

The function is called from main using: collisionDetector.detect(userTank, enemyTank);

Upvotes: 1

Views: 3623

Answers (2)

deW1
deW1

Reputation: 5660

bool CollisionDetector::detect(Object &a, Object &b)

You gotta give the function the actual Object as reference as I did in the example above.

Right now you're creating a copy of the variables so you're not modifying the original.

Different possible option would be with a pointer to the object like:

bool CollisionDetector::detect(Object *a, Object *b)

Just search the keywords reference and pointer in combination with c++ and you'll find a ton of articles and tutorials about it.

Explaining everything would be beyond the scope.

Upvotes: 2

newfolder
newfolder

Reputation: 108

I was right in comment above - look at this function -
bool CollisionDetector::detect(Object a, Object b)
You are passing objects as value, so they are copied elsewere, and function operate on this copies. If you want to change original objects, pass them by pointer or reference.
Pointer : bool CollisionDetector::detect(Object* a, Object* b)
Reference : bool CollisionDetector::detect(Object &a, Object &b)

Upvotes: 0

Related Questions