|
No, it can't. Posts can not be moved between unrelated forums.
|
|
|
|
|
It wasn't exactly hard to find; all I did was type "AutoHotkey" into Google.
|
|
|
|
|
Rick,
I misunderstood you, I thought you meant it was a group on this forum. Unfortunately that forum is not really active, and I have tried to ask there. Do you think you could, or would be able to take on a task like this?
Thank You
ThatOldGuy
|
|
|
|
|
ThatOldGuy wrote: Do you think you could, or would be able to take on a task like this? Sorry, I have no idea what AutoHotKey is, or what it is supposed to do; that's why I pointed you to their website.
|
|
|
|
|
Rick,
Ok, no worries. I appreciate the response.
Thank You
|
|
|
|
|
Hello,
I am trying to make a program in C with multiple bouncing balls. The problem is that I have created a code, but my program doesn’t run properly. To be more exact, I think the part where it reads the data is fine, but then there is a problem in the code of the graphics.h library. I cannot understand where the problem is, as it is the first time I use this library.
Thank you in advamce.
Here is my code:
#include <stdio.h>
#include <graphics.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
int main()
{
FILE *input;
int n, *num, i, total, j;
if((input=fopen("data.txt", "r"))!=NULL)
{
fscanf(input, "%d", &n);
total=n*7;
num=(int *) malloc(total*sizeof(int));
if(num!=NULL)
{
for(i=0; i<total; i++)
{
fscanf(input, "%d", &num[i]);
}
fclose(input);
clock_t start,finish, previous;
double step;
int gdriver = DETECT, gmode, errorcode;
initgraph(&gdriver, &gmode, "");
errorcode = graphresult();
if (errorcode != grOk)
{
printf("Graphics error: %s\n", grapherrormsg(errorcode));
system ("pause");
exit(1);
}
start=clock();
previous=start;
do
{
j=0;
for(i=0; i<=n; i++)
{
finish = clock();
step = (finish-previous)*1.0/CLOCKS_PER_SEC;
if (step >= 0.03)
{
previous = finish;
setfillstyle(SOLID_FILL,BLACK);
setcolor(BLACK);
fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
num[j]+= num[j+5]*step;
num[j+1]+= num[j+6]*step;
if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
{
num[j+5] *= -1;
}
if (num[j+1]+num[j+4]>=getmaxy() || num[j+1]-num[j+4]<=0)
{
num[j+6] *= -1;
}
setfillstyle(SOLID_FILL,RED);
setcolor(RED);
fillellipse(num[j],num[j+1],num[j+4],num[j+4]);
j=j+7;
}
}
}
while (!kbhit());
closegraph();
}
else
{
printf("Could not allocate memory\n\n");
}
}
else
{
printf("Could not open file\n\n");
}
system("pause");
return 0;
}
|
|
|
|
|
Member 13632263 wrote: my program doesn’t run properly You need to provide more exact details than that, as we have no idea what you are seeing when you run it. It has to be said that these libraries are a throwback from the old days of MS-DOS and may not work correctly under Windows. See How to use graphics.h in visual C++ 2012?[^].
|
|
|
|
|
When I run it it just shows the Windows error that the program stopped working, so I have to close it.
|
|
|
|
|
I am afraid that information tells us nothing. You need to run your code in the debugger which will help you to find out exactly where it is stopping. But, as I already said, this library is very old and may not work under Windows.
|
|
|
|
|
I have seen similar projects which ran very good. Here is what it shows after the debugging:
https://mega.nz/#!NRwEAJKR!KI5cTeFhAaagIaRlbBoba1ugKt1j2RYEgUkF4lc8Atc
|
|
|
|
|
And what does that tell you?
|
|
|
|
|
To tell the truth, I don't understand.
|
|
|
|
|
Then maybe you should forget this program and start learning C and how it operates in better detail. You cannot become a developer by copying other people's code and guesswork.
|
|
|
|
|
You might get out of bound array accesses when step is always >= 0.63:
total=n*7;
num=(int *) malloc(total*sizeof(int));
do
{
j=0;
for(i=0; i<=n; i++)
{
if (step >= 0.03)
{
j=j+7;
}
}
}
while (!kbhit()); You have to change the for loop to
for(i=0; i<n; i++) I also don't understand why you are only updating when step is large enough. If that does not happen, balls at the end of the array will be updated less often than the initial ones.
You are checking if the new ball position is outside and inverts the direction if so but are still drawing the ball when outside. A better solution might be drawing it at the border (here for x):
if (num[j]+num[j+4]>=getmaxx() || num[j]-num[j+4]<=0)
{
num[j+5] *= -1;
if (num[j]+num[j+4] > getmaxx())
num[j] = getmaxx() - num[j+4];
if (num[j]-num[j+4] < 0)
num[j] = num[j+4];
}
Finally it might not work as expected because you have up to 7 balls and did not handle overlapping ones. I guess you want to detect also ball collisions which would result in no overlapping. But that is also not handled by your code.
|
|
|
|
|
Well, it didn't work and yes I also want to handle overlapping balls and make them bounce together and change direction. But, I don't know how. However, I don't have up to 7 balls. I actually use 3, but each of them has 7 data (color, position, speed etc.). That's why I use i at the for loop to count the number of loops (each ball). Then I use j to go to the desired data of each ball. All the data is being inserted to the program from a .txt file as you can see. It might seem stupid that I don't understand some thing. However, I am a beginner and I am trying to learn. So, please help me and be patient. I really appreciate your help.
|
|
|
|
|
Sorry, I missed the number of balls.
But my other notes are still valid (out of bound array access and why not updating with small steps). For timing purposes (slower movement), wait outside the for loop.
I suggest to start with one ball to implement and check the border bouncing. To be realistic, the new directions should be calculated according to the collision angle.
Then proceed with two balls to make it simpler. For collison detection you need another loop (runing from currently processed ball index + 1 to number of balls) to check for collisions like with the borders. Upon a collision you have to set the collision position as new position for both balls and adjust the directions (which requires a flag / state that the other ball has been already processed within the current loop execution). Again, the new directions should be calculated according to the collision angles.
To check the code, you can suppress the drawing and print out the positions and changings instead. Or write them to a text file so that you can inspect it later. For testing it might be also helpful to stop after each step and continue after a key press. So you can check your calculations.
|
|
|
|
|
I have already made it with one ball and I tried to continue with two, three etc. However, I still do something wrong with more than one balls and I can't understand what. I have been working on this for almost a month and I have tried everything. I also searched the internet and asked at other forums too, but still nothing. I only managed to make it with one ball. I understand what you say and that's what I am thinking too, but I can't put it inside code and make it run correctly.
|
|
|
|
|
Your posted code does not check for ball to ball collisions. You have to implement it. There should be examples in the net for the algorithms.
|
|
|
|
|
Are you trying to recreate a 30-years old xonix game?
|
|
|
|
|
For your next assignment, extend your program to work in three dimensions and use OpenGL to display the balls. For bonus credit, display the bounding cube with translucent walls. In other words, partially transparent but not totally.
Good luck.
|
|
|
|
|
2 errors is coming can anyone help me
|
|
|
|
|
Here are some possibilities: LMGTFY[^]
<sig notetoself="think of a better signature">
<first>Jim</first> <last>Meadors</last>
</sig>
|
|
|
|
|
nithiin_sai wrote: 2 errors is coming can anyone help me
Maybe...
But only if you'll show your code and exact error messages.
|
|
|
|
|
It could be worse, it could be raining.
|
|
|
|
|
nithiin_sai wrote: ...can anyone help me
At this point, no.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|