【问题标题】:Implementing Collision Response in Simulation在仿真中实现碰撞响应
【发布时间】:2011-09-05 18:58:51
【问题描述】:

我正在尝试在我正在创建的模拟中实现碰撞响应。 基本上,该程序模拟了一个球以一定的初始速度从 50 米的建筑物中抛出。

我不相信该程序会输出实际的碰撞时间值以及 x、y 和 vx、vy 的值。

这是程序:

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

 int main() {

     FILE *fp; 
     FILE *fr;

     //Declare and initialize all variables to be used
     float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0; 
     float time = 0, deltaTime = .001; 
     float vyImpact = 0, vxImpact = 0, xImpact = 0; 

     float old_y = 0,  old_x = 0, old_vy = 0, old_vx = 0;
     float deltaTime2 = 0,  deltaTime3 = 0;

     int numBounces = 0;

     //Coefficient of Restitution; epsilon = ex = ey
     float ex = .5;
     float ey = .5;

     fr = fopen("input_data.txt", "rt"); //Open file for reading

     fp = fopen( "output_data.txt", "w" ); // Open file for writing

     if(fr == NULL){ printf("File not found");} //if text file is not in directory...

     if(fp == NULL){ printf("File not found");} //if text file is not in directory...

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

     while (numBounces < 9) {

          //time = time + deltaTime
          time = time + deltaTime;

          //velocity[new] = velocity[old] + acc * deltaTime
          vx = vx + ax*deltaTime;
          vy = vy + ay*deltaTime;

          //position[new] = position[old] + velocity*deltaTime + .5*acc*(deltaTime)^2
          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) {

                   //"Undo" values for y, x, and velocity
                   old_y = y - vy*deltaTime - (.5*ay*deltaTime*deltaTime); 
                   old_x = x - vx*deltaTime - (.5*ax*deltaTime*deltaTime); 
                   old_vy = vy - ay*deltaTime;
                   old_vx = vx - ax*deltaTime; 

                   //Calculate time of collision
                   deltaTime2 = (-old_y + sqrt((old_y*old_y) - 2*ay*old_y)) / (ay);
                   printf("Time of Collision = %f\n", time - deltaTime2); 

                   //Calculate velocity and x position at collsion
                   vyImpact = old_vy + ay*deltaTime2;
                   vxImpact = old_vx + ax*deltaTime2;
                   xImpact = old_x + old_vx*deltaTime2 + .5*ax*(deltaTime2*deltaTime2);

                   //Calculate new time for when ball bounces
                   deltaTime3 = deltaTime - deltaTime2;

                   //Calculate new x and y position and velocity for when ball bounces
                   x = xImpact + (ex)*vxImpact*deltaTime3 + .5*ax*(deltaTime3*deltaTime3);
                   y = 0 + (-ey)*vyImpact*deltaTime3 + .5*ay*(deltaTime3*deltaTime3);
                   vy = (-ey)*vyImpact + ay*deltaTime3;
                   vx = (ex)*vxImpact + ax*deltaTime3; 

                   numBounces++; 
                   printf("Number of Bounce(s) = %d\n", 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); //Close output file
     fclose(fr); //Close input file

     //system ("PAUSE"); 
     return 0;
  }

基本上,我正在尝试生成准确的值,以便我可以看到这个模拟应该是什么样子的图。我假设逻辑错误与物理学有关。但是由于我的物理知识有限,我无法看出到底是哪里出了问题。

这是示例输入: ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5

【问题讨论】:

  • 第一:不要使用float!在没有强烈的理由不这样做的情况下,您程序中的所有浮点变量都应该是double(或double _Complex,如果需要)。
  • “我不相信程序在输出现实值”:那么它输出的是什么?你喂它吃什么?你期待什么?
  • 这里是一些示例数据: ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5 但我真的只是想查看模拟图,我可以甚至不明白。所以我真的只想要“可草图”的数据。

标签: c physics simulation


【解决方案1】:

在我看来,您的问题可能在于您如何实现运动学方程。

//velocity[new] = velocity[old] + acc * deltaTime
vx = vx + ax*deltaTime;
vy = vy + ay*deltaTime;

//position[new] = position[old] + velocity*deltaTime + .5*acc*(deltaTime)^2
x = x + vx*deltaTime + (.5*ax*deltaTime*deltaTime);
y = y + vy*deltaTime + (.5*ay*deltaTime*deltaTime);

这里有两件事:您已经在 vxvy 的方程中考虑了加速度,并且您使用的是求和而不是积分方程。不应包含 .5*ax*deltaTime*deltaTime.5*ay*deltaTime*deltaTime。当基于速度方程的积分计算在时间内由于恒定加速度而行进的距离时,使用方程 x= 0.5*a*t^2。由于您正在进行求和并且已经在速度方程中包含加速度,因此无需在位置方程中包含加速度。

【讨论】:

  • 好的,所以我应该删除 x,y,old_x 和 old_y 的方程?如果我删除这些方程式,我将如何获得行进的距离?
  • 您不应该完全删除它们,只需从xy 的方程式中删除+ (.5*a_*deltaTime*deltaTime) 位,看看结果是否更好。
  • 我仍然没有得到一个情节。 0.056073 会是一个现实的初始碰撞时间吗?
  • +1 翻译集成确实应该是 p = p + dt.v 我要补充一点,弹簧/阻尼器的接触比您尝试的要容易得多。
  • 使用胡克定律将触点建模为弹性弹簧。添加一个阻尼项,即 term prop。如果你想在接触过程中提取能量,可以加速
猜你喜欢
  • 1970-01-01
  • 2011-09-05
  • 2011-10-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多