计算球的反弹

时间:2014-03-17 06:13:24

标签: c# math trigonometry

我基本上只是想让球从100x反弹到300x ...我寻找抛物线方程然后在trig函数中我似乎无法让它正常工作...它已经有一段时间了我已经完成了数学运算,所以我可能甚至没有接近这个...这是我的整个弹跳的代码,如果有人能指出我的正确方程式那将是美妙的。

    private void btStart_Click(object sender, EventArgs e)
    {
        lbBall.Location = new Point(0, 0);
        bounceBall(lbBall, 100);
    }

    public void bounceBall(Label ball, int xFinal)
    {
        //this just initiales variables and timers
        this.animateTimer = new Timer();
        this.animateTimer.Interval = 75;
        this.animateTimer.Tick += animateTimer_Tick;

        this.ball = ball;
        this.xInit = ball.Location.X;
        this.yInit = ball.Location.Y;
        this.xCurrent = ball.Location.X;
        this.yCurrent = ball.Location.Y;
        this.xFinal = ball.Location.X + xFinal;
        this.maxHeight = 100;

        this.animateTimer.Start();
    }
    private void animateTimer_Tick(object sender, EventArgs e)
    {
        //this just makes sure the timer stops and doesn't go on forever
        if (xCurrent >= xFinal)
        {
            ball.Location = new Point((int)xFinal, (int)yInit);
            animateTimer.Stop();
            return;
        }

        //adds 1 to x on every iteration
        xCurrent = xCurrent + 1;

        //calculates yCurrent based on xCurrent
        double result = 0;
        float dx = (xCurrent - xInit) / (xFinal - xInit);
        if (dx < 0.5)
        {
            double dxA = Math.Asin(dx);
            result = (Math.Sin(dxA)) * maxHeight;
        }
        else
        {
            dx = (float)0.5 - dx;
            double dxA = Math.Acos(dx);
            result = (Math.Cos(dxA)) * maxHeight;
        }
        yCurrent = (float)result;


        ball.Location = new Point((int)xCurrent, (int)yCurrent);
        lbLocation.Text = "xPos: " + xCurrent + "        yPos: " + yCurrent;
    }

这个想法是用正弦反弹,然后从中途点换到cos ...这是我能够达到我想要的最接近但不幸的是它仍然无效

==编辑== 澄清我对短语问题的不稳定性...我不需要进行物理计算,因为球会从x上的一个点到x上的另一个点,并且有一个小跳到达某个最大高度...对不起有误解

2 个答案:

答案 0 :(得分:2)

不完全确定为什么你在这里使用三角法,适用的公式是恒定加速度系统的suvat公式。

基本上,水平轴没有加速度,所以你的位置(位移)是时间的恒定倍数。

然而,在垂直轴上,重力发挥作用,因此您需要根据(a)计算出位移:

         1   2
s = ut + - at
         2

其中:

  • s是计算的排量;
  • u是初始速度;
  • t是经过的时间;和
  • a是加速度(g = 9.8 m / s / s)。

所以,让我们假设你在20米的高度以10米/秒的速度水平投球。时间t处的水平位置(假设您的投掷方向为正):

 t   hpos
---  ----
0.0     0
0.1     1
0.2     2
0.3     3

_vertical位置稍微复杂一些。由于您将其水平投掷,因此初始垂直速度为零,因此我们可以对ut项进行折扣。但是另一个术语取决于加速度,因为我们考虑的是积极因素,可以理解为-9.8

因此,20米高线的垂直位移将是:

 t     vpos
---  --------
0.0     0.000
0.1    -0.049
0.2    -0.196
0.3    -0.441
:
2.0   -19.600
2.1   -21.609

现在请注意,在时间t = 2.0t = 2.1之间的某个时间点,球已经低于地面,因此您需要实施反弹功能能量损失的系数(球在弹跳时会失去能量,所以它不仅仅是扭转速度的问题,而是像v <- -.9 * v那样能量损失10%)


(a)你必须处理弹跳的事实意味着你需要在每个时间点知道当前位移的速度是多少你可以处理它。上面给出的s公式假设没有任何东西会妨碍你的对象。

