作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
所以我正在制作一个 2D android 游戏,你用光标瞄准,你的角色在光标点击的地方射击。创建箭头时调用此方法
private final float GRAVITY = 100, SPEED = 50f;
public Arrow(float dx, float dy, float x1, float y1, float x2, float y2,)
{
destination = new Vector2(dx, dy);//mouse clicked here
//x1 and y1 are the starting coordinates
bounds = new Polyline(new float[]{x1, y1, x2, y2});
double r = Math.atan2(dy-y1, dx-x1);//calculate angle
velocity = new Vector2();
velocity.x = (float)(Math.cos(r) * SPEED);
velocity.y = (float)(Math.sin(r) * SPEED) + ACCOUNT FOR GRAVITY;
acceleration= new Vector2(0, GRAVITY);
}
这就是更新方法,非常简单
public void update(float delta)
{
velocity.add(acceleration.cpy().scl(delta));
position.add(velocity.cpy().scl(delta));
}
如何考虑重力?如果重力设置为 0,箭头会沿直线移动到鼠标单击的坐标,但在重力作用下,箭头总是会达不到要求。我不确定如何解释重力。我认为达美航空可能把我搞砸了。
最佳答案
这更像是一个数学/物理问题,而不是一个编程问题。首先,您知道箭头的水平速度是恒定的(除非有空气阻力,在这种情况下情况要复杂得多)。您可以计算箭头到达目的地的 x 坐标所需的时间。
let (dx, dy) = displacement from launcher to destination
let c = cos(angle), s = sin(angle), vx = c * speed, vy = s * speed
vx * t = dx
t = dx / vx
利用该值,您可以计算垂直位移
dy = 0.5*acc * t^2 + V0 * t
dy = 0.5*acc * (dx/vx)^2 + vy*t
dy = 0.5*acc * (dx/(c*speed))^2 + (s*speed)*(dx/(c*speed))
因为 sin = sqrt(1 - cosine^2),
dy = 0.5*acc * (dx/(c*speed))^2 + (sqrt(1-c^2)*speed)*(dx/c*speed))
现在你有一个只有已知值(acc、dy、dx、速度)和 c 的方程。如果你解出 c,你就知道余弦,并且可以求出正弦值。
关于java - 在坐标处射箭时考虑重力,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30157662/
我是一名优秀的程序员,十分优秀!