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=32 ;
int i,j,m,k;
// sphere **ball; // <--------------------------------------------------this is achange I made
sphere *ball[nballs]; // <--------------------------------------------------this is achange I made
// ball=(sphere *)malloc(nballs*sizeof(sphere));
mlimit=tmax/dt;
//initialise positions
for(i=0;i<nballs;i++)
{
ball[i]=(sphere *)malloc(sizeof(sphere)); // <--------------------------------------------------this is achange I made
// 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[i]->vvel=1.0;
ball[i]->vvel=0.0;
//i++;
}
ball[0]->xpos=(real) 0 * (2*ball[0]->radius + gap);
//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;
}
This is a bit more like how I would have done the memory allocation.
Note I allocated space for one ball at a time. Up to 32.
Code:
int nballs=32 ;
int i,j,m,k;
// sphere **ball; // <--------------------------------------------------this is achange I made
sphere *ball[nballs]; // <--------------------------------------------------this is achange I made
Indeed if you use
I will also work (I think) it did when I tried anyway but I think this is a bug as I am not declaring space for the pointers to the balls as I do with the
Code:
sphere *ball[nballs];
declaration.
It's useful to do it like that if you do not know at run time how many balls you will have, so you just allocate a new one when needed saving memory.
I also need to free up the memory at the end and I will post and update to do that when I have more time.
Do you would need a loop freeing up the memory at the end, but windows does that for you when the program terminates anyway.