Greetings. This is not exactly a java gaming question, but since it still involves a java-jogl project, I figure I might try and ask here.
System.arraycopy is a method for copying the contents of one array to another one, and that should be self-evident. It’s supposed to produce reference-independent duplication of arrays. We all know that having two arrays, one empty and another one filled, and writing second=first will simply turn “second” into a second (no pun intended) name for “first”. By using arraycopy, instead, one should be able to have two fully independent arrays with the same contents.
But it doesn’t seem to work for me. I use multi-dimensional arrays to store vertexes for a 3d scene in jogl, and I’d thought I could replicate objects by copying the arrays containing their vertexes. Specifically, I’d do things like:
float[][][] cube = new float[6][4][3]; //6 faces, 4 vertexes each face, 3 coordinates (xyz) each vertex.
cube = fillCubeCoordinates();
then I’d go
float[][][] anotherCube = new float[6][4][3];
System.arraycopy(cube, 0, anotherCube, 0, cube.length);
And then I’d modify this new array:
for (int face = 0; face < 6; face++)
{
anotherCube[face][0][0] += 1.0f; //shear the bottom-left vertex of each face on the x coordinate
}
Now, what I would expect is that, when I’ll run these arrays through the jogl methods to create polygons, they will produce two distinct objects, a proper cube and a sheared one.
Fact is, though, that they will end up having the same contents, those of the sheared cube, as though anotherCube was a second name for cube, just as if I hadn’t used System.arraycopy.
This also happens when copying contents within the same array, and leaves me baffled and puzzled. Does anybody know if I’m doing something wrong or have forgotten something?
Thanks in advance.
