Hello. I have some experience with Java, but I'm finding that programming in an object-oriented sense in C++ is a world different, and I'm a bit lost among all these different pointers and other information.
In particular I'm having some problems keeping a few float values in place in a custom object, Sphere. I have a method called setPosition that should change some values in a particular Sphere object in an array of Sphere objects, but somehow using this method seems to alter the information in other objects in the array! After several hours of experimenting, I'm still pretty stumped and am tempted to think I'm getting pointers tangled up somehow.
Here is the source code:
Sphere.cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
|
#include <iostream>
#include <math.h>
#include <vector>
#include <Inventor/SbLinear.h>
#include <Inventor/actions/SoGetMatrixAction.h>
#include <Inventor/nodes/SoPerspectiveCamera.h>
#include <Inventor/nodes/SoTransform.h>
#include <Inventor/nodes/SoSphere.h>
#include <Inventor/nodes/SoCube.h>
#include <Inventor/nodes/SoLight.h>
#include <Inventor/nodes/SoPointLight.h>
using namespace std;
#include "OSUInventor.h"
#include "Camera.h"
class Sphere {
SbVec3f pos;
SbVec3f color;
bool on;
public:
Sphere ();
//Sphere (SbVec3f);
//Sphere (SbVec3f,SbVec3f);
void setPosition(SbVec3f);
void setOn();
void setOff();
SbVec3f getPosition();
void dumpPosition();
};
// constructor using position as input
Sphere::Sphere () {
cout << "Sphere object: Constructing a new sphere at 0,0,0." << endl;
float x = 0;
float y = 0;
float z = 0;
pos = SbVec3f(x,y,z);
color[0]=255;
color[1]=255;
color[2]=255;
}
// constructor using position as input
//Sphere::Sphere (SbVec3f posInput) {
//cout << "Sphere object: Constructing a new sphere at specified input." << endl;
//pos = posInput;
//color[0]=255;
//color[1]=255;
//color[2]=255;
//}
// constructor using position as input
//Sphere::Sphere (SbVec3f posInput, SbVec3f colorInput) {
//cout << "Sphere object: Constructing a new sphere at specified input, with color!" << endl;
//pos = posInput;
//color[0]=colorInput[0];
//color[1]=colorInput[1];
//color[2]=colorInput[2];
//}
void Sphere::setPosition (SbVec3f posInput) {
pos = posInput;
}
void Sphere::setOn () {
on = true;
}
void Sphere::setOff () {
on = false;
}
SbVec3f Sphere::getPosition () {
return pos;
}
void Sphere::dumpPosition () {
cout << "Sphere object: Dumping position." << endl;
cout << "X: " << pos[0] << endl;
cout << "Y: " << pos[1] << endl;
cout << "Z: " << pos[2] << endl;
}
|
And I'm trying to manipulate my items here, in this snippet from main:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
Sphere sphereArray[3];
int sphereArrayIndex = 0;
sphereArray[0].dumpPosition();
sphereArray[1].dumpPosition();
sphereArray[2].dumpPosition();
cout << "-------------------" << endl;
// SPHERE CONSTRUCTOR
float x0 = 1;
float y0 = 1;
float z0 = 1;
SbVec3f coord = SbVec3f(x0,y0,z0);
sphereArray[0].setPosition(coord);
sphereArray[1].setPosition(coord);
sphereArray[2].setPosition(coord);
sphereArray[0].dumpPosition();
sphereArray[1].dumpPosition();
sphereArray[2].dumpPosition();
cout << "-------------------" << endl;
float x1 = 2;
float y1 = 2;
float z1 = 2;
SbVec3f coord2 = SbVec3f(x1,y1,z1);
//sphereArray[1].setPosition(coord2);
sphereArray[0].dumpPosition();
sphereArray[1].dumpPosition();
sphereArray[2].dumpPosition();
cout << "-------------------" << endl;
|
My first "dump" shows that each Sphere still holds position 0,0,0. However, when I dump the values after calling the first setPosition, the values end up like this:
1 2 3 4 5 6 7 8 9 10 11 12
|
Sphere object: Dumping position.
X: 0
Y: 2.277795e-041
Z: 2.277795e-041
Sphere object: Dumping position.
X: 0
Y: -8.82818e-044
Z: -8.82818e-044
Sphere object: Dumping position.
X: 1
Y: 1
Z: 1
|
The first two sphere objects should also all have 1,1,1 values, but they have these values that seem kind of like floating point errors. And the attempt to put 2,2,2 doesn't seem to work any better.
Is anyone able to point out what I might be doing wrong here? Thanks!