简体   繁体   English

在Java中射击预测算法

[英]Shooting prediction algortithm in java

I have an issue in my current game dev hobby. 我当前的游戏开发人员爱好有一个问题。 I have two units, which are hostile in 2D space. 我有两个单位,它们在2D空间中是敌对的。 One is shooting directly at the opponent, so if he moves it misses. 一个是直接向对手开枪,因此如果他移动,就会错过。 But the other should predict it's opponents movement and shoot "ahead". 但是对方应该预测对手的动作并“朝前”射击。

Let's assume first unit is A and second one is B. I can calculate their distances and I have their viewing angle and I have the speed at which they are moving. 假设第一个单位是A,第二个单位是B。我可以计算它们的距离,并确定它们的视角,并确定它们的移动速度。 (player speed and bullet speed are different constants) (玩家速度和子弹速度是不同的常数)

I tried with approximations to calculate distance between A and B and then use the Bv and orientation angle to calculate where the B will be in the next second and then scale that by the distance of two players divided by the bullet speed. 我尝试近似地计算出A和B之间的距离,然后使用Bv和方向角来计算B在下一秒的位置,然后用两个玩家的距离除以子弹速度来缩放。 But this is very inefficient and does not work well. 但这是非常低效的,并且效果不佳。

float distanceK = MathUtil.distance(unit.x, unit.y, opponent.x, opponent.y) / Constants.BULLET_VELOCITY;

float x = (float) (opponent.x + (Constants.UNIT_FORWARD_VELOCITY * distanceK * Math.cos(opponent.orientationAngle)));
float y = (float) (opponent.y + (Constants.UNIT_FORWARD_VELOCITY * distanceK * Math.sin(opponent.orientationAngle)));

float angleToRotate =  MathUtil.angleBetweenUnitAndPoint(unit, x, y);

In the example above I then use angleToRotate variable to determine how much do I have to rotate to hit opponent, but the rotation too takes some time (54deg/s) 在上面的示例中,我然后使用angleToRotate变量来确定要旋转多少才能击中对手,但旋转也需要一些时间(54deg / s)

I would need a more optimal solution for this problem. 对于这个问题,我需要一个更优化的解决方案。

a) predict opponent movement when you are standing still. a)静止不动时预测对手的动作。

b) predict opponent movement when you are moving. b)在移动时预测对手的移动。

You could use a vector representation of the space. 您可以使用空间的矢量表示。 So BA would represent the vector between A and B from A's point of view. 因此,从A的角度来看,BA将代表A和B之间的向量。 You could then add the vector Bv of unit B's velocity. 然后,您可以添加单位B速度的矢量Bv。 The resultant vector would be the vector between B and Bvp the predicted position of B at some time t in the future. 所得向量将是B与Bvp之间的向量,该向量是将来某个时间t处B的预测位置。 As for a moving calculation, you'd need to also account for the movement vector of A. 至于移动计算,您还需要考虑A的移动向量。

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

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