Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Loops and Arrays (simulation programming)

Author
Message
FIRESTORM 000
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 22nd May 2008 07:57
I'm looking to program a simulator that can calculate the trajectory of an indefinite number of objects through space. They would all experience the gravity of each other and a potentially large and unknown number of objects would all need to be evaluated. whether or not it's 2 or 3d doesn't matter. The display is rather strait forward.

I want to use 2 for loops and a 2 dimensional array to store the location velocity etc of the objects. This is my basic idea:


for (int loop1 = 0; #OfObjects > loop1; loop1 ++)
{
for (int loop2 = loop1 + 1; loop2 < #OfObjects; loop2 ++)
{
//calculate the first objects atraction to each
//other object and store the results in the array
}
}


The concept should work, but I need help on the implementation
any improvements/ suggestions/ code examples would be welcome

Thanks
jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 22nd May 2008 09:15
change the first line to
for (int loop1 = 1; loop1<=objects; loop1 ++)

and the third to
for (int loop2 = loop1 + 1; loop2 <= objects; loop2 ++)
and that should work, assuming that every time you create a new object, you increment objects. e.g.

etc
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 22nd May 2008 18:09
Don't you want to set loop1 to zero on the first loop and limit the condition to loop1 < objects, not <= ? That way when the first loop reaches the next to last object it only tests for the one remaining object.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
jason p sage
17
Years of Service
User Offline
Joined: 10th Jun 2007
Location: Ellington, CT USA
Posted: 22nd May 2008 22:10
Here Here - based on usually these kinds of loops are iterating thorugh arrays which have their first index starting at Zero.

Otherwise - counting 0 thru Limit-1 or 1 thru Limit is superfluous... what a strange word... superfluous... its right up there with ambiguous.

jezza
16
Years of Service
User Offline
Joined: 8th Mar 2008
Location: Bham, UK
Posted: 23rd May 2008 09:21
yeha sorry forgot u were using an array. Why do you need a 2D array?
FIRESTORM 000
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 25th May 2008 22:03 Edited at: 25th May 2008 22:40
Sorry, I haven't been able to do much work on this, finals and all that, but I think a 2d array would be the best way to go, because it could be declared however large I need to accommodate however many objects there are.

I don't think there is away, or at least I'm not aware of a way, to do this with just normal variables

I would think of having 1 dimension for each object, and the second store all of their information

EX:

object ID__001 002 003 004 005 ...
X position_010 003 063 183 902 ...
Y Pos______300 097 083 643 206 ...
any other ... ... ... ... ... ...
program
specific info


since each object would have there own position, speed,
and who knows how many other properties that would need to be
effected by many other variables, a matrix (2d array), should be the best way to go.

I'll try to have an example ready soon
FIRESTORM 000
16
Years of Service
User Offline
Joined: 21st May 2008
Location:
Posted: 25th May 2008 22:41
Sorry, I haven't been able to do much work on this, finals and all that, but I think a 2d array would be the best way to go, because it could be declared however large I need to accommodate however many objects there are.

I don't think there is away, or at least I'm not aware of a way, to do this with just normal variables

I would think of having 1 dimension for each object, and the second store all of their information

EX:

object ID 001 002 003 004 005 ...
X position 010 003 063 183 902 ...
Y Pos______300 097 083 643 206 ...
any other ... ... ... ... ... ...
program
specific info


since each object would have there own position, speed,
and who knows how many other properties that would need to be
effected by many other variables, a matrix (2d array), should be the best way to go.

I'll try to have an example ready soon

Login to post a reply

Server time is: 2024-11-20 11:42:42
Your offset time is: 2024-11-20 11:42:42