[英]Method to move an object from point(x1,y1) to point(x2,y2) at a given speed in a straight line in java
I have to write a method that moves an object (eg circle) in a straight line from one coordinate to another with a given speed.我必须编写一种方法,以给定的速度沿直线将对象(例如圆)从一个坐标移动到另一个坐标。 The object must get to the target point and stop.
对象必须到达目标点并停止。 The speed correlates to the time in which it takes the object to reach the point (speed = 15 is equivalent to time = 15 ms for example).
速度与物体到达该点所需的时间相关(例如,速度 = 15 相当于时间 = 15 毫秒)。 If someone could help me with the maths here, I would be garateful, please.
如果有人可以在这里帮助我计算数学,我会很高兴,拜托。
The interpolation formula for moving from point p0 to point p1 at constant speed is:从 p0 点匀速移动到 p1 点的插值公式为:
p(t) = p0*(1-t) + p1*t
where t
is time that has been scaled to vary from 0 at the start to 1 at the end and p
, p0
, and p1
are (x,y) coordinate pairs.其中
t
是已缩放为从开始时的 0 到结束时的 1 的时间,并且p
、 p0
和p1
是 (x,y) 坐标对。 Since Java doesn't have a built-in way to write the interpolation formula, you just apply it to the x and y components in parallel.由于 Java 没有编写插值公式的内置方法,因此您只需将其并行应用于 x 和 y 分量。 The result is:
结果是:
t = (time_now - start_time) / total_time;
x = x0*(1-t) + x1*t;
y = y0*(1-t) * y1*t;
This is the core calculation.这是核心计算。 To get the object to move, you follow these steps:
要使对象移动,请执行以下步骤:
The time increment can be regular wall-clock time as determined by System.getTimeMillis()
.时间增量可以是由
System.getTimeMillis()
确定的常规挂钟时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.