# Making a Solar System - Vector Math

This topic is 3352 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

So this is a continuation of the last thread that I started but in this one I will completely explain what I am doing. I am making a solar system simulation where the gravity of everything affects the the orbit of everything else. What I need to do right now is add together all the vectors of gravitational forces acting on a planet (from the sun and other planets) and then calculate out its resultant vector. I am not sure how to add 3D vectors and find the resultant. Below is the code for adding 2D vectors using an angle and magnitude, 0 degrees being north. //Take in number of vectors cout << "Enter Number of Vectors between 2 and 10" << endl; cin >> numberofvectors; //Take in vector data for(int i=0; i<numberofvectors; ++i) { system("cls"); FORMAT; cout << "Enter Mangnitude for " << i + 1 << endl; cin >> mag; cout << endl; cout << "Enter Angle for " << i + 1 << endl; cin >> ang; } //Calculate x and y component for ( int i = 0; i<numberofvectors; ++i) { x = x + (( mag)* (cos(2*pival*ang/360))) ; y = y + (( mag)* (sin(2*pival*ang/360))); } //Calculate Magnitude magnitude = sqrt((x*x) + (y*y)); //Calculate the angle theta = atan(y/x)*(180/pival); //Calculate the quadrent of angle if (x<0) { if(y>0) { theta = theta + 180; } } if (x<0) { if(y<0) { theta = theta + 180; } } if (x>0) { if(y<0) { theta = theta + 360; } } system("cls"); cout << "The x component of the resultant is " << x << endl; cout << "The y component of the resultant is " << y << endl; cout << "The magnitude of the resultant is " << magnitude << endl; cout << "The angle of the resultant is " << theta << endl; The question is how do I convert this code so it can do the same for 3D objects. Note: I will always have access to the x,y,z locations and the gravitational pull of every planet.

##### Share on other sites
Use [source] tags when posting code. Also, assuming you have it available, use the 'atan2' function to compute angles rather than 'atan' (it's more numerically robust, and eliminates the need for computing the quadrant manually).

To address your question regarding vector math, adding two vectors (in any dimension) is easy: each element of the vector sum is the sum of the corresponding elements of the input vectors. For example:
(x1,y1) + (x2,y2) = (x1+x2,y1+y2)
I think where you're running into trouble is that you're thinking primarily in terms of polar coordinates, with Cartesian coordinates as an afterthought. I would recommend instead working primarily in Cartesian coordinates (with Cartesian coordinates, summing the input vectors will be the same in 3-d as it is in 2-d).

Perhaps you're using polar coordinates during the input process because it seems more intuitive to enter an angle and magnitude than to enter an x and a y coordinate. That's perfectly reasonable - you can acquire the input in that form, but then you should immediately convert to Cartesian coordinates (IMO) and work in Cartesian space from there on.

If you find it's a more intuitive way to enter the data, you can do the same in 3-d by acquiring the input as spherical coordinates (two angles and a radius/magnitude), and then converting to Cartesian coordinates.

##### Share on other sites
So, if I take in values as Cartesian (x,y,z) what would the formula or function look like for the resultant calculator?

What I'm specifically asking for a function that takes in multiple vectors and then gives me the resultant with x,y,z co-ordinates.

[I'm sorry if this seems like a question I should know the answer too, I am a little bit of trouble with understanding vectors]

##### Share on other sites
Quote:
 So, if I take in values as Cartesian (x,y,z) what would the formula or function look like for the resultant calculator?What I'm specifically asking for a function that takes in multiple vectors and then gives me the resultant with x,y,z co-ordinates.[I'm sorry if this seems like a question I should know the answer too, I am a little bit of trouble with understanding vectors]
Assuming I'm understanding the question correctly, there's no big mystery - you just add them together like you would real numbers :)

Here's an example though to make it a little more clear:
vector2 sum(vector2[] v, int numVectors) {    vector2 sum = zero_vector();    for (int i = 0; i < numVectors; ++i) {        sum.x += v.x;        sum.y += v.y;    }    return sum;}
Normally, in a language that supports it we would use an overloaded operator for the '+=' operation, but here I wrote it out longhand for clarity.

Does that help at all?

##### Share on other sites
Quote:
As I said earlier, adding vectors together is fundamentally the same regardless of dimension. The only thing that differs is the number of elements involved.

Here's what the 3-d version would look like:
vector3 sum(vector3[] v, int numVectors) {    vector3 sum = zero_vector();    for (int i = 0; i < numVectors; ++i) {        sum.x += v.x;        sum.y += v.y;        sum.z += v.z;    }    return sum;}
Starting to make sense?

##### Share on other sites
Yes, but how do I factor in the magnitude? Do I do it that same way as with the original function?

The magnitude will be the amount of gravitational force being put out by a planet or star.

For exmaple the planet at 10,10,10 is putting out X amount of gravitational pull on the planet at 0,0,0 and the planet at -5,-5,10 is pulling on it with 2X amount of force.

##### Share on other sites
Quote:
 Yes, but how do I factor in the magnitude?
It is factored in :)

You might be a little confused about the different ways a vector can be represented, and the relationship between those representations. I'm not going to undertake an explanation at the moment, but there are plenty of good references available online. If you haven't already, I suggest Googling for e.g. 'vector math' or 'vector tutorial' and see what you find.

I will give you a hint though: in the case of a 2-d vector (for example) the direction (angle) and magnitude are implicit in the x and y coordinates. So, when you're working with the x, y coordinates, you're also implicitly working with the direction and magnitude.

##### Share on other sites
Ok, that makes a lot more sense. That means that I have to break down the magnitude into its x and y pieces.

But the question I'm having now is that the direction. For example the planet at 10,10,10 is putting out 10 units of gravitational pull on the planet at 0,0,0 and the planet at -5,-5,10 is pulling on it with 20 units of force.

[Sorry if I'm not getting this, for some reason this topic is really confusing]

##### Share on other sites
Elaborating: magnitude is completely dependent upon the X,Y and Z components in a 3D vector, and can be found by an equation derived from Pythagoras:

|V| = √(x2 + y2 + z2)

Where |V| is the magnitude of vector V.

If you're not convinced, you can derive it yourself.

I'm pretty sketchy on the polar representation of 3D vectors, so I can't help you there.

1. 1
2. 2
Rutin
19
3. 3
khawk
18
4. 4
5. 5
A4L
11

• 9
• 12
• 16
• 26
• 10
• ### Forum Statistics

• Total Topics
633769
• Total Posts
3013754
×