简体   繁体   English

JavaScript:函数返回最接近的值

[英]JavaScript: function return closest value

I am trying to return the value of the closest object to my position in a function. 我试图将最接近的对象的值返回到函数中的位置。

I tried to put the entities in a array and than I tried return closest object to my position with a for loop, but it did not work. 我尝试将实体放入数组中,然后尝试使用for循环将最接近的对象返回到我的位置,但是它不起作用。 How can I do this? 我怎样才能做到这一点?

function getMyEntity() {
  return Game.currentGame.world.localPlayer.entity.getFromTick();
}

function getOtherEntity() {
  var MyPlayerEntity = getMyEntity();

  var entities = Game.currentGame.world.entities;
  for (var uid in entities) {
    // how i get closest entity to my player entity here?
    var gameObject = entities[uid].fromTick;

    console.log(entities[uid].fromTick.position, MyPlayerEntity.position)
    if (gameObject.entityClass == "Prop" && gameObject.uid !== MyPlayerEntity.uid) {
      return gameObject;
    }
  }
}

function aimAssist() {
  var MyPlayerEntity = getMyEntity();
  var OtherEntity = getOtherEntity();
  if (OtherEntity == undefined) return

  var aimPosition = {
    x: OtherEntity.position.x - MyPlayerEntity.position.x,
    y: OtherEntity.position.y - MyPlayerEntity.position.y
  }
  return aimPosition;
}

I'll give you a bad advice, for now it'll work, as your game grows it will be bad because of O(n^2) complexity. 我会给你一个不好的建议,因为它会随着O(n ^ 2)的复杂性而发展,随着您的游戏的发展,它会变得很糟糕。 Read a bit about quadtrees and see if you can do that. 阅读有关四叉树的知识,看看是否可以这样做。 Meanwhile you can compare the euclidean distance, do not need to take the square root: 同时您可以比较欧几里得距离,不需要取平方根:

Object.keys(entities)
.map(function(d,i){
  var dx = entities[d].fromTick.position.x - MyPlayerEntity.position.x, 
       dy = entities[d].fromTick.position.y - MyPlayerEntity.position.y,
       result = {D:(dx * dx) + (dy + dy), obj:entities[d] , valueOf: function(){return this.D};
  return result;
}).sort(function(a,b){
    return a-b;
})[0].obj; //this returns the closest one

So your original function becomes this: 因此,您的原始功能变为:

function getOtherEntity() {
  var MyPlayerEntity = getMyEntity();

  var entities = Game.currentGame.world.entities;
  return Object.keys(entities)
    .map(function(d,i){
      var dx = entities[d].fromTick.position.x - MyPlayerEntity.position.x, 
           dy = entities[d].fromTick.position.y - MyPlayerEntity.position.y,
           result = {D:(dx * dx) + (dy + dy), obj:entities[d] , valueOf: function(){return this.D};
      return result;
    }).sort(function(a,b){
        return a-b;
    })[0].obj; //this returns the closest one
}

I'd create an array of objects which contain the Object-ID and distance, and sort it by distance. 我将创建一个包含对象ID和距离的对象数组,并按距离对它进行排序。 The first array-item is the closest to the player. 第一个数组项最接近播放器。

The array may look like [{uid: 1234, distance: 12}, {uid: 1235, distance: 16}, ...] 该数组可能看起来像[{uid: 1234, distance: 12}, {uid: 1235, distance: 16}, ...]

You can sort arrays with arrayName.sort(sortingFunction) 您可以使用arrayName.sort(sortingFunction)对数组进行排序

assuming you can get x and y coordinates from gameObject.position and MyPlayerEntity.position you could use a bit of Pitagoras: c^2 = a^2 +b^2, with c being the distance 假设您可以从gameObject.positionMyPlayerEntity.position获取x和y坐标, gameObject.position可以使用一些Pitagoras:c ^ 2 = a ^ 2 + b ^ 2,其中c为距离

let a = gameObject.position.x - MyPlayerEntity.position.x;
let b = gameObject.position.y - MyPlayerEntity.position.y;
let distanceSquared = a*a + b*b;

Since you don't seem to need the exact distance and sqrt() is expensive, you can use the value in distanceSquared and other variables declared outside of the loop to keep track 由于您似乎不需要确切的距离并且sqrt()很昂贵,因此可以使用distanceSquared的值以及在循环外部声明的其他变量来跟踪

let closestDistance;
let closest;

to make the proper comparisons 进行适当的比较

if(distanceSquared < closestDistance){
    closestDistance = distanceSquared;
    closest = gameObject;
}

After you loop through the array, the closest entity reference should be: 遍历数组后,最接近的实体引用应为:

return closest;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM