kachilous
kachilous

Reputation: 2529

Implementing Collision Response in Simulation

I am attempting to implement collision response in a simulation that I am creating. Basically, the program simulates a ball being thrown off a 50 meter building with some initial velocity.

I don't believe that the program is outputting realistic values for time of collision as well as values for x, y and vx, vy.

Here is the program:

 #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;
  }

Basically, I am trying to produce accurate values so that I can see a plot of what this simulation is supposed to look like. I am assuming the logical errors have something to do with the physics. But being that my physics knowledge is limited, I am not able to see what exactly is wrong.

Here is sample input: ax: 0 ay: -9.8 x: 0 y: 50 vx: 8.66 vy: 5

Upvotes: 2

Views: 312

Answers (1)

JAB
JAB

Reputation: 21089

It seems to me that your problem may lie in how you're implementing the kinematics equations.

//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);

Two things here: you're already taking the acceleration into account in your equations for vx and vy, and you're using summation rather than integrated equations. The .5*ax*deltaTime*deltaTime and .5*ay*deltaTime*deltaTime shouldn't be included. The equation x= 0.5*a*t^2 is used when calculating the distance traveled due to a constant acceleration for the total amount of time, based on the integral of the velocity equation. As you're doing summation and already include the acceleration in your velocity equations, there's no need to include the acceleration in the position equations.

Upvotes: 1

Related Questions