# Physics gravity conundrum

### » Wed Sep 17, 2008 9:41 am

I'm wondering if anyone has any ideas on making a bunch of like objects attract each other, as though they all have their own gravity. The only way I can think to do it would be with a nested loop, and it seems terribly inefficient and would take up a lot of processing power.

The effect I'm going for can be seen in here:

Where a bunch of little balls clump together and writhe around in a mass with little pieces that break off and stuff.

I've made a secondary gravity object that the little balls all move towards, but it's not quite the same effect (though kinda cool in it's own way).

And just for the record, no, I don't actually need this for anything, it's just to satisfy my curiosity. So if you feel like helping out would be a waste of time I understand. It's basically just for fun.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Wed Sep 17, 2008 9:54 am

ForEach Object nested in ForEach Object doesn't sound good. Maybe you'd try to store object X, Y, mass into some array or binary data object.

You know:
1. ForEach object
-> Populate array/binary data object
2. ForEach object
-> for every record in object

Maybe it'd be faster than
1. ForEach Object
-> ForEach Object
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

### » Wed Sep 17, 2008 11:16 am

If I remember my Physics correctly, all you need to do is calculate the centre of gravity, and make everything fall towards that. This example shows that, but I don't think it's quite perfect (it doesn't take in to account that objects with a bigger mass have a greater effect on the centre of gravity). It shows the right idea though.
Scirra Founder
B
413
S
244
G
92
Posts: 25,082
Reputation: 199,732

### » Wed Sep 17, 2008 11:34 am

[quote="Ashley":36lyi7wo]If I remember my Physics correctly, all you need to do is calculate the centre of gravity, and make everything fall towards that. This example shows that, but I don't think it's quite perfect (it doesn't take in to account that objects with a bigger mass have a greater effect on the centre of gravity). It shows the right idea though.[/quote:36lyi7wo]

That's pretty close to what I've done on my own, what with the objects all moving towards a single point, though yours is a little more sophisticated by averaging that point.

The effect I'm looking for though is one in which each object exerts a small amount of it's own gravity, and the effect is cumulative in larger clusters. As you can see from the video, when a mass breaks apart, single stray objects and small clusters clump together rather than around a central point. Presumably if left alone they would all collapse into one big mass, but each object clearly has it's own influence.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Wed Sep 17, 2008 5:39 pm

[quote="Ashley":14trz2ux]If I remember my Physics correctly, all you need to do is calculate the centre of gravity, and make everything fall towards that.[/quote:14trz2ux]
Soo, do you stay indoors during the day, so you can stand on the ceiling instead of falling into the sun?

As I understand it, what you can do is simplify the whole complicated mess into a binary tree: In each tick, you take two objects, work out how they interact with each other only (ignoring everything else), and from then on treat them as one object with a common center of gravity (and a mass of the sum of their masses, and a velocity of the resultant vector of their individual velocities). Keep simplifying pairs of objects into single objects until you're down to two. Then you propagate the velocity changes back down the tree until all individual sprites know theirs, update the simulation and start the next tick.
B
2
S
1
G
4
Posts: 60
Reputation: 1,320

### » Wed Sep 17, 2008 9:52 pm

[quote="Znirk":te2swrf5]Soo, do you stay indoors during the day, so you can stand on the ceiling instead of falling into the sun? [/quote:te2swrf5]

Should I be doing this? Now I'm scared

[quote="Znirk":te2swrf5]As I understand it, what you can do is simplify the whole complicated mess into a binary tree: In each tick, you take two objects, work out how they interact with each other only (ignoring everything else), and from then on treat them as one object with a common center of gravity (and a mass of the sum of their masses, and a velocity of the resultant vector of their individual velocities). Keep simplifying pairs of objects into single objects until you're down to two. Then you propagate the velocity changes back down the tree until all individual sprites know theirs, update the simulation and start the next tick.[/quote:te2swrf5]

Took me a second to grasp the concept, but I'm lost as to how you could accomplish this with Construct events.
Moderator
B
5
S
2
G
6
Posts: 4,348
Reputation: 10,971

### » Wed Sep 17, 2008 10:52 pm

[quote="Znirk":2zh2or8j][quote="Ashley":2zh2or8j]If I remember my Physics correctly, all you need to do is calculate the centre of gravity, and make everything fall towards that.[/quote:2zh2or8j]
Soo, do you stay indoors during the day, so you can stand on the ceiling instead of falling into the sun? [/quote:2zh2or8j]
No, because you have to take in to account the Earth's centre of gravity and the Earth's mass! (My example does not take in to account mass, which I noted). In reality, the centre of gravity you fall towards will be ever so slightly towards you from the centre of the Earth's gravity due to your own mass, but the effect is vanishingly small.
Scirra Founder
B
413
S
244
G
92
Posts: 25,082
Reputation: 199,732

### » Wed Sep 17, 2008 10:55 pm

Made an example situation with three objects located in lovely Pitagoras triangle. Maybe I've screwed physics laws but here's the result:

Maybe there'd be some use of acceleration vectors counted on each of binary tree steps?
B
6
S
3
G
6
Posts: 219
Reputation: 3,013

### » Sat Sep 20, 2008 2:20 pm

If I can find out the command for SQAUREROOT in Construct, I think I might be able to get this working... tried squareroot, sqrt, square, etc etc to no avail.

Anyone know what it is?

~Sol
Tired of crappy file hosts that are crappy? Get DROPBOX - https://db.tt/uwjysXJF
Moderator
B
84
S
34
G
41
Posts: 3,032
Reputation: 30,999

### » Sat Sep 20, 2008 2:23 pm

[quote="SoldjahBoy":1r3j68fr]If I can find out the command for SQAUREROOT in Construct, I think I might be able to get this working... tried squareroot, sqrt, square, etc etc to no avail.

Anyone know what it is?

~Sol[/quote:1r3j68fr]
sqrt()
B
4
S
2
G
4
Posts: 719
Reputation: 2,938

Next