如何解决在仿真中实现碰撞响应
| 我正在尝试在创建的仿真中实现碰撞响应。 基本上,该程序模拟以某个初始速度将一个球从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,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,time);
}
}
fclose(fp); //Close output file
fclose(fr); //Close input file
//system (\"PAUSE\");
return 0;
}
基本上,我试图产生准确的值,以便可以看到该模拟的外观图。我假设逻辑错误与物理学有关。但是由于我的物理知识有限,所以我看不到到底是什么错误。
这是示例输入:
斧头:0 ay:-9.8 x:0 y:50 vx:8.66 vy:5
解决方法
在我看来,您的问题可能出在您如何实现运动学方程式上。
//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);
这里有两件事:您已经在vx
和vy
的方程中考虑了加速度,并且您正在使用求和而不是积分方程。 included4ѭ和.5*ay*deltaTime*deltaTime
不应该包括在内。当基于速度方程的积分来计算由于恒定加速度而在整个时间量内行驶的距离时,使用方程x = 0.5 * a * t ^ 2。在进行求和时,已经在速度方程式中包含了加速度,因此无需在位置方程式中包含加速度。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。