因此,你需要存储物体的位置和当前速度,并使用s = s + (v * td) + (0.5 * a * td * td)之类的东西来更新位置,必要时处理反弹(通过反转速度和失去能量),然后{ {1}}更新速度(其中v = v + (a * td)是与先前状态的时差)。


举例来说,这是一个相当简单的C程序,它使用这些公式来反弹球直到它停止为止(这里没有水平运动,因为它是一个简单的加法方程式,所以没有必要):

td

根据给定的方程式显示球的高度,初始高度为20米,时间差为十分之一秒。

查看输出(特别是高度)以查看其实际效果:

#include <stdio.h>

static void outData (double h, double v, int t) {
    printf ("Time t = %4d.%d, veloc = %5.2f, height = %5.2f\n",
        t / 10, t % 10, v, h);
}

int main(void) {
    double td = 0.1, h = 20, a = -9.8 * td, v = 0, lasth = h;
    int tenthT = 0;

    outData (h, v, 0);
    while ((h != 0) || (lasth != 0)) {
        lasth = h;
        if (h == 0)
            v = -0.9 * v;
        v = v + a;
        h = h + (v * td) + (a * td * td / 2);
        if (h < 0)
            h = 0;
        outData (h, v, tenthT++);
    }

    return 0;
}

事实上,&#34;图形&#34;从微小的修改中得到的观点可能会更清楚。因此改变程序的初始部分:

