如何获得更流畅的2D射弹物理

编程入门 行业动态 更新时间:2024-10-23 17:32:38
本文介绍了如何获得更流畅的2D射弹物理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

所以我想开始研究2D平台游戏,并且直到明年我才能从事物理学工作,所以我在一本古老的物理学书中找到了方程,我不得不帮助找到x和y位置以及他们的速度.问题是,它们似乎加速太快,因为渲染太慢.这不是程序故障,这是我的错,因为我不知道如何减慢加速速度,从而使其看起来更平滑.这是我正在使用的Ball类的代码(我知道代码绝对是一团糟!我只是在四处弄乱以使方程正确无误....如果我正在工作,我永远不会像这样写我的代码在一个严肃的项目上,所以请不要提醒我它看起来很糟糕)

So I want to start working on a 2D platforming game, and I won't be in physics till next year, so I found the equations in an old physics book I had to help be find the x and y positions along with their velocities. The problem is, they seem to accelerate too fast in that the rendering is too slow. It's not the programs fault, its my fault in that I don't know how to slow down the acceleration so that it looks smoother. Here is the code for the ball class that i'm using (i know the code is an absolute mess! i was just messing around trying to get the equations right.... I would NEVER write my code like this if I was working on a serious project so please don't remind me that it looks bad)

import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; public class Ball extends JComponent{ public double xpos; double oX; double oY; double ypos; double xvel; double yvel; long time; double oxvel; double oyvel; long angle = 45; long startTime = System.currentTimeMillis(); public Ball(int p1, int p2, long t){ xpos = p1; oxvel = 50; oX = p1; ypos = p2; time = t; oyvel = -50; } public void update(){ xvel = (oxvel*Math.cos(45)); yvel = (oyvel*Math.sin(45)) + (9.8*time); if(!(xpos + xvel + 10 > getWidth())&&!(xpos + xvel < 0)) xpos +=xvel; if(!(ypos + yvel + 10 > getHeight())&&!(ypos + yvel <= 0)) ypos+=yvel; time++; } public void paint(Graphics g){ Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.RED); g2d.fillOval((int)xpos, (int)ypos, 15, 15); } }

推荐答案

您有硬编码的时间,在更新循环中,您只需将加速度乘以预先计算的时间步长(1个任意单位,那么使用单位会更好)秒),您每次都假设相同,这总是导致跳动.您应该将变量时间步传递给该方法并使用它.这意味着渲染进行的速度并不重要,您将获得平滑的运动.请记住,您可能会要求1/60秒钟的帧,但不一定总能做到这一点.

You have hard coded time, within your update loop you simply multiply your acceleration by a precomputed timestep (of 1 arbitrary units, it would be much better to use units of seconds), which you assume is the same every time, this always leads to jumpy motion. You should instead pass the variable timestep to the method and use that. This means it doesn't matter how fast the rendering is taking place you will get smooth motion. Remember you may ask for 1/60 of a second frames but you won't always get exactly that.

public void update(double time){ xvel = (oxvel*Math.cos(45)); yvel = (oyvel*Math.sin(45)) + (9.8*time); if(!(xpos + xvel + 10 > getWidth())&&!(xpos + xvel < 0)) xpos +=xvel*time; if(!(ypos + yvel + 10 > getHeight())&&!(ypos + yvel <= 0)) ypos+=yvel*time; }

在整个循环中,您将测量实际时间步长并将其传递.您可以使用System.currentTimeMillis();

Within the overall loop you would measure the actual timestep and pass that. You can do that measuring using System.currentTimeMillis();

long previousStep=System.currentTimeMillis(); public void gameLoop(){ long newStep=System.currentTimeMillis(); double frameTime=(newStep-previousStep)/1000.0; ball.update(frameTime); previousStep= newStep; }

time很可能不是long,而应该是double,以整数为增量测量的实时数据很难正确设置.

time should very likely not be a long but should be a double, real time measured in integer increments is very difficult to get right.

更多推荐

如何获得更流畅的2D射弹物理

本文发布于:2023-06-04 19:12:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503393.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何获得   流畅   物理

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!