Gravity with Vector calculation

AI Thread Summary
The discussion revolves around implementing vector calculations in a C++ gravity simulation program. The user initially encountered compilation errors when trying to use a custom class, Vector, for gravitational force calculations. The original scalar code was replaced with vector operations, but the gcc compiler raised issues due to the lack of an overloaded multiplication operator for the Vector class. The user found a temporary solution by manually accessing the x component of the Vector for calculations. A key suggestion provided was to define the multiplication operator for the Vector class, which can be achieved through operator overloading in C++. This approach would enable proper vector arithmetic in the gravity formula, allowing for more efficient and cleaner code.
Sword7
Messages
19
Reaction score
2
Hello folks,

I implemented class Vector into my C++ gravity simulation program that I recently started to write from scratch. I changed individual variables into Vector variable but gcc compiler refuse compile gravity formula with Vector. My original code was:

Fgx = (G * M * m * Px) / (r*r*r);
Fgy = (G * M * m * Py) / (r*r*r);
Fgz = (G * M * m * Pz) / (r*r*r);

Ax = Fgx / m; Ay = Fgy / m; Az = Fgz / m;
Vx += Ax; Vy += Ay, Vz += Az;

My new code is:

Vector Ps, Fs;

Ps = Vector(7000 * 1000, 0, 0);

Fs = (G * M * m * Ps) / (r*r*r);

As = Fs / m;
Vs += As;

I tried to compile that but gcc compiler complaint about operator * not defined in class Vector for that. I had changed that code and it now worked:

Fs.x = (G * M * m * Ps.x) / (r*r*r);
:

Does anyone know any solution with vector calculation within gravity formula? I am new to vector math.

Thanks!
Sword7
 
Technology news on Phys.org
You have to define the * operation for the class Vector. Google "operator overload".
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

Similar threads

Back
Top