Gravity with Vector calculation

Click For Summary
SUMMARY

The forum discussion centers on implementing vector calculations in a C++ gravity simulation program. The user, Sword7, encountered compilation errors with the GCC compiler due to the lack of operator overloading for the multiplication operator (*) in their custom Vector class. Initially, the user attempted to perform vector operations directly, which led to errors. The solution provided involves defining the multiplication operator for the Vector class to enable proper calculations.

PREREQUISITES
  • Understanding of C++ programming language
  • Familiarity with object-oriented programming concepts
  • Knowledge of operator overloading in C++
  • Basic principles of vector mathematics
NEXT STEPS
  • Research C++ operator overloading techniques
  • Learn about implementing vector classes in C++
  • Explore mathematical operations on custom data types
  • Study the principles of gravity simulation in physics
USEFUL FOR

C++ developers, physics simulation programmers, and anyone interested in implementing vector mathematics in their 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".
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
17K
  • · Replies 2 ·
Replies
2
Views
11K
Replies
18
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 14 ·
Replies
14
Views
8K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K