• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Simulating Gravity for multiple objects

I am making a solar system simulation where the planets will orbit a star. I am going to simulate gravity using Newton's equations. The only problem is that I don't know how to find the gravitational force for all objects in a room. For example, I can make 2 different objects gravitate towards each other but I want to have just one object that I can place multiple times in a room and have it them all interact.
 

chamaeleon

Member
Just do what you would do for one pair, but for all pairs (except possibly updating the instance for both, just update it for one of them).
 
Just do what you would do for one pair, but for all pairs (except possibly updating the instance for both, just update it for one of them).
I want to have a system where I can just use one type of object for all of the planets but I don't know how to do this. I was thinking I could use a for loop to find all of the instances in the room and then calculate the gravity for all of those objects.
 

FrostyCat

Redemption Seeker
A for loop that goes through all instances of an object is called a with block. Learn how to use that.

The 3 rules of a with block:
  • Code inside the block run from the perspective of an instance of the subject.
  • The original instance is referred to as other inside the block.
  • Local variables (i.e. those declared with var) remain in-scope inside the block.
GML:
with (objPlanet) if (id != other.id) {
    motion_add(point_direction(x, y, other.x, other.y), G*mass*other.mass/max(1, sqr(point_distance(x, y, other.x, other.y))));
}
 
A for loop that goes through all instances of an object is called a with block. Learn how to use that.

The 3 rules of a with block:
  • Code inside the block run from the perspective of an instance of the subject.
  • The original instance is referred to as other inside the block.
  • Local variables (i.e. those declared with var) remain in-scope inside the block.
GML:
with (objPlanet) if (id != other.id) {
    motion_add(point_direction(x, y, other.x, other.y), G*mass*other.mass/max(1, sqr(point_distance(x, y, other.x, other.y))));
}
Thank you! I do not want to use the built in motion add function but I now know exactly how to do what I was thinking of.
 

chamaeleon

Member
Thank you! I do not want to use the built in motion add function but I now know exactly how to do what I was thinking of.
Ideally it means accumulating the total change for each instance without updating the position at the same time. Either a two-part process in a controller object, or if each instance handles itself, calculate the changes in step perhaps, and updating in end step, or use begin step and step the same way. If you don't, for some given pair, when you calculate it for the second instance in the pair, the first will have moved slightly, and the second one will not use the same state as the first did in order to find out the effect they have on each other.
 
GML:
with (objPlanet) if (id != other.id) {
    motion_add(point_direction(x, y, other.x, other.y), G*mass*other.mass/max(1, sqr(point_distance(x, y, other.x, other.y))));
}
Shouldn't it be the square of point_distance(x, y, other.x, other.y) rather than the square root of point_distance(x, y, other.x, other.y)
 
Top