XNA查找距离最近的球员矢量 [英] XNA Find nearest Vector from player

查看:116
本文介绍了XNA查找距离最近的球员矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有载体和PlayerVector我只是想知道我怎么能找到最近的矢量我在我的名单PlayerVector的List。

I have a List of vectors and a PlayerVector I just want to know how I can find the nearest Vector to my PlayerVector in my List.

下面是我的变量

List<Vector2> Positions;
Vector2 Player;



该变量已经声明,所有的,我只需要一个简单的代码,将在最近的位置搜索我的球员。是不是有一个简单的方法?

The variables are already declared and all, I just need a simple code that will search for the nearest position to my player. Isn't there a simple way?

推荐答案

创建一个名为distanceToPlayer INT,将其设置为0。

Create a int called distanceToPlayer, set it to 0.

创建一个名为nearestObject INT将其设置为0。

Create a int called nearestObject set it to 0.

遍历所有有循环的对象。它是比foreach循环稍快,并且是在这种情况下更为有用

Loop through all the objects with a for loop. It is slightly faster than a foreach loop, and is more useful in this situation.

在环

获取具有Vector2.Distance的距离,并检查对distanceToPlayer,如果少了,那么存储在nearestObject对象的索引号,并存储在distanceToPlayer新的距离。

Get the distance with Vector2.Distance, and check it against distanceToPlayer, if less, then store the index number of the object in nearestObject, and store the new distance in distanceToPlayer.

循环完成后,您将有整像素的距离,该项目在存储列表中的索引。您可以使用位置[索引]进入该项目。

After the loop is done, you will have the distance in whole pixels, and the index of the item in the list stored. You can access the item using Positions[index].

这篇关于XNA查找距离最近的球员矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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