gpt4 book ai didi

c - 在仿真中实现碰撞响应

转载 作者:太空宇宙 更新时间:2023-11-03 23:56:17 24 4
gpt4 key购买 nike

我正在尝试在我创建的模拟中实现碰撞响应。基本上,该程序模拟一个球以某个初始速度从 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

最佳答案

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

//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 用于根据速度方程式的积分计算在时间内由于恒定加速度而行进的距离。由于您正在进行求和并且已经在速度方程中包含加速度,因此无需在位置方程中包含加速度。

关于c - 在仿真中实现碰撞响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6193083/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com