【问题标题】:Firing projectiles in a circle绕圈发射弹丸
【发布时间】:2015-05-21 19:36:29
【问题描述】:

所以我似乎无法找到答案,但我正试图将子弹射入一个圆圈。我有一个简单的循环路径类,我将它附加到子弹上,当给定时间值时,它会从该类中读取位置。子弹只是增加这个时间值,不断更新它的位置到下一个。这可以改进,但直到我弄清逻辑,这就是我所拥有的。我知道这种方法有效,因为我用线性路径尝试过。问题是将其应用于圆形路径。

我希望子弹以给定的半径和速度围绕一个点(比如点“中心”)旋转。无论圆的半径如何,我都希望所有子弹都以相同的速度行进,因此较大的圆比较短的圆需要更长的时间才能完成。目前正在发生的事情是我有 CircularPath 对象给出说 x = r * cos(t) 和 y = r * sin (t) ,其中 t 以弧度为单位,但这会形成一个随着半径增加而速度增加的圆,并且这个圆的半径和中心是完全关闭的。子弹从正确的位置开始,除了半径和速度关闭。我希望我能充分描述这一点。我会将代码发布给任何人检查。

package io.shparki.tetris.go;

import io.shparki.tetris.util.Point2D;

import java.awt.Color;
import java.awt.Graphics2D;

public class CircularPath extends Path{


private  double radius;

// Where Start is the center and end is the location of mouse
// Radius will be distance between the two
public CircularPath(Point2D start, Point2D end) {
    super(start, end);
    radius = normalToEnd.getLength();

    color = Color.YELLOW;
}

public Point2D getPointAtTime(double time){
    double px = start.getX() + radius * Math.cos(Math.toRadians(time));
    double py = start.getY() - radius * Math.sin(Math.toRadians(time));
    return new Point2D(px, py);
}
public double getFinalTime() { return 0; }
public CircularPath getClone() { return new CircularPath(start.getClone(), end.getClone()); }

public void update(){
    super.update();

    radius = normalToEnd.getLength();
}
public void render(Graphics2D g2d){
    super.render(g2d);

    g2d.drawLine((int)start.getX(), (int)start.getY(), (int)end.getX(), (int)end.getY());
    //g2d.drawOval((int)(start.getX() - radius), (int)(start.getY() - radius), (int)radius * 2, (int)radius * 2);
}

}

【问题讨论】:

    标签: java geometry projectile


    【解决方案1】:
    x = r * cos(t/r)
    y = r * sin(t/r)
    

    【讨论】:

    • 这让他们都只是向上飞;我仍然希望弹丸以一致的速度和正确的中心和半径绕圈移动;
    • @Matt:这是正确的答案。当 x 的值明显改变时,为什么它们会向上飞?你确定你正确实施了吗?
    • 我正在研究我的其余代码;我认为我的射弹发射方式与应有的不同,但谢谢!
    【解决方案2】:

    另一种解决方案是对 2d 动量进行建模,然后将“引力”施加到您希望移动物体绕其轨道运行的中心点(或更一般地说是椭圆形焦点)。

    (经典的 Space Wars 游戏是在一台机器上实现的,速度太慢,无法实时处理三角计算,因此他们为重力场的 x 和 y 分量分别预先计算了一个 2d 数组;然后他们可以只进行表查找基于船的最后位置,并用它来更新它的动量,然后用它来更新它的位置。更慢的机器迫使更聪明的解决方案。)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多