XNA Vector3边界检查 [英] XNA Vector3 Boundary Checking

查看:83
本文介绍了XNA Vector3边界检查的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在XNA Game Studio 4.0中进行边界检查,但是我的模型一直滑出屏幕,而不是出现在屏幕的另一侧.这是我编辑职位的地方:

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:

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!

推荐答案

据我所知,您尝试将矢量包裹在屏幕边缘.

首先,仅当对象仅包含一个点时,这样的代码才是好的.如果这是由多个向量描述的几个3D构面组成的对象,则需要确定该对象的最左,最右,最顶和最底点,要求它在边界上停止,计算校正后的向量偏移,然后将其应用于所有另外一点.

这可能是您观察到滑出屏幕的原因.
一个小问题是:必须为>= windowWidth>= windowHeight.在最后一种情况下,应使用Y而不是X.

相反,您需要一些不同的东西.首先,您将需要一个类模型,其中需要能够计算这些最左边,最右边,最上面和最下面的点:

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 {
    //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:

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);



这样的事情.

—SA



Something like that.

—SA


您好!您可能已经知道了这一点,但我还是会发布此信息.
在您的Y检查代码中,else if语句
Hello! You have possibly already figured this out but I will post this anyway.
In your Y checking code the else if statement
else if (Position.X < 0)
{
    Position = new Vector3(Position.X, windowHeight, Position.Z);
}



需要更改为



needs to be changed to

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



注意从Position.X到Position.Y的更改. :)



Note the change from Position.X to Position.Y. :)


这篇关于XNA Vector3边界检查的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