箭的射弹运动:基于用户目标的改变轨迹 [英] Projectile Motion of Arrow: Altered trajectory based on User's Aim

查看:65
本文介绍了箭的射弹运动:基于用户目标的改变轨迹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个需要从静止位置(设定坐标)拍摄箭头的游戏。箭头的轨迹基于用户在GUI中单击的位置。这基本上是一个Aiming功能。我不能让箭头沿着工作路径行进,任何使用的方程式都会导致奇怪的,有毛病的,有缺陷的结果。



  public   class  ReShoot  implements  ActionListener 
{
public void actionPerformed(ActionEvent e){
ArrowShoot shoot = < span class =code-keyword> new ArrowShoot();
shoot.ReShoot();
}
}
public class ArrowShoot implements ActionListener
{
public Timer T = new 计时器( 5 );
箭头A = 箭头();


public void ReShoot(){
T.start();
arrow_x = 0;
arrow_y = 200;
A.setBounds( 0 200 10 10 );
}
// MAIN:y = -16t ^ 2 + Vy * t + h
// Vy = v * sin(a)
< span class =code-comment> // Vx = v * cos(a)
// a = arctan((200-mouse_y)/ v
// v = Mouse_x - Arrow_x
// t = x / Vx


public void actionPerformed(ActionEvent e)
{ // arrow_y = 0.0025 * Math.pow((mouse_x-arrow_x ),2)+ mouse_y;
容器container_arrow = getContentPane();
container_arrow.setLayout(null);
con tainer_arrow.add(A);
A.setBounds( 0 200 10 10 );
arrow_x ++;

double v = mouse_x / 2; // 身高变化
double a = 50 * Math.atan(( 200 -mouse_y)/(v));
double Vy = v * Math.sin(a);
double Vx = v * Math.cos(a);
double t = arrow_x / Vx;
double h = 200 ;

arrow_y =( 16 )* Math.pow(t, 2 ) +(Vy * t)+ h;

int x =( int )Math.round(arrow_x);
int y =( int )Math.round(arrow_y);
A.setBounds(x,y, 10 10 );
if (arrow_y> = 500)
T.stop();

}



我很确定我这样做是错的,并且必须有一种更有效的方法来完成这项任务。

解决方案

您甚至没有尝试计算运动的轨迹/时间。代码中可能存在怪异和小故障动作,但实际上并不存在。



在第一次近似中,弹丸的运动用简单的等式描述:恒定加速度的运动(地球引力,在这种情况下):

x = x 0 + v x * t;

y = y 0 + v y * t + g * t 2 ;

您正确计算组件从角度来看速度。



不是很明显吗?!在接下来的近似中,您可以尝试考虑空气阻力。在分析计算中,它可能看起来很难,但你可以简单地用数字模拟运动,点对点。



-SA

I am making a game which needs an "arrow" to be shot from a stationary location (a set coordinate). The arrow's trajectory is based on the location that the user Clicks in the GUI. This is essentially an Aiming feature. I cant get the arrow to follow a working path, any equations ie used have led to weird, glitchy, and buggy results.

public class ReShoot implements ActionListener
{
    public void actionPerformed(ActionEvent e){
        ArrowShoot shoot = new ArrowShoot();
        shoot.ReShoot();
    }
}
public class ArrowShoot implements ActionListener
{
    public Timer T = new Timer(5, this);
    Arrow A = new Arrow();


    public void ReShoot(){
        T.start();
        arrow_x=0;
        arrow_y=200;
        A.setBounds(0,200,10,10);
    }
    // MAIN: y=-16t^2 + Vy * t + h
    //Vy = v * sin(a)
    //Vx = v * cos(a)
    //a = arctan( (200-mouse_y)/v
    //v = Mouse_x - Arrow_x
    //t = x / Vx


    public void actionPerformed(ActionEvent e)
    {//arrow_y = 0.0025 * Math.pow((mouse_x-arrow_x), 2)+ mouse_y;
        Container container_arrow = getContentPane();
        container_arrow.setLayout(null);
        container_arrow.add(A);
        A.setBounds(0,200,10,10);
            arrow_x++;

            double v = mouse_x/2; //height change
            double a = 50* Math.atan((200-mouse_y) / (v)); 
            double Vy = v * Math.sin(a);
            double Vx = v * Math.cos(a);
            double t = arrow_x/Vx;
            double h = 200;

            arrow_y = (16) * Math.pow(t, 2) + (Vy * t) + h;

            int x = (int)Math.round(arrow_x);
            int y = (int)Math.round(arrow_y);
            A.setBounds(x, y,10,10);
        if (arrow_y>=500)
            T.stop();

    }


I am pretty sure im doing this all wrong, and there has to be a more effective method to accomplish this task.

解决方案

You are not even trying to calculate the trajectory/timing of the motion. "Weird" and "glitchy" motion may exist in your code, but not in reality.

In first approximation, the motion of the projectile is described by simple equation: motion with constant acceleration (Earth gravity, in this case):
x = x0 + vx*t;
y = y0 + vy*t + g*t2;
You correctly calculate components of the speed from the angle.

Isn't it obvious?! And in next approximation, you can try to take into account the air resistance. In analytic calculation, it may look difficult, but you can simply model the motion numerically, point to point.

—SA


这篇关于箭的射弹运动:基于用户目标的改变轨迹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