简体   繁体   English

如何确保怪物不在圈内?

[英]How to make sure that the monster is not in the circle?

I have a sketch https://codepen.io/korolariya/pen/KXQaJK?editors=0011 我有一个草图https://codepen.io/korolariya/pen/KXQaJK?editors=0011

 update(){
    if(!this.prey){    
      return;
    }
    let p = this.calcPointInCircle(this.prey.position,this.lake.position,this.lake.radius);
    this.goToPoint(p);
  }

The monster must pursue the victim without entering the circle. 怪物必须在不进入圆圈的情况下追捕受害者。

Now it moves linearly to the point closest to the fisherman on the circle. 现在它线性移动到最靠近圆圈渔夫的点。

在此输入图像描述

A simple way would be to just move the monster towards the player at a certain speed, but force the monster's position to remain outside the circle. 一种简单的方法是以一定速度将怪物移向玩家,但强迫怪物的位置保持在圆圈之外。

var currentDist = Math.sqrt(monsterX*monsterX+monsterY*monsterY);
var requiredDist = 50;
if (currentDist<requiredDist)
{
    var f = requiredDist/currentDist;
    monsterX *= f;
    monsterY *= f;
}

Not entirely correct as the resulting speed would depend on the arc difference to the player on the circle, but it appears kinda natural: 不完全正确,因为得到的速度将取决于圆圈上玩家的弧度差异,但它看起来有点自然:

Live example (drag player around with mouse, monster will chase) 实例 (用鼠标拖动玩家,怪物会追逐)

Use the algo below: 使用下面的算法:

1- calculate the angle of the line formed by the prey and the center of the lake, let's call this angle alpha : 1-计算猎物和湖心形成的线的角度,让我们称这个角度为alpha

Since tangent of alpha is equal to (yPrey - yLake) / (xPrey - xLake), 由于alpha的正切等于(yPrey - yLake)/(xPrey - xLake),

var alpha = arctan((yPrey - yLake) / (xPrey - xLake));

2- When alpha is calculated in radians, you can calculate where the line intersect the circle in point of cordinates x and y: 2-当以弧度计算alpha时,您可以计算线与坐标x和y的点相交的位置:

var x = lakeRadius * cos(alpha) + xLake;
var y = lakeRadius * sin(alpha) + yLake;

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

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