我写了一个程序,将模拟从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
多么错误是你的代码生成输出数据? – Ali1S232 2011-05-29 21:33:30
我的数据情节由什么组成 – kachilous 2011-05-29 21:35:30
你什么意思什么都没有?你可以发表一个例子吗? – flow 2011-05-30 20:32:11