简体   繁体   English

在java中以给定速度以直线将对象从点(x1,y1)移动到点(x2,y2)的方法

[英]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 的时间,并且pp0p1是 (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:要使对象移动,请执行以下步骤:

  1. [Given: start_time, total_time, x0, y0, x1, y1] [给定:start_time、total_time、x0、y0、x1、y1]
  2. put the circle at (x0, y0) and set time_now = start_time将圆圈放在 (x0, y0) 并设置 time_now = start_time
  3. until time_now == start_time + total_time, calculate (x, y) using the above, move the circle to (x, y), and increment time_now.直到 time_now == start_time + total_time,使用上述计算 (x, y),将圆圈移动到 (x, y),并增加 time_now。

The time increment can be regular wall-clock time as determined by System.getTimeMillis() .时间增量可以是由System.getTimeMillis()确定的常规挂钟时间。

暂无
暂无

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

相关问题 给定两个点 A(x1,y1) & B(x2,y2),我想在球面上找到第三点 C(x3,y3) 和线 AB 之间的距离和 AD 的长度 - Given two points A(x1,y1) & B(x2,y2), I want to find the distance between the third point C(x3,y3) and line AB and length of AD on spherical surface 按钮无法绘制具有坐标(x1,x2,y1,y2)的折线图 - Button not working to draw line graph having its coordinate (x1, x2, y1, y2) 如果我们知道距离x1,y1,y2,则计算x2 - Calculate x2 if we know distance, x1, y1, y2 如何划分字符串(x1,y1)(x2,y2)格式 - How to divide string (x1,y1)(x2,y2) format 如何重新格式化 GetVertices 以返回 (x1,y1,0), (x2,y2,0), (x3,y3,0);? - How to re format GetVertices to return (x1,y1,0), (x2,y2,0), (x3,y3,0);? 在X1 y1 X2 y2形式的10,000个点的文件中,如何检测至少四个正方形? 爪哇 - In a file of 10,000 points of the form X1 y1 X2 y2 ,,, How to detect at least 4 which form a square ? java 如何使用Quartz库每天在XX:XX处安排从日期X1 / Y1 / Z1到日期X2 / Y2 / Z2的某些任务? - How can I use Quartz library to schedule some task from date X1/Y1/Z1 to date X2/Y2/Z2 at XX:XX every day? 如何确定百万数据点中的哪些点 (x,y) 位于矩形 (x1, x2, y1, y2) 所描述的区域内? - How to determine which points (x,y) out of million data points lie inside the area described by a rectangle (x1, x2, y1, y2)? Java - setXY(x1, y1) 和新的 Object 有什么区别 - Java - What the difference of setXY(x1, y1) and new Object 将位图从(x,y)移到(x2,y2)如何计算起始和目标之间的x,y值 - move bitmap from (x,y) to (x2,y2) how to compute x,y values between start and destination
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM