tags:

views:

102

answers:

3

i do have 2 points on a 2d plane. one has already an vector that does determine in which direction it will move.

now i want to add a vector to this existing vector. so he accelerates in the direction of the other point.

to be a bit more clear, it is about 2 asteroids flying in space (only 2d) and gravitation should move them a bit closer to each other.

what i did build till now is this:

c = body.position - body2.position;
dist = c.Length();

acc = (body.masse * body2.masse) / (dist * dist);

xDist = body2.position.X - body.position.X;
yDist = body2.position.Y - body.position.Y;

direction = MathHelper.ToDegrees((float)(Math.Atan2((double)yDist, (double)xDist)));

body.velocity.Y = body.velocity.Y + (float)(Math.Sin(direction) * acc);
body.velocity.X = body.velocity.X + (float)(Math.Cos(direction) * acc);

in the moment the direction calculated is completly off. surely i am making just a stupid mistake, but i have no idea.

A: 

Not completely sure what you try to do. Why can't you just use Vector2.Add(v1, v2)?

Razzie
+2  A: 

You need to pass your direction angle in in radians to Math.sin and Math.Cos (rather then in degree as you do in your smaple code).

see also: http://msdn.microsoft.com/en-us/library/system.math.sin.aspx

The angle, a, must be in radians. Multiply by Math.PI/180 to convert degrees to radians.

sum1stolemyname
Probably remove the call to `ToDegrees`, rather than going and multiplying the result ;)
Andrew Russell
@andrew: Exactly.
sum1stolemyname
changing direction = MathHelper.ToDegrees((float)(Math.Atan2((double)yDist, (double)xDist)));to direction = (float)(Math.Atan2((double)yDist, (double)xDist));did the trick. thanks!
ralph
@ralph, your algorithm is not quite correct. Where you are calculating acc, you are missing two things: a gravitational constant and tempering the force by the body's mass. force must be divided by mass to get acceleration, try this for better accuracy:acc = ((GravitationalConstant * (body.mass * body1.mass)) / (dist squared)) / body.mass;You can ignore the gravitational constant if you want to be 1.0, but you shouldn't ignore dividing it by the body's mass at the end. otherwise, it would be like jumping in the air and the earth came up to meet you as fast as you came down to meet it.
Steve H
+1  A: 

My mechanics and linear algebra are a bit rusty but I think you should be able to do it without resorting to trigonometry. These formulae probably need tweaking, I'm not sure if I got u and -u mixed up.

Here it is in pseudo code

T is whatever time period you're iterating over
G is the gravitational constant

body1 starts with  a velocity of v1
body2 starts with  a velocity of v2

c = body.position - body2.position

c1 is a vector
use the vector c to get a vector of length 1 in the direction of the force

u = c1 / c.Length()

body1 should have an acceleration vector of a1 =  G * body2mass/c.Length()^2 * (-u)

body2 should have an acceleration vector of a2 = G * body1mass/c.Length()^2 * (u)

body1 has a new velocity vector of v1 + a1/T

body2 has a new velocity vector of v1 + a2/T

rinse and repeat

Jonny Cundall
i look into that one later. looks a bit more elegant.
ralph