简体   繁体   English

从2点获得子弹X到Y的移动比率

[英]Getting bullet X to Y movement ratio from 2 points

I'm making pretty simple game. 我正在制作非常简单的游戏。 You have a sprite onscreen with a gun, and he shoots a bullet in the direction the mouse is pointing. 屏幕上有一个带有枪的精灵,他按照鼠标指向的方向射出一颗子弹。 The method I'm using to do this is to find the X to Y ratio based on 2 points (the center of the sprite, and the mouse position). 我用来做这个的方法是根据2个点(精灵的中心和鼠标位置)找到X到Y的比例。 The X to Y ratio is essentially "for every time the X changes by 1, the Y changes by __". X与Y的比率基本上是“每次X改变1,Y改变__”。

This is my method so far: 到目前为止这是我的方法:

public static Vector2f getSimplifiedSlope(Vector2f v1, Vector2f v2) {
    float x = v2.x - v1.x;
    float y = v2.y - v1.y;

    // find the reciprocal of X
    float invert = 1.0f / x;
    x *= invert;
    y *= invert;

    return new Vector2f(x, y);
}

This Vector2f is then passed to the bullet, which moves that amount each frame. 然后将此Vector2f传递给子弹,每帧移动该量。

But it isn't working. 但它没有用。 When my mouse is directly above or below the sprite, the bullets move very fast. 当我的鼠标直接位于精灵的上方或下方时,子弹的移动速度非常快。 When the mouse is to the right of the sprite, they move very slow. 当鼠标位于精灵的右侧时,它们的移动速度非常慢。 And if the mouse is on the left side, the bullets shoot out the right side all the same. 如果鼠标在左侧,则子弹射出右侧。

When I remove the invert variable from the mix, it seems to work fine. 当我从混音中删除反转变量时,似乎工作正常。 So here are my 2 questions: 所以这是我的两个问题:

  1. Am I way off-track, and there's a simpler, cleaner, more widely used, etc. way to do this? 我是否偏离轨道,并且有更简单,更清洁,更广泛使用等方式来做到这一点?
  2. If I'm on the right track, how do I "normalize" the vector so that it stays the same regardless of how far away the mouse is from the sprite? 如果我在正确的轨道上,我如何“标准化”矢量,使其保持不变,无论鼠标离精灵有多远?

Thanks in advance. 提前致谢。

Use vectors to your advantage. 使用矢量对您有利。 I don't know if Java's Vector2f class has this method, but here's how I'd do it: 我不知道Java的Vector2f类是否有这种方法,但这是我如何做到的:

return (v2 - v1).normalize(); // `v2` is obj pos and `v1` is the mouse pos

To normalize a vector, just divide it (ie each component) by the magnitude of the entire vector : 要标准化矢量,只需将其(即每个分量)除以整个矢量的大小:

Vector2f result = new Vector2f(v2.x - v1.x, v2.y - v1.y);
float length = sqrt(result.x^2 + result.y^2);

return new Vector2f(result.x / length, result.y / length);

The result is unit vector (its magnitude is 1). 结果是单位矢量(其幅度为1)。 So to adjust the speed, just scale the vector. 因此,要调整速度,只需缩放矢量。

Yes for both questions: 对于这两个问题都是:

  • to find what you call ratio you can use the arctan function which will provide the angle of of the vector which goes from first object to second object 要找到你所谓的比例你可以使用arctan函数,它将提供从第一个对象到第二个对象的向量的角度
  • to normalize it, since now you are starting from an angle you don't need to do anything: you can directly use polar coordinates 规范化它,因为现在你从一个角度开始,你不需要做任何事情:你可以直接使用极坐标

Code is rather simple: 代码很简单:

float magnitude = 3.0; // your max speed
float angle = Math.atan2(y,x);
Vector2D vector = new Vector(magnitude*sin(angle), magnitude*cos(angle));

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

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