Time t =    0.0, veloc =   0.00, height =  20.00
Time t =    0.0, veloc =  -0.98, height =  19.90
Time t =    0.1, veloc =  -1.96, height =  19.70
Time t =    0.2, veloc =  -2.94, height =  19.40
Time t =    0.3, veloc =  -3.92, height =  19.00
Time t =    0.4, veloc =  -4.90, height =  18.51
Time t =    0.5, veloc =  -5.88, height =  17.91
Time t =    0.6, veloc =  -6.86, height =  17.22
Time t =    0.7, veloc =  -7.84, height =  16.43
Time t =    0.8, veloc =  -8.82, height =  15.55
Time t =    0.9, veloc =  -9.80, height =  14.56
Time t =    1.0, veloc = -10.78, height =  13.48
Time t =    1.1, veloc = -11.76, height =  12.30
Time t =    1.2, veloc = -12.74, height =  11.02
Time t =    1.3, veloc = -13.72, height =   9.64
Time t =    1.4, veloc = -14.70, height =   8.17
Time t =    1.5, veloc = -15.68, height =   6.59
Time t =    1.6, veloc = -16.66, height =   4.92
Time t =    1.7, veloc = -17.64, height =   3.15
Time t =    1.8, veloc = -18.62, height =   1.29
Time t =    1.9, veloc = -19.60, height =   0.00
Time t =    2.0, veloc =  16.66, height =   1.66
Time t =    2.1, veloc =  15.68, height =   3.22
Time t =    2.2, veloc =  14.70, height =   4.69
Time t =    2.3, veloc =  13.72, height =   6.06
Time t =    2.4, veloc =  12.74, height =   7.33
Time t =    2.5, veloc =  11.76, height =   8.50
Time t =    2.6, veloc =  10.78, height =   9.57
Time t =    2.7, veloc =   9.80, height =  10.54
Time t =    2.8, veloc =   8.82, height =  11.42
Time t =    2.9, veloc =   7.84, height =  12.20
Time t =    3.0, veloc =   6.86, height =  12.88
Time t =    3.1, veloc =   5.88, height =  13.47
Time t =    3.2, veloc =   4.90, height =  13.95
Time t =    3.3, veloc =   3.92, height =  14.34
Time t =    3.4, veloc =   2.94, height =  14.63
Time t =    3.5, veloc =   1.96, height =  14.82
Time t =    3.6, veloc =   0.98, height =  14.91
Time t =    3.7, veloc =  -0.00, height =  14.91
Time t =    3.8, veloc =  -0.98, height =  14.80
Time t =    3.9, veloc =  -1.96, height =  14.60
Time t =    4.0, veloc =  -2.94, height =  14.30
Time t =    4.1, veloc =  -3.92, height =  13.91
Time t =    4.2, veloc =  -4.90, height =  13.41
Time t =    4.3, veloc =  -5.88, height =  12.82
Time t =    4.4, veloc =  -6.86, height =  12.13
Time t =    4.5, veloc =  -7.84, height =  11.34
Time t =    4.6, veloc =  -8.82, height =  10.45
Time t =    4.7, veloc =  -9.80, height =   9.47
Time t =    4.8, veloc = -10.78, height =   8.38
Time t =    4.9, veloc = -11.76, height =   7.20
Time t =    5.0, veloc = -12.74, height =   5.92
Time t =    5.1, veloc = -13.72, height =   4.55
Time t =    5.2, veloc = -14.70, height =   3.07
Time t =    5.3, veloc = -15.68, height =   1.50
Time t =    5.4, veloc = -16.66, height =   0.00
Time t =    5.5, veloc =  14.01, height =   1.40
Time t =    5.6, veloc =  13.03, height =   2.70
Time t =    5.7, veloc =  12.05, height =   3.90
Time t =    5.8, veloc =  11.07, height =   5.00
Time t =    5.9, veloc =  10.09, height =   6.00
Time t =    6.0, veloc =   9.11, height =   6.91
Time t =    6.1, veloc =   8.13, height =   7.72
Time t =    6.2, veloc =   7.15, height =   8.43
Time t =    6.3, veloc =   6.17, height =   9.04
Time t =    6.4, veloc =   5.19, height =   9.56
Time t =    6.5, veloc =   4.21, height =   9.97
Time t =    6.6, veloc =   3.23, height =  10.29
Time t =    6.7, veloc =   2.25, height =  10.51
Time t =    6.8, veloc =   1.27, height =  10.63
Time t =    6.9, veloc =   0.29, height =  10.66
Time t =    7.0, veloc =  -0.69, height =  10.58
Time t =    7.1, veloc =  -1.67, height =  10.41
Time t =    7.2, veloc =  -2.65, height =  10.14
Time t =    7.3, veloc =  -3.63, height =   9.78
Time t =    7.4, veloc =  -4.61, height =   9.31
Time t =    7.5, veloc =  -5.59, height =   8.75
Time t =    7.6, veloc =  -6.57, height =   8.09
Time t =    7.7, veloc =  -7.55, height =   7.33
Time t =    7.8, veloc =  -8.53, height =   6.47
Time t =    7.9, veloc =  -9.51, height =   5.51
Time t =    8.0, veloc = -10.49, height =   4.46
Time t =    8.1, veloc = -11.47, height =   3.31
Time t =    8.2, veloc = -12.45, height =   2.06
Time t =    8.3, veloc = -13.43, height =   0.71
Time t =    8.4, veloc = -14.41, height =   0.00
Time t =    8.5, veloc =  11.99, height =   1.19
Time t =    8.6, veloc =  11.01, height =   2.29
Time t =    8.7, veloc =  10.03, height =   3.29
Time t =    8.8, veloc =   9.05, height =   4.19
Time t =    8.9, veloc =   8.07, height =   4.99
Time t =    9.0, veloc =   7.09, height =   5.69
Time t =    9.1, veloc =   6.11, height =   6.30
Time t =    9.2, veloc =   5.13, height =   6.81
Time t =    9.3, veloc =   4.15, height =   7.21
Time t =    9.4, veloc =   3.17, height =   7.53
Time t =    9.5, veloc =   2.19, height =   7.74
Time t =    9.6, veloc =   1.21, height =   7.86
Time t =    9.7, veloc =   0.23, height =   7.87
Time t =    9.8, veloc =  -0.75, height =   7.79
Time t =    9.9, veloc =  -1.73, height =   7.61
Time t =   10.0, veloc =  -2.71, height =   7.34
Time t =   10.1, veloc =  -3.69, height =   6.96
Time t =   10.2, veloc =  -4.67, height =   6.49
Time t =   10.3, veloc =  -5.65, height =   5.92
Time t =   10.4, veloc =  -6.63, height =   5.25
Time t =   10.5, veloc =  -7.61, height =   4.49
Time t =   10.6, veloc =  -8.59, height =   3.62
Time t =   10.7, veloc =  -9.57, height =   2.66
Time t =   10.8, veloc = -10.55, height =   1.60
Time t =   10.9, veloc = -11.53, height =   0.44
Time t =   11.0, veloc = -12.51, height =   0.00
Time t =   11.1, veloc =  10.28, height =   1.02
Time t =   11.2, veloc =   9.30, height =   1.95
Time t =   11.3, veloc =   8.32, height =   2.78
Time t =   11.4, veloc =   7.34, height =   3.51
Time t =   11.5, veloc =   6.36, height =   4.14
Time t =   11.6, veloc =   5.38, height =   4.67
Time t =   11.7, veloc =   4.40, height =   5.11
Time t =   11.8, veloc =   3.42, height =   5.44
Time t =   11.9, veloc =   2.44, height =   5.68
Time t =   12.0, veloc =   1.46, height =   5.82
Time t =   12.1, veloc =   0.48, height =   5.87
Time t =   12.2, veloc =  -0.50, height =   5.81
Time t =   12.3, veloc =  -1.48, height =   5.66
Time t =   12.4, veloc =  -2.46, height =   5.41
Time t =   12.5, veloc =  -3.44, height =   5.06
Time t =   12.6, veloc =  -4.42, height =   4.61
Time t =   12.7, veloc =  -5.40, height =   4.07
Time t =   12.8, veloc =  -6.38, height =   3.43
Time t =   12.9, veloc =  -7.36, height =   2.69
Time t =   13.0, veloc =  -8.34, height =   1.85
Time t =   13.1, veloc =  -9.32, height =   0.91
Time t =   13.2, veloc = -10.30, height =   0.00
Time t =   13.3, veloc =   8.29, height =   0.82
Time t =   13.4, veloc =   7.31, height =   1.55
Time t =   13.5, veloc =   6.33, height =   2.18
Time t =   13.6, veloc =   5.35, height =   2.71
Time t =   13.7, veloc =   4.37, height =   3.14
Time t =   13.8, veloc =   3.39, height =   3.47
Time t =   13.9, veloc =   2.41, height =   3.71
Time t =   14.0, veloc =   1.43, height =   3.85
Time t =   14.1, veloc =   0.45, height =   3.89
Time t =   14.2, veloc =  -0.53, height =   3.83
Time t =   14.3, veloc =  -1.51, height =   3.67
Time t =   14.4, veloc =  -2.49, height =   3.42
Time t =   14.5, veloc =  -3.47, height =   3.07
Time t =   14.6, veloc =  -4.45, height =   2.62
Time t =   14.7, veloc =  -5.43, height =   2.07
Time t =   14.8, veloc =  -6.41, height =   1.42
Time t =   14.9, veloc =  -7.39, height =   0.68
Time t =   15.0, veloc =  -8.37, height =   0.00
Time t =   15.1, veloc =   6.56, height =   0.65
Time t =   15.2, veloc =   5.58, height =   1.20
Time t =   15.3, veloc =   4.60, height =   1.66
Time t =   15.4, veloc =   3.62, height =   2.01
Time t =   15.5, veloc =   2.64, height =   2.27
Time t =   15.6, veloc =   1.66, height =   2.43
Time t =   15.7, veloc =   0.68, height =   2.50
Time t =   15.8, veloc =  -0.30, height =   2.46
Time t =   15.9, veloc =  -1.28, height =   2.33
Time t =   16.0, veloc =  -2.26, height =   2.10
Time t =   16.1, veloc =  -3.24, height =   1.77
Time t =   16.2, veloc =  -4.22, height =   1.34
Time t =   16.3, veloc =  -5.20, height =   0.81
Time t =   16.4, veloc =  -6.18, height =   0.19
Time t =   16.5, veloc =  -7.16, height =   0.00
Time t =   16.6, veloc =   5.47, height =   0.54
Time t =   16.7, veloc =   4.49, height =   0.99
Time t =   16.8, veloc =   3.51, height =   1.33
Time t =   16.9, veloc =   2.53, height =   1.58
Time t =   17.0, veloc =   1.55, height =   1.73
Time t =   17.1, veloc =   0.57, height =   1.78
Time t =   17.2, veloc =  -0.41, height =   1.74
Time t =   17.3, veloc =  -1.39, height =   1.59
Time t =   17.4, veloc =  -2.37, height =   1.35
Time t =   17.5, veloc =  -3.35, height =   1.01
Time t =   17.6, veloc =  -4.33, height =   0.57
Time t =   17.7, veloc =  -5.31, height =   0.03
Time t =   17.8, veloc =  -6.29, height =   0.00
Time t =   17.9, veloc =   4.68, height =   0.46
Time t =   18.0, veloc =   3.70, height =   0.83
Time t =   18.1, veloc =   2.72, height =   1.10
Time t =   18.2, veloc =   1.74, height =   1.27
Time t =   18.3, veloc =   0.76, height =   1.34
Time t =   18.4, veloc =  -0.22, height =   1.31
Time t =   18.5, veloc =  -1.20, height =   1.19
Time t =   18.6, veloc =  -2.18, height =   0.96
Time t =   18.7, veloc =  -3.16, height =   0.64
Time t =   18.8, veloc =  -4.14, height =   0.22
Time t =   18.9, veloc =  -5.12, height =   0.00
Time t =   19.0, veloc =   3.63, height =   0.36
Time t =   19.1, veloc =   2.65, height =   0.62
Time t =   19.2, veloc =   1.67, height =   0.78
Time t =   19.3, veloc =   0.69, height =   0.84
Time t =   19.4, veloc =  -0.29, height =   0.81
Time t =   19.5, veloc =  -1.27, height =   0.68
Time t =   19.6, veloc =  -2.25, height =   0.45
Time t =   19.7, veloc =  -3.23, height =   0.12
Time t =   19.8, veloc =  -4.21, height =   0.00
Time t =   19.9, veloc =   2.81, height =   0.28
Time t =   20.0, veloc =   1.83, height =   0.45
Time t =   20.1, veloc =   0.85, height =   0.54
Time t =   20.2, veloc =  -0.13, height =   0.52
Time t =   20.3, veloc =  -1.11, height =   0.40
Time t =   20.4, veloc =  -2.09, height =   0.19
Time t =   20.5, veloc =  -3.07, height =   0.00
Time t =   20.6, veloc =   1.78, height =   0.17
Time t =   20.7, veloc =   0.80, height =   0.25
Time t =   20.8, veloc =  -0.18, height =   0.23
Time t =   20.9, veloc =  -1.16, height =   0.10
Time t =   21.0, veloc =  -2.14, height =   0.00
Time t =   21.1, veloc =   0.95, height =   0.09
Time t =   21.2, veloc =  -0.03, height =   0.08
Time t =   21.3, veloc =  -1.01, height =   0.00
Time t =   21.4, veloc =  -0.07, height =   0.00

