So I'm making a simple 3D graphics program. But I have a bug which is not directly related to the graphics.
I have a Triangle class that can translate, rotate, etc. I also have a Shape class that makes shapes from these triangles. So when I want to move a shape I can use a shape object to move all shapes uniformly.
The bug occurs when I try to move a shape. What happens is the translation is meant to be passed to all triangles in a row. But some of them are having the translation doubled.
After a lot of debugging I have found some weird results as it seems that some of the vertexes take on the translation of the vertex of the previous iteration, but only some of them. So therefore having the translation doubled.
I would include screenshots but my rep is not high enough.
My only explanation so far is either that either some of the vertexes share data. Or the data of some of these vertexes is being stored in run time and being used because the vertexes are similar.
Shape Class -> holds Triangle Objects
Triangle Class -> holds 3 Vertex Objects -> also has methods for display & translation
Vertex Class -> holds double x, double y, double z
May be worth noting that some of the vertexes for different triangles in the shape shape class have the same vertexes. But this should not be an issue as they are passed to different triangles.
Index: 0 // First Triangle
next check: 1.0 // The z value of the first vertex int the next triangle(index 1)
1.0 // z val. before translation
1.010356248053852, dz: 0.009999833334166664 // z val. after translation
1.0 // z val. before translation
1.010356248053852, dz: 0.009999833334166664 // z val. after translation
1.0 // z val. before translation
1.010356248053852, dz: 0.009999833334166664 // z val. after translation
Index: 1 // Second triangle
next check: 1.5 // next check
1.010356248053852 // z val. before translation
1.0207124961077039, dz: 0.009999833334166664 // z val. after translation
The last two lines are the important ones. The first one should say 1.0, as the Index 0 said it would be (next check: 1.0). Yet when we get to the second triangle it has had the translation of the first triangle. So therefore giving the effect again.
This does not happen to all only some.
Thanks for any help or direction to articles in advance.
public Triangle(Point x, Point y, Point z, byte r, byte g, byte b, double fov)
{
points = new Point[]{x, y, z};
m_r = r;
m_g = g;
m_b = b;
FOV = fov;
}
public void move(double x, double y, double z, double delta)
{
for (int i = 0; i < 3; i++)
{
points[i].setDX(points[i].getDX() + (x * delta));
points[i].setDY(points[i].getDY() + (y * delta));
points[i].setDZ(points[i].getDZ() + (z * delta));
}
}
/\ important triangle code
public void move(double x, double y, double z, double delta)
{
for (int i = 0; i < index.length; i++)
{
rs.triMove(index[i], x, y, z, delta);
}
}
/\ Shape move code rs is a collective space for all triangles but the bug started before this was added. The index is what position the triangle is being held in the array in rs.
public void triMove(int index, double x, double y, double z, double delta)
{
triangles.get(index).move(x, y, z, delta);
}
/\ RS class movement code.
Some maybe helpful pics.
Fine at the moment /\ when still
/\ Bit broken now. two of these have doubled z values the other two are fine.
But they are still the same points going wrong and the same amount of broken?
But Some thing very Strange if the same rec is made with four triangles.
But obviously this is a hacked way of fixing this and it won't work with more complex shapes.