- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在尝试进行物理模拟,其目的是模拟一枚导弹从很远的距离发射(在我的例子中,我使用了1.6公里),并且我在这个1.6公里的距离上有一个防空武器;当导弹发射时,通过数学,我已经计算出何时需要释放防空武器来击中导弹。数学太酷了,我 99% 确定这在数学上应该可行;但错误似乎发生在动画阶段,我觉得这与我处理重力的方式有关。
问题在于导弹的飞行路径不是遵循可爱的抛物线,而是采用由许多不同线性函数组成的路径(因此是欧拉积分推测(来源: http://natureofcode.com/book/chapter-5-physics-libraries/ ));我相信这与我的规模有关。我使用的比例为 1 像素 = 1 米。图像每 60 秒更新一次,所以我是说每个周期,取垂直速度 - (9.81/60),然后更新位置 (垂直速度/60)。问题是,它只想四舍五入到最近的像素,所以在前 2 秒左右,它只想让垂直位置以每个周期 4 个像素的速度变化,然后变成 5,然后 6...这导致飞行路径(当导弹以 50 度以 200m/s 的速度发射,防空炮以相同速度以 70 度发射时)如下所示:
如果有人知道我如何解决这个问题,将不准确的线性表示变成一个对时间仍然准确的抛物线表示(发射后的时间约为 3 秒)。如果有人有任何建议和/或解决方案,或者如果您能够解释为什么会发生这种情况并且不介意花一点时间向我解释以更好地理解问题,我们将不胜感激!如果您需要更多信息来帮助您帮助我,请发表评论,我将为您提供任何信息。相关代码片段是:
public void gravity(){
for (int i = 0; i < rockets.size(); i++){
if(timeToStart <= milliSecondTimer){
rockets.get(1).fired = true;
//Trail of other rocket
if (milliSecondTimer > 0.1 * count){
rockets.add(new Ball(rockets.get(1).x - 20 ,rockets.get(1).y - HEIGHT + 100,5,0,0,false));
}
}
if(rockets.get(i).fired){
rockets.get(i).vSpeed -= 9.81 / 60;
rockets.get(i).move(0, (int) (rockets.get(i).vSpeed / 60));
rockets.get(i).move(1, (int) (rockets.get(i).hSpeed / 60));
} else if (timeToStart==1110){
//function to work out the time displacment
derr(1);
}
//For the trail
if (milliSecondTimer > 0.1 * count){
rockets.add(new Ball(rockets.get(0).x - 20 ,rockets.get(0).y - HEIGHT + 100,5,0,0,false));
count++;
}
}
}
public static void main(String[] args) throws InterruptedException{
JFrame frame = new JFrame("App Name");
Rockets app = new Rockets();
frame.setSize((int)(WIDTH * SCALER),(int)(HEIGHT * SCALER));
frame.add(app);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setResizable(false);
frame.requestFocus();
long lastLoopTime = System.nanoTime();
int fps = 0, lastFpsTime = 0, count = 1;
final int TARGET_FPS = 60;
final long OPTIMAL_TIME = 1000000000 / TARGET_FPS;
//Game Loop
while(true){
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
delta = updateLength / ((double)OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime > 100000000 * count){
milliSecondTimer += 0.1;
count++;
}
if (lastFpsTime >= 1000000000){
System.out.println("(FPS: "+fps+")");
lastFpsTime = 0;
fps = 0;
count = 1;
}
loopsGone++;
app.repaint();
Thread.sleep( (lastLoopTime-System.nanoTime() + OPTIMAL_TIME)/1000000 );
}
}
谢谢,
山姆
最佳答案
也许你可以将你的 x 和 y 火箭坐标更改为 float (我假设它们现在是 int )。然后当你绘图时,就是添加 (int) 强制转换的时候。例如,
rockets.get(i).move(0, (int) (rockets.get(i).vSpeed / 60));
不应进行 int 强制转换。
而且,你会想要改变
(rockets.get(i).vSpeed / 60));
到
(rockets.get(i).vSpeed / 60.0));
您希望您的位置保持精度,但目前 int 转换并未做到这一点。这就是使用 float 所能达到的效果。
看起来规模并不是问题所在。如果您尝试在 10 x 10 像素的图像上绘制抛物线,这将是一个问题。
你能发布你的火箭类(class)吗?我想运行这个,看起来很有趣。
感谢您接受我的回答。这是我看到的另一个问题。
为了获得更好的位置精度,您需要更频繁地更新位置函数,这意味着每次重新绘制时都不止一次。这就是我的游戏循环的样子。
public void run() {
long lastTime=System.nanoTime();
final double amountOfTicks=60.0;
double ns=1000000000/amountOfTicks;
double delta=0;
int updates=0;
int frames=0;
long timer=System.currentTimeMillis();
while (running) {
long now=System.nanoTime();
delta +=(now-lastTime) / ns;
lastTime=now;
if (delta>1) {
update(); <--- this is where your gravity() method belongs
delta--;
updates++;
}
render(); <---- this will be your repaint method
frames++;
if (System.currentTimeMillis()-timer>1000) {
timer+=1000;
currUpdates=updates;
currFrames=frames;
updates=0;
frames=0;
}
}
}
它的作用是每毫秒更新一次宇宙飞船位置,但仅以 60 fps 的速度渲染。
关于java - 为什么在处理大型/速度射弹时会出现不准确的情况,是由于欧拉积分吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42742643/
我在想出一个算法时遇到了麻烦... 我有一系列 GPS 数据,以 1 秒为间隔记录时间、速度、距离。假设距离是米,速度是米/秒。可能有超过 2 小时的数据,或 7200 个点。这里的“时间”字段主要是
使用java排序器,即: Collections.sort(myArrayList, new Comparator() { @Override public int c
有什么区别吗 SELECT * FROM my_table 和 SELECT my_column_id FROM my_table 地点: my_table 有百万行 网站上有大量并发用户进行sql查
有2个样本。 在第一个示例中,使用 orderby 可以更快地获得结果。 (根据 phpmyadmin 速度报告) 在另一个例子中,我没有使用 order by,它给出的结果较慢。 (根据 phpmy
我注意到,如果我将训练数据加载到内存中并将其作为 numpy 数组提供到图中,与使用相同大小的 shuffle 批次相比,速度会有很大差异,我的数据有大约 1000 个实例。 使用内存 1000 次迭
我在 python 中使用破折号。我正在绘制记录到 SQLite 数据库中的实时数据,目前,我正在绘制单个值与时间线图。我计划再添加 20 个图表,但目前,随着时间的增加, plotly 变慢,我认为
我试图调用 hasNext Velocity 模板中的方法,以便根据 foreach 循环中的位置影响行为 - 仅 hasNext没有按照文档工作。 这是 Velocity 用户指南的片段,关于 ha
在我正在制作的游戏中,我有两个点,pt1 和 pt2,我想计算出它们之间的角度。我已经在较早的计算中计算出距离。显而易见的方法是对垂直距离上的水平距离进行反正切 (tan(theta) = opp/a
我经常遇到字符串值不存在和/或为空的情况。这是测试这种情况的最佳方法吗? #if( $incentive.disclaimer && $!incentive.disclaimer != '' )
我想将一个模板nested包含在其他模板cont1,cont2和cont3中。 并且嵌套模板应仅对cont1隐藏一个特定控件。 在包含在cont1中之前,我想为一些标志变量$hideMyControl
是否可以更改从“Windows Azure Media Encoder”输出的音频的播放速度? 我正在使用配置为“WMA High Quality Audio”的“Windows Azure Medi
我使用速度将String(template)与字段合并 hi there I'am ${name}, And I'am ${age} old. velocity将字段${name}和${age}与一种
我使用的是 LockedBitmap 类,它简化了 C# 中位图数据的处理。目前它正在将数据复制到本地 byte[] 数组中,然后通过其类方法访问该数组以获取/设置像素颜色值。 这比直接通过指针访问锁
我尝试在 VM_global_library.vm 文件中添加一堆 #set($x=abc) 语句,但这些变量在我的 VM 模板中不可用。 我想为图像的基本路径等设置一个全局变量。这可能吗? 最佳答案
我的项目结构: -src --main ---java ----makers -----SomeClass ---resources ----htmlPattern.vm 如何告诉 SomeClass
我正在尝试从 Velocity 中的字符串中删除不需要的字符(换行符可以,但不能像 EM 和 CAN ASCII 控制字符那样)。 #set($cleanScreen = $cleanScreen.r
我想在日.月.年之间的点处分割日期。例如:2015 年 1 月 14 日至 {14, 01, 2015}这是我使用的代码:dates3.get(0) 包含我从页面的文本字段获取的字符串“14.01.2
之后,从 1.5 升级到速度引擎 1.7 出现了 1.5 没有的问题。为了解释这个问题,我必须展示一个代码片段: #foreach($someVariable in $someCollection)
我想知道从表中选择所有字段是否更快: SELECT * 或只选择您真正需要的: SELECT field1, field2, field3, field4, field5... 假设表有大约 10 个
我正在尝试模仿照片应用程序的行为,在该应用程序中,用户用手指平移照片并且照片具有一定的速度。由于我不会深入的原因,我不能将 UIScrollView 与它的缩放 UIImageView 一起使用,而是
我是一名优秀的程序员,十分优秀!