该节目显示:

#include <stdio.h>
#include <unistd.h>

static void outData (double h, double v, int t) {
    sleep (1);
    printf ("Time t = %4d.%d: ", t / 10, t % 10);
    int ih = h;
    while (ih-- > 0) putchar (' ');
    puts ("*");
}

答案 1 :(得分:0)

正如paxdiablo和其他人所指出的那样,物理学确定了弹跳球所采用的路径,即抛物线。但是知道这可以简化工作。

通过x = 0,y = 0的抛物线的公式是y = ax ^ 2,其中a> 0的值打开(a <1)或关闭(a> 1)抛物线。

如果地板处于y = b,则当x = sqrt(b / a)时发生反弹

物理学告诉我们x方向没有加速度,当时间t = 0时x = 0,在时间t确定x的最简单方法是x = t

因此,对于第一次反弹t从0到sqrt(b / a)

对于第二次反弹的路径,抛物线需要在x方向上平移2 * sqrt(b / a)

所以让d = sqrt(b / a)抛物线方程为第二次反弹的路径是

Y = A(X-2D)^ 2

所以从t = 0开始,x = 0,y = 0,设定p = 0(p是球所用路径编号的计数器),然后

Y = A(X-2PD)^ 2

每当t = d + 2pd = d(1 + 2p)p增加p时,t增加; (显然需要考虑浮点的不准确性和t的增量,所以t> d + 2pd)