I just want to create a simple code of a moving ball colldiing to each other. I am able to print the results, however, my fprintf isn't working . I am quite new to this, so please help? Thank you in advance.
After running it just says "EASY.exe has stopped working..."
My compiler didnt show where the error either.
Code:
#include <stdio.h>
#include <math.h>
#include <conio.h>
typedef double real;
typedef struct
{
real xpos;
real ypos;
real zpos;
real uvel;
real vvel;
real wvel;
real omegax;
real omegay;
real omegaz;
real radius;
real mass;
} sphere;
void update_position(sphere *ball, real dt)
{
//(*ball).xpos
ball->xpos=ball->xpos + dt*ball->vvel;
//ypos,zpos
}
void detect_ball(sphere *ball1, sphere *ball2, real *pdist)
{
*pdist=sqrt((ball2->xpos-ball1->xpos)*(ball2->xpos-ball1->xpos)+
(ball2->ypos-ball1->ypos)*(ball2->ypos-ball1->ypos));
// if ( dist<= (ball1->radius + ball2->radius) +0.0075 )
// {
// if ( dist>= ball1->radius + ball2->radius -0.0075 )
// {
// *ptime=0;
// }
// }
}
int main(int argc, char *argv)
{
real dt=0.01;
real t, dist1, tmax=1, mlimit, firstvvel, gap=1.5, dist[2], ballmatxpos[101][2];
int nballs=1 ;
int i,j,m,k;
sphere *ball;
ball=(sphere *)malloc(nballs*sizeof(sphere));
mlimit=tmax/dt;
//initialise positions
for(i=0;i<=nballs;i++)
{
ball[i].radius=0.5;
ball[i].xpos=(real) i * (2*ball[i].radius + gap);
ball[i].ypos=0.0;
ball[i].uvel=1.0;
ball[0].vvel=1.0;
ball[1].vvel=0.0;
}
//pendulum to be released
// ball[0].xpos=0;
//ball[0].ypos=0;
// firstvvel=0;
//timestep loop
t=0.0;
dist1=0.0;
while(t<tmax)
{
for(m=0;m<=mlimit;m++)
{
//update position
for(i=0;i<=nballs;i++)
{
update_position(&(ball[i]), dt);
}
//collision detection
for(i=0;i<=nballs;i++)
{
for(j=0;j<=nballs;j++)
{
detect_ball(&(ball[i]),&(ball[j]), &dist1);
if ( dist1 <= (ball[i].radius + ball[j].radius) +0.075 )
{
if ( dist1 >= (ball[i].radius + ball[j].radius) -0.075 )
{
// ball2->uvel=ball1->vvel;
//yes collision, modify velocities and rotation rates
t=0;
}
}
}
}
ballmatxpos[m][0]=t;
ballmatxpos[m][1]=ball[0].xpos;
printf("In this case, initial velocity= %3.4lf with t=%3.4lf \n", ballmatxpos[m][0], t );
t+=dt;
}
}
// ---------------//-------------------------
FILE *pFile1;
FILE *pFile2;
pFile1 = fopen("graphing3.txt", "w");
pFile2 = fopen("graphing4.txt", "w");
if (pFile1 != NULL)
{
for(m=0;m<=mlimit;m++)
{
fprintf( pFile1, " %3.4lf \n ", ballmatxpos[m][0]);
}
fprintf( pFile1,"\n");
fclose(pFile1);
}
if (pFile2 != NULL)
{
for(m=0;m<=mlimit;m++)
{
fprintf( pFile2, " %3.4lf \n", ballmatxpos[m][1]);
}
fprintf( pFile2,"\n");
fclose(pFile2);
}
else
{
printf("inda dapat print bro");
}
// -----------------------------------------------------
getch();
return 0;
}