Click here to Skip to main content
15,887,425 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to do boundary checking in XNA Game Studio 4.0, but my models keep gliding off screen, instead of appearing on the other side of the screen. Here is where I edit the position:
C#
if (Position.X > windowWidth)
{
    Position = new Vector3(0, Position.Y, Position.Z);
}
else if (Position.X < 0)
{
    Position = new Vector3(windowWidth, Position.Y, Position.Z);
}

// Y checking.
if (Position.Y > windowHeight)
{
    Position = new Vector3(Position.X, 0, Position.Z);
}
else if (Position.X < 0)
{
    Position = new Vector3(Position.X, windowHeight, Position.Z);
}


Please help!
Posted
Updated 27-Nov-11 13:47pm
v3
Comments
LanFanNinja 27-Nov-11 19:39pm    
Check solution below

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:

C#
class Model {
    //some part of model providing access to all vectors, List for simplicity:
    System.Collections.Generic.List<vector3> Vectors = new List<Vector3>();

    //I don't know the definition of the point, floating point? integer?
    public double Leftmost, Rightmost, Topmost, Bottommost; // should be properties, public fields just for a sample
    internal void Make() {
        Leftmost = double.MaxValue; //sic!
        Rightmost= double.MinValue; //sic!
        Topmost = double.MaxValue; //sic!
        Bottommost = double.MinValue; //sic! 
        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:

C#
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
 
Share this answer
 
v10
Comments
ge-force 9-Dec-11 15:21pm    
Thank you SO much, for this and for that bug fix :P.
Sergey Alexandrovich Kryukov 9-Dec-11 15:35pm    
You are very welcome; and thanks for formally accepting the answer.
(I wonder who was so "clever" to vote 2? a mistake? :-).

Good luck, come again.
--SA
Hello! You have possibly already figured this out but I will post this anyway.
In your Y checking code the else if statement
C#
else if (Position.X < 0)
{
    Position = new Vector3(Position.X, windowHeight, Position.Z);
}


needs to be changed to
C#
else if (Position.Y < 0)
{
    Position = new Vector3(Position.X, windowHeight, Position.Z);
}


Note the change from Position.X to Position.Y. :)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900