#### Howdy, Stranger!

We are about to switch to a new forum software. Until then we have removed the registration on this forum.

# Gravity help?

Hey, so I have a question about the gravity of two objects acting on one object. I heard that you just need to find the net gravitational force, but it's not working for me, as the ball just goes in a different direction. Help, please?

``````PVector location1;
PVector velocity1;
PVector acceleration1;
PVector locationSun;
PVector locationSun2;
PVector distance;
float r;
float r1;
float vSun = 1;

void setup() {
size(600, 600);

location1 = new PVector(350, 30);
velocity1 = new PVector(6, 0);
acceleration1 = new PVector(0, 0);

locationSun = new PVector(100, 300);
locationSun2 = new PVector(500, 300);
}

void draw() {
background(0);
r = dist(location1.x, location1.y, locationSun.x+locationSun2.x+100, locationSun.y+locationSun2.y+100);

if ( r < 50 ) {
r = 50;
}

/* println(acceleration1.x);
println(locationSun.x);
println(acceleration1.x);
println(acceleration1.x);*/

acceleration1.x = ((locationSun.x - location1.x)*100000/pow(r, 3)) + (locationSun2.x - location1.x)*100000/pow(r, 3);
acceleration1.y = ((locationSun.y - location1.y)*100000/pow(r, 3)) + (locationSun2.y - location1.y)*100000/pow(r, 3);

//locationSun.x += vSun;

if ((locationSun.x >= width) || (locationSun.x <= 0)) {
vSun *= -1;
}

ellipse(locationSun.x, locationSun.y, 50, 50);
ellipse(locationSun2.x, locationSun2.y, 50, 50);
ellipse(location1.x, location1.y, 20, 20);

}
``````
Tagged:

• Yes indeed, though an easier and slightly more intuitive way would simply be to add each force in turn to the acceleration of the particle. Do look up the reference for PVector; many of the operations you can do with vector math: you don't have to compute each component individually.

• edited November 2015
• Alright cool, so I can directly subtract the location vectors without using two separate components for x and y. Thanks :)

So now, suppose I wanted to add the two forces of the two large objects to get the net force, how would I do that and apply it to the particle?

• ^^ Yesss!

Even bigger fan of his videos..

https://vimeo.com/channels/natureofcode/page:2