让敌人以相同的速度走 [英] Let an Enemy follow a path with the same speed

查看:92
本文介绍了让敌人以相同的速度走的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始用Java编写游戏.我有一个敌人和一个Player,一个基本的2D游戏,那就是第一次获得经验.

I started programming a game in java. I have Enemys and a Player, a basic 2D game, thats to get experience for the first time.

现在,我希望敌人遵循一条路径,可以将其绘制到关卡编辑器"中. 我有一个JPanel,一个mouseMoveListener,然后单击Path2D开始将mouseMove点保存到Path2D.Double对象.

Now i wanted the Enemys to follow a path, which i can draw in to a "level editor". I have a JPanel, a mouseMoveListener, and on click the Path2D starts saving the mouseMove Points to a Path2D.Double Object.

在那之后,我实现了以下方法来使敌人沿着这条路前进:

After that, i implemented the following method to make the enemys following this path:

public void forward(){
    if(!pathIterator.isDone()){
        pathIterator.currentSegment(current);
        x = current[0];
        y = current[1];
        pathIterator.next();
    }
    else {
        dead = true;
    }
}

我认为现在很清楚发生了什么:敌人正在跟随,但是速度是我移动鼠标的速度.因此,如果我快速移动到Mouse,敌人就会从一个点跳"到另一个点.放慢脚步,在那一点上偷偷摸摸". (而且因为我不是机器人,所以我无法以相同的速度^^移动鼠标)

I think its clear what happens now: The Enemy is following, but the speed is that i moved the mouse with. So if i move to Mouse to fast, the enemy just.. "jumps" from one point to an other. To slow, its "sneaking" over that points. (And because im not an Robot, i cannot move the Mouse with the same speed ^^)

机器人对话:是的,我可以让awt.Robot移动我的鼠标.但这实际上也是不可能的,因为我必须绘制复杂的路径,而这些路径后面没有任何可见的数学.

Talking of Robot: Yes, i could let a awt.Robot move my Mouse. But this isnt really possible too, because i have to draw complicated paths, which dont have any visible mathematics behind.

所以,我想让这个敌人以相同的速度在这条路径上移动.我的问题:我不知道在哪里实现修复".我有2个主意:

So, i want let this Enemys to move on this path with the same speed. My Problem: I don't know where to implement a "fix". I have 2 Ideas:

  • 也许我可以进行Path创建:不仅可以将点添加到Path2D中,还可以计算快速移动的点之间的点,或者删除彼此靠近的点.但是:首先,我不知道该如何计算(是否有任何数学逻辑可以实现这一目标?)其次,当我这样做时,我很可能无法改变敌人在游戏中的速度,而那会很糟糕

  • Maybe i could work on the Path creation: Instead of just adding the Points to the Path2D, maybe i could calculate points between the points where i moved to fast, or deleting points which are to near by each other. But: First, I don't know how to calculate this, (Is there any maths logic to achieve this?) And Second, when i do this i probably would'nt be able to change the speed of the enemys ingame, and that would be bad

我的第二个想法是计算之间的点(跳过的点),这应该相对于实际速度在每一帧发生.但是我在数学方面还没有足够的经验.

The second idea i have, is to calculate the points between (oder the points to jump over), this should happen every frame, relative to the actual speed. But here im not experienced enough in maths to.

那么,这种方法是否可行?如果可以,有人知道如何计算吗? 而且,如果没有,我还必须实现什么其他可能性?

So, are that ways possible, and if yes, has someone an idea how to calculate this? And, if not, what other possibilitys i have to achieve this?

谢谢,抱歉英语不好!

Thank you, and sorry for the bad english!

推荐答案

您需要做的就是定义敌人在敌人职业内部的移动速度.

All you need to do is define the speed of movement of the enemy inside the enemy class.

计算出下一个要移动的点,然后通过从新位置减去当前位置来创建方向矢量.

When it works out the next point to move to then create a direction vector by subtracting the current position from the new position.

归一化方向矢量(长度为1),然后乘以速度和tpf(每帧时间).

Normalize the direction vector (so it is length 1) then multiply it by the speed and the tpf (time per frame).

移动该数量而不是跳到下一个点.

Move by that amount instead of jumping to the next point.

(请注意,如果这些点非常靠近或帧速率太低,可能会导致它过冲一点,但是应该没问题).

(Note if the points are very close together or the framerate is low this can cause it to overshoot a bit, it should be fine though).

这篇关于让敌人以相同的速度走的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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