2011-05-29 169 views
0

我写了一个程序,将模拟从50米大楼抛出的球。 我加入碰撞检测时,通过颠倒y方向的速度,当球击中地面时(y < 0),保持水平速度相同,并将两个速度乘以某个最小值,这样球就会最终到达休息。执行碰撞检测

#include<stdio.h> 
#include<math.h> 
#include <stdlib.h> 

int main() { 

    FILE *fp; 
    FILE *fr; 

    float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0; 
    float time = 0, deltaTime = .001; 

    float min = -.00000000001; 
    int numBounces = 0; 

    fr = fopen("input_data.txt", "rt"); 

    fp = fopen("output_data.txt", "w"); 

    if(fr == NULL){ printf("File not found");} 

    if(fp == NULL){ printf("File not found");} 

    fscanf(fr, "ax: %f ay: %f x: %f y: %f vx: %f vy: %f\n", &ax, &ay, &x, &y, &vx, &vy); 

    while (vx > min && vy > min) { 

      time = time + deltaTime; 
      vx = vx + ax*deltaTime; 
      vy = vy + ay*deltaTime; 
      x = x + vx*deltaTime + (.5*ax*deltaTime*deltaTime); 
      y = y + vy*deltaTime + (.5*ay*deltaTime*deltaTime); 

      fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time); 

    //Collision occurs; implement collision response 
      if(y < 0) { 
       vx = vx + ax*deltaTime*(.00001); 
       vy = -(vy + ay*deltaTime*(.00001)); 
       numBounces++; 

       fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time); 
     } 
} 

fclose(fp); 
fclose(fr); 

system ("PAUSE"); 
return 0; 

} 

我没有得到正确的值来产生正确的数据图。 这可能是因为我在while循环中的条件需要改变,或者我没有正确实现碰撞响应。

这里也是一些示例数据:

斧:0 AY:-9.8 X:0​​ Y:50 VX:8.66 VY:5

Picture of data plot

+0

多么错误是你的代码生成输出数据? – Ali1S232 2011-05-29 21:33:30

+0

我的数据情节由什么组成 – kachilous 2011-05-29 21:35:30

+0

你什么意思什么都没有?你可以发表一个例子吗? – flow 2011-05-30 20:32:11

回答

1

不outputing任何你可以试试fflush(fp)在每个周期结束时。并且就我看到的代码而言,只要碰到地面,您的对象就会获得更高的速度,您必须将vy = -(vy + ay*deltaTime*(.00001))更改为vy = -(vy - ay*deltaTime*(.00001))才能更正它。如果计算每次碰撞时的确切碰撞时间y < 0,然后移动物体,改变速度并在剩余的周期内移动物体以产生更逼真的碰撞,您还可以创建更好的碰撞实现。

我们知道移动deltaY = 1/2 * AY * T^2 + VY * T,所以我们可以使用folling公式计算T:

assuming py is the current height of object(it's distance to ground) 
=> -py = 0.5 * ay* t * t + vy * t 
=> 0 = 0.5 * ay * t * t+ vy * t + py 
=> t = (-vy +- sqrt(vy*vy - 2 * ay * py))/(2 * ay) 

并且由于吨必须为正并且知道AY是negetive和py是积极的,我们可以假设正确的答案是

=> tc = (sqrt(vy*vy - 2 * ay * py) - vy)/2/ay 

现在我们有tc这是碰撞时间。所以我们必须扭转位置和速度的最后变化,然后仅步进时间tc秒,然后颠倒vy并且步骤​​秒来完成该帧。所以里面的if条件会是这样(我只是可能有一些问题,做数学,所以万一你没有得到预期的结果仅仅指刚双检所有方程):

if (y < 0) { 
    float tc = (sqrt(vy*vy - 2 *ay * y))/2/ay; 
    x = x - vx*deltaTime - (.5*ax*deltaTime*deltaTime); 
    y = y - vy*deltaTime - (.5*ay*deltaTime*deltaTime); 
    vx = vx - ax * deltaTime; 
    vy = vy - ay * deltaTime; 
    vx = vx + ax * tc; 
    vy = vy + ay * tc; 
    x = x + vx*tc + (.5*ax*tc*tc); 
    y = y + vy*tc + (.5*ay*tc*tc); 
    vy = -(vy - ay*deltaTime*(.00001)); 
    // you can also change above line and simply write 
    // vy = vy * -0.99; 
    // that will also create friction as you want it to be there 
    vx = vx + ax * (deltaTime - tc); 
    vy = vy + ay * (deltaTime - tc); 
    x = x + vx* (deltaTime - tc) + (.5*ax* (deltaTime - tc)* (deltaTime - tc)); 
    y = y + vy* (deltaTime - tc) + (.5*ay* (deltaTime - tc)* (deltaTime - tc)); 
    numBounces++; 

    fprintf(fp, "%f\t%f\t%f\t%f\t%f\t%f\t%f\t\n", ax, ay, x, y, vx, vy, time); 
} 
+0

将vy改为vy = - (vy - ay * deltaTime *(。00001))似乎并不奏效。如果我想要实现更逼真的碰撞,我怎么能找到确切的碰撞时间? – kachilous 2011-05-29 21:42:47

+0

不写入输出是因为你的程序从未真正离开while,所以调用fclose neve并且输出不是真正写入文件(它只是创建一个缓存,写什么它的性能问题只是使用fflush来修复),并且改变是为了更正你的公式,我现在正在编辑我的答案,以添加所需的所有公式,以便更好地模拟。 – Ali1S232 2011-05-29 21:47:30

+0

不应该用公式来计算tc是deltaY =(1/2)ay * t^2 + vy * t代替deltaY = ay * t^2 + vy * t – kachilous 2011-05-29 22:27:28