To get right to the point, I need to get the gravitational force between two point masses as a vector. Currently I have a function which can get the magnitude of the vector, but how do I calculate the vector itself? More of a math problem than anything else, but I figured i'd post it here. Function to get magnitude:
[code]
public double GetAppliedPower(Particle p){
return (6.67300*Math.pow(10, -11))*((Mass*p.GetMass())/Point2D.distance(position.x, position.y, p.getPos().x, p.getPos().y));
}
[/code]
If I understood you right, you want a vector applied to one of the bodies in the direction of the other with <gravitation force between them> as the magnitude.
If that's it:
1) obtain a vector between the two points composed of x2-x1, y2-y1 where x1,y1 are the coordinates of the body the force is applied on and x2,y2 are the coordinates of the other one. Let's call this vector v1
2) obtain a unit vector with the same direction as v1 by dividing v1 by its magnitude. (magnitude of v1 = sqrt((x2-x1)^2 + (y2-y1)^2). Let's call this vector v2
3) obtain the vector you want by multiplying v2 by <the gravitation force between the bodies>
That should work, unless my math is incredibly shitty atm due to alcohol.
PS: if you are working in 3D space, you'll need to adapt this to include z co-ordinates of course
[b]Edit:[/b]There's probably a way of obtaining the vector directly without going trough all those steps. let me think about it for a bit.
Ah. Hah. Yeah. Didn't think about that. No wait, that will only work if the body to be moved has no current vector. How can I *some term that I am not familiar with here (interpolate, maybe?)* the current vector and the vector obtained by doing what you said such that the direction of the body to be moved seems to turn? If that makes any sense what so ever.
You add them?
Correcting my edit, the way to obtain the force really is by multiplying the scalar value by the unit vector from object 1 to 2.
Answering your second question: If you then want to move the body based on this force, there's a bit of math to be done. If I were doing this, here's what I would do:
1) calculate a general "resulting force" vector for the body being moved which is the vectorial sum of all forces being applied to the body.
2) Calculate acceleration for the body, based on this force. (Newton's 2nd law, F=ma where F is the sum of all forces, m is mass and a is acceleration.)
3) Based on this, apply the laws of motion in each iteration (of what I am assuming is some sort of simulation) to calculate the velocity and position in each moment.
Note that you probably want to run this not only for one body but for every body in the system. If A pulls B, B pulls A with equal force. Of course that if the mass of A is much larger than the mass of B then A won't move much, but if you want to be accurate, you need to move them both.
Once you know what the magnitude of the force is, do this:
[code]
Vector delta = body2.pos - body1.pos;
delta.normalize();
delta.scale(force);
body1.applyForce(delta);
delta.negate();
body2.applyForce(delta);
[/code]
[QUOTE=Smashmaster;28571984]Once you know what the magnitude of the force is, do this:
[code]
Vector delta = body2.pos - body1.pos;
delta.normalize();
delta.scale(force);
body1.applyForce(delta);
delta.negate();
body2.applyForce(delta);
[/code][/QUOTE]
Is that box2d, pseudocode or something else?
[QUOTE=Smashmaster;28571984]Once you know what the magnitude of the force is, do this:
[code]
Vector delta = body2.pos - body1.pos;
delta.normalize();
delta.scale(force);
body1.applyForce(delta);
delta.negate();
body2.applyForce(delta);
[/code][/QUOTE]
Very helpful, except that I have no idea how to make pos-pos a vector, instead of another pos.
Bah, is there a library for this in java? like VectorMath or something?
[QUOTE=bobthe2lol;28572309]Very helpful, except that I have no idea how to make pos-pos a vector, instead of another pos.
Bah, is there a library for this in java? like VectorMath or something?[/QUOTE]
this is pseudocode
[code]
vector delta = new vector(0,0);
delta.x = body2.pos.x - body1.pos.x;
delta.y = body2.pos.y - body1.pos.y
[/code]
[QUOTE=grlira;28572661]this is pseudocode
[code]
vector delta = new vector(0,0);
delta.x = body2.pos.x - body1.pos.x;
delta.y = body2.pos.y - body1.pos.y
[/code][/QUOTE]
Sorry for being quite thick, but what if there are more than 2 bodys?
Position is a vector, so pos2-pos1 will be a vector.
If there are more than two bodies, you repeat the sequence for each pair.
[QUOTE=Metroid48;28573338]Position is a vector, so pos2-pos1 will be a vector.[/QUOTE]
And that's because adding vectors is just adding their co-ordinates respectively and subtracting is just adding the symmetric.
[QUOTE=Metroid48;28573338]If there are more than two bodies, you repeat the sequence for each pair.[/QUOTE]
Which can be computationally demanding. There's an optimization tho': Because the force with which body A pulls B ( represented as A <- B from here on out) is equal in magnitude and symmetric in orientation to the force with which body B pulls A (B <- A), once you compute A <- B, you can just get the symmetric of each of the components of that vector and directly get B <- A. (of course the acceleration vectors can be different in magnitude if the masses are different)
This allows you to do n + (n-1) + ... + 1 calculations instead of n^2 calculations.
Does anyone know of a better way?
The nice thing about force vectors is you just add them up to get a resultant force.
With more than 2 bodies, for each of them get the force vector and add it to a rolling force vector.
I'd also strongly advise you don't use the real life gravitational constant as it's really fucking awkward to deal with as it's so tiny. If you must use it I suggest you declare it as a constant static double (if your language supports this).
Sorry, you need to Log In to post a reply to this thread.