简体   繁体   English

将物体从一个点移动到另一个点

[英]Moving an object from one point to another

I'm trying to get an object to move from one point to a different one in a straight line.我试图让一个物体从一个点沿直线移动到另一个点。 I tried something similar a while back, but it moved until it was aligned with the axis.不久前我尝试过类似的东西,但它一直移动到与轴对齐。 That code looked like this:该代码如下所示:

if(x < otherObject.x) x++;
else if(x > otherObject.x) x--;

if(y < otherObject.y) y++;
else if(y > otherObject.y) y--;

This is a highly inefficient system, and I have been looking at other ways to go about this.这是一个非常低效的系统,我一直在寻找其他方法来解决这个问题。

But I also have to move at a specific speed which is what I have been struggling to solve.但我也必须以特定的速度移动,这是我一直在努力解决的问题。 Example of coordinate movement:坐标移动示例:

-50,200 => 50,-100
300, 300 => 600,230

The goal is to get it to move in a straight line from point A to B, but it should move at an even speed.目标是让它从 A 点到 B 点沿直线移动,但它应该以均匀的速度移动。 Considering this:考虑到这一点:

例子

The goal is to get it to move through the line, and the speed should make it stay on the line(some inaccuracy is acceptable, but it aligning to the X/Y axis(meaning targetX/targetY is equal to currentX/currentY should not happen).目标是让它穿过线,速度应该让它保持在线(有些不准确是可以接受的,但它与 X/Y 轴对齐(意思是 targetX/targetY 等于 currentX/currentY 不应该)发生)。

Any ideas?有任何想法吗?

First of all, you need to forget the concept of movement from a given "source" point to a given "target" point.首先,您需要忘记从给定“源”点到给定“目标”点的运动概念。

Instead, think of your object at any given moment as being at a current point, and moving in a certain direction with a certain speed .相反,将任何给定时刻的对象视为处于当前点,并以特定速度特定方向移动。

You will need a real number to hold the angle (in radians) representing the direction your object is moving.您将需要一个实数来保持表示对象移动方向的角度(以弧度为单位)。 If your object needs to go from source position (sx,sy) to target position (tx,ty) then the angle is computed as follows:如果您的对象需要从源位置 (sx,sy) 移动到目标位置 (tx,ty),则角度计算如下:

float deltaX = tx - sx;
float deltaY = ty - sy;
float angle = Math.atan2( deltaY, deltaX );

You will also need a real number to hold the speed at which your object is traveling.您还需要一个实数来保持物体移动的速度。 Ideally your speed should be expressed in terms of screen units (pixels?) per second, but let's keep things simple and just let speed be expressed in terms of screen units per frame.理想情况下,您的速度应以每秒屏幕单位(像素?)表示,但让我们保持简单,让速度以每帧屏幕单位表示。 "Frame" simply means "whenever you get around to calculating stuff and rendering stuff". “框架”只是意味着“每当你开始计算东西和渲染东西时”。 You may be doing this as fast as you can, or you may be doing it 30 times per second, the choice is yours.您可以尽可能快地执行此操作,也可以每秒执行 30 次,选择权在您。

So, given all of the above parameters, to calculate your object's new position at each frame, use the following:因此,鉴于上述所有参数,要计算对象在每一帧的新位置,请使用以下命令:

currentX += speed * Math.cos( angle );
currentY += speed * Math.sin( angle );

To find out whether your object has reached the "target" point, compute the distance between your object and the "target" point, and if it is small enough, consider it "there".要确定您的对象是否已到达“目标”点,请计算您的对象与“目标”点之间的距离,如果它足够小,则将其视为“在那里”。 Do not expect the current point of your object to ever become equal to the target point, that would require too much precision.不要期望对象的当前点与目标点相等,这将需要太多的精度。

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

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