以A,B的一致速度移动对象 [英] Moving an object at a consistent speed from point A to B

查看:170
本文介绍了以A,B的一致速度移动对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建自己的原始2D图形游戏引擎。游戏的核心部分是向敌人发射不同的射弹。我需要让这个组件工作才能继续工作。

I'm attempting to create my own primitive 2D graphics based game engine. The core component of the game is firing different projectiles at enemies. I need to get this component working before I can continue working.

我现在所拥有的东西沿着一条直线穿过起点(x,y)和目标点(x1,x2)。我使用线性函数 y = mx + b 。问题在于我如何更新射弹的位置会导致不一致的速度,具体取决于线的斜率。 (更大的斜率导致它更快地离开)。

What I have now moves my projectile along a line going through the Starting point (x, y) and the Target point (x1, x2). I make use of the linear function y = mx + b. The problem is that how I update the position of the projectile causes an inconsistent velocity depending on the slope of the line. (greater slopes cause it to move away more quickly).

这是我正在运行的游戏循环的核心结构:

Here's the core structure of the game loop I'm running:

    private void executeGameLoop() 
    {

        long nextFrameStart = System.nanoTime();
        while(panel.getRunning()) 
        {
            do 
            {
                panel.update();
                nextFrameStart += FRAME_PERIOD;
            } while(nextFrameStart < System.nanoTime());

            long remaining = nextFrameStart - System.nanoTime();
            panel.repaint();

            if (remaining > 0) 
            {
                try 
                {
                    Thread.sleep(remaining / 1000000);
                } 
                catch(Throwable e) 
                {
                System.out.println(e.getMessage()); 
                }
            }
        }
    }

这个只是更新结构和图形的机制。每次调用 panel.update 时,在特定情况下,射弹会更新其位置。以下是更新射弹的方法:

This is just the mechanism for updating the structure and graphics. Every time this calls panel.update the projectile updates its position, given certain circumstances. Here are the methods that update the projectile:

这告诉射弹它有一个目标并设置有关该线的信息。

This tells the projectile it has a target and sets up information about the line.

public void setHasTarget(boolean hasTargetIn)
    {
        if(hasTargetIn)
        {
            deltaX = getTargetX() - getX();
            deltaY = getTargetY() - getY();
            slope = deltaY / deltaX;
            intercept = getY();
            System.out.println("y = " + slope + "x + " + intercept); //line the projectile will follow
        }
        hasTarget = hasTargetIn;
    }

下一个方法将位置设置为该行的下一步。 (X由速度更新,y根据x更新)

This next method sets the position to the next step on the line. (X is updated by velocity, y is updated dependent on x)

public void setNext()
        {
            float temp = (slope) * (getX() + velocity) + intercept;
            System.out.println("Slope: " + (slope) * (getX() + velocity));
            System.out.println("Current: (" + getX() + ", " + getY() + ")");
            System.out.println("Next: (" + (getX() + velocity)  + ", " + (getY() + temp) + ")");
            setX(getX() + velocity);
            setY(temp);
        }

这最后一个方法调用 setNext()并由主循环调用。

This last method calls setNext() and is called by the main loop.

public void update()
        {
            if(hasTarget)
                setNext();
        }

正如我所说,鉴于我目前的代码,我跑的结果是一种射弹,它以不一致的速度在屏幕上移动,取决于线的斜率。我希望能够更改我的代码,以便射弹在任何轨迹上以一致的速率在屏幕上移动。提前感谢您的帮助。

As I've said, given my current code, the result when I run is a projectile that moves on the screen at inconsistent speeds dependent on the slope of the line. I would like to be able to change my code so that the projectile moves on the screen at a consistent rate over any trajectory. Thank you in advance for any help.

推荐答案

一般来说,处理方向移动的最佳方法是使用三角法。
你的射弹需要两件事:方向(弧度)和速度。

In general the best way to handle directional movement is using trigonometry. Your projectile needs two things for this: direction (in radians) and speed.

你需要的三个三角函数是sin,cos和arctan

The three trig functions you need are sin, cos, and arctan

更新你的X: setX(getX()+(speed * Math.cos(direction)));

更新你的Y: setY(getY()+(speed * Math.sin(direction)));

用于计算方向: Math.atan(斜率)

您应该将字段 direction speed 添加到您的班级并将其声明为双精度。

You should add the fields direction and speed to your class and declare them as doubles.

public void setHasTarget(boolean hasTargetIn)
{
    if (hasTargetIn)
    {
        deltaX = getTargetX() - getX();
        deltaY = getTargetY() - getY();
        direction = Math.atan(deltaY / deltaX); // Math.atan2(deltaY, deltaX) does the same thing but checks for deltaX being zero to prevent divide-by-zero exceptions
        speed = 5.0;
    }
    hasTarget = hasTargetIn;
}

public void setNext()
{
    setX(getX() + (speed * Math.cos(direction)));
    setY(getY() + (speed * Math.sin(direction)));
}

这篇关于以A,B的一致速度移动对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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