Possible Java / Eclipse compiler error? (no, just stupidity as usual)
So I am making a simple 3D graphics program. But I have an error that is not directly related to graphics.
I have a Triangle class that can translate, rotate, etc. I also have a Shape class that shapes shapes from these triangles. So when I want to move a shape, I can use the shape object to move all the shapes evenly.
The error occurs when I try to move the shape. It happens that the translation is for all triangles in a row. But some of them are translated twice.
After a lot of debugging, I found some strange results as it seems that some vertices are taking over the translation of the vertices of the previous iteration , but only some of them . Therefore, if the translation has been doubled.
I would include screenshots, but my reputation is not high enough.
So far, my only explanation is that either part of the vertices is transferring data. Or, data from some of these vertices is stored at runtime and used because the vertices are similar.
Shape class -> contains triangle objects
Triangle class -> contains 3 vertex objects -> also has display and translation methods
Vertex class -> contains double x, double y, double z
It might be worth noting that some vertices for different triangles in the shape class have the same vertices. But that shouldn't be a problem, since they are sent 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 important. The first one should say 1.0, since index 0 said it would be (next check: 1.0). But when we got to the second triangle, it had a translation of the first triangle. Therefore, giving effect again.
This happens not only to everyone.
Thanks for any help or article guidance 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 triangular 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);
}
}
/ \ The form move code rs is a collective space for all triangles, but the error started before this was added. The index is the position at which the triangle is 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);
}
/ \ code for moving the RS class.
Some possibly useful photos. Fine at the moment / \ when else
/ \ Bit is broken. two of them have doubled z values, the other two are exact.
But are they still the same points that go wrong and just as many broken ones?
But some thing is very strange if the same rec is made with four triangles. But obviously this is a hacky way to fix this, and it won't work with more complex forms.
source to share
A bit of a shot in the dark here, as it's hard to figure out what the actual problem is since you posted such little code, but this really stood out to me:
It might be worth noting that some vertices for different triangles in the shape class have the same vertices. But that shouldn't be a problem, since they are sent to different triangles.
I would each Triangle
make a copy of each entry Point
to ensure they don't interfere with other points Triangle
.
i.e:.
public Triangle(Point x, Point y, Point z, byte r, byte g, byte b, double fov)
{
// This is just an example since I have no idea what your
// Point class actually looks like
points = new Point[]{
new Point(x.getX(), x.getY(), x.getZ()),
new Point(y.getX(), y.getY(), y.getZ()),
new Point(z.getX(), z.getY(), z.getZ()),
};
m_r = r;
m_g = g;
m_b = b;
FOV = fov;
}
Update:
To explain what's going on, here's a quick example:
Point.java:
public class Point {
int x;
int y;
public Point(final int x, final int y) {
this.x = x;
this.y = y;
System.out.printf("Created point %s at (%s,%s)%n", this, x, y);
}
void move(int dx, int dy) {
x += dx;
y += dy;
System.out.printf("Moved point %s (%s,%s) from (%s,%s) to (%s,%s)%n", this, dx, dy, x - dx, y - dy, x, y);
}
}
Triangle.java:
public class Triangle {
Point a;
Point b;
Point c;
public Triangle(final Point a, final Point b, final Point c) {
this.a = a;
this.b = b;
this.c = c;
System.out.printf("Created triangle %s with points (%s, %s, %s)%n", this, this.a, this.b, this.c);
}
void move(int dx, int dy) {
System.out.printf("Moving triangle %s (%s,%s)%n", this, dx, dy);
a.move(dx, dy);
b.move(dx, dy);
c.move(dx, dy);
}
}
Demo.java:
public class Demo {
public static void main(String[] args) {
Point p1 = new Point(0, 0);
Point p2 = new Point(1, 0);
Point p3 = new Point(0, 1);
Point p4 = new Point(1, 1);
Triangle t1 = new Triangle(p1, p2, p3);
Triangle t2 = new Triangle(p2, p3, p4);
t1.move(1, 1);
t2.move(1, 1);
}
}
When run, it produces the following output:
Created point Point@448139f0 at (0,0)
Created point Point@7cca494b at (1,0)
Created point Point@7ba4f24f at (0,1)
Created point Point@3b9a45b3 at (1,1)
Created triangle Triangle@7699a589 with points (Point@448139f0, Point@7cca494b, Point@7ba4f24f)
Created triangle Triangle@58372a00 with points (Point@7cca494b, Point@7ba4f24f, Point@3b9a45b3)
Moving triangle Triangle@7699a589 (1,1)
Moved point Point@448139f0 (1,1) from (0,0) to (1,1)
Moved point Point@7cca494b (1,1) from (1,0) to (2,1)
Moved point Point@7ba4f24f (1,1) from (0,1) to (1,2)
Moving triangle Triangle@58372a00 (1,1)
Moved point Point@7cca494b (1,1) from (2,1) to (3,2)
Moved point Point@7ba4f24f (1,1) from (1,2) to (2,3)
Moved point Point@3b9a45b3 (1,1) from (1,1) to (2,2)
As you can see, since the two triangles are separating some point objects, these points move twice.
If we change the constructor in Triangle
to look like this:
public Triangle(final Point a, final Point b, final Point c) {
this.a = new Point(a.x, a.y);
this.b = new Point(b.x, b.y);
this.c = new Point(c.x, c.y);
System.out.printf("Created triangle %s with points (%s, %s, %s)%n", this, this.a, this.b, this.c);
}
instead we get the following output:
Created point Point@448139f0 at (0,0)
Created point Point@7cca494b at (1,0)
Created point Point@7ba4f24f at (0,1)
Created point Point@3b9a45b3 at (1,1)
Created point Point@7699a589 at (0,0)
Created point Point@58372a00 at (1,0)
Created point Point@4dd8dc3 at (0,1)
Created triangle Triangle@6d03e736 with points (Point@7699a589, Point@58372a00, Point@4dd8dc3)
Created point Point@568db2f2 at (1,0)
Created point Point@378bf509 at (0,1)
Created point Point@5fd0d5ae at (1,1)
Created triangle Triangle@2d98a335 with points (Point@568db2f2, Point@378bf509, Point@5fd0d5ae)
Moving triangle Triangle@6d03e736 (1,1)
Moved point Point@7699a589 (1,1) from (0,0) to (1,1)
Moved point Point@58372a00 (1,1) from (1,0) to (2,1)
Moved point Point@4dd8dc3 (1,1) from (0,1) to (1,2)
Moving triangle Triangle@2d98a335 (1,1)
Moved point Point@568db2f2 (1,1) from (1,0) to (2,1)
Moved point Point@378bf509 (1,1) from (0,1) to (1,2)
Moved point Point@5fd0d5ae (1,1) from (1,1) to (2,2)
And this works as expected, since the two triangles no longer separate any point objects.
A good way to avoid such errors is to always make copies of constructor arguments ( copy defaults ), or use immutable objects .
source to share