As far as I can see, you attempt to wrap-over the vectors at the edge of the screen.
First of all, such code would be good only if the object consisted of just one point. If this is some object composed of several 3D facets described by several vectors, you would need to determine leftmost, rightmost, topmost and bottommost points of the object, require it to stop on the boundary, calculate corrected vector shift an then apply it to all other point.
This is probably a reason for gliding off screen you observe.
One minor problem is this: must be
>= windowWidth
and
>= windowHeight
. In last condition,
Y
should be used instead of
X
.
Instead, you need something different. First of all, you will need a class model where you need to be able to calculate those leftmost, rightmost, topmost and bottommost points:
class Model {
System.Collections.Generic.List<vector3> Vectors = new List<Vector3>();
public double Leftmost, Rightmost, Topmost, Bottommost;
internal void Make() {
Leftmost = double.MaxValue;
Rightmost= double.MinValue;
Topmost = double.MaxValue;
Bottommost = double.MinValue;
foreach (Vector3 vector in Vectors) {
if (vector.X < Leftmost) Leftmost = vector.X;
if (vector.X > Rightmost) Rightmost = vector.X;
if (vector.Y < Topmost) Topmost = vector.Y;
if (vector.Y > Bottommost) Bottommost = vector.Y;
}
}
}
You need to use this information to wrap a model where you need it:
Model model = new Model();
double shiftX = 0;
double shiftY = 0;
Model model = new Model();
if (model.Rightmost >= windowWidth)
shiftX = -windowWidth - (model.Rightmost - model.Leftmost);
else if (model.Leftmost < 0)
shiftX = windowWidth - model.Rightmost;
if (model.Bottommost >= windowHeight)
shiftY = -windowHeight - (model.Bottommost - model.Topmost);
else if (model.Topmost < 0)
shiftY = windowHeight - model.Bottommost;
foreach (Vector3 vector in model.Vectors)
vector.Translate(shiftX, shiftY, 0);
Something like that.
—SA