Gravity with Vector calculation

In summary, the conversation is about implementing a Vector class into a C++ gravity simulation program and encountering difficulties with the compiler due to the lack of a defined * operator for the Vector class. The individual variables were changed into Vector variables, but the compiler could not compile the gravity formula. The solution was to define the * operation for the Vector class.
  • #1
Sword7
19
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
  • #2
You have to define the * operation for the class Vector. Google "operator overload".
 
  • #3


Hello Sword7,

It seems like you are on the right track with incorporating vector calculations into your gravity simulation program. However, as the compiler is indicating, you need to define the multiplication operator for your Vector class in order to use it in your formula.

In order to do this, you will need to overload the multiplication operator for your Vector class. This can be done by creating a function within your class that takes in another Vector object as a parameter and performs the necessary calculations to return the result of the multiplication.

Once you have defined the multiplication operator, you should be able to use it in your formula and the compiler should no longer complain about it being undefined. I recommend doing some research on operator overloading in C++ to get a better understanding of how to implement this in your code.

Best of luck with your gravity simulation program! Keep exploring and learning about vector math and you will be able to create even more complex and accurate simulations in the future.

Best regards,
 

1. What is gravity?

Gravity is a natural phenomenon by which all objects with mass are brought towards each other. It is the force that keeps us grounded on Earth and governs the motion of the planets in our solar system.

2. How is gravity calculated using vectors?

Gravity can be calculated using the vector equation F = G * (m1 * m2 / r^2), where F is the force of gravity between two objects, G is the gravitational constant, m1 and m2 are the masses of the two objects, and r is the distance between them.

3. What is the difference between scalar and vector quantities in gravity?

Scalar quantities in gravity, such as mass and distance, only have magnitude and no direction. Vector quantities, such as force and acceleration due to gravity, have both magnitude and direction.

4. How does gravity affect the motion of objects?

Gravity causes objects to accelerate towards each other. This acceleration is directly proportional to the mass of the objects and inversely proportional to the square of the distance between them.

5. What role does the direction of gravity play in vector calculations?

The direction of gravity is crucial in vector calculations as it determines the direction of the force acting on the objects. This allows us to accurately predict the motion of objects under the influence of gravity.

Similar threads

  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
2
Views
2K
  • Programming and Computer Science
Replies
7
Views
17K
  • Atomic and Condensed Matter
Replies
3
Views
861
Replies
1
Views
585
  • Special and General Relativity
Replies
1
Views
528
  • Introductory Physics Homework Help
Replies
11
Views
763
  • Introductory Physics Homework Help
Replies
5
Views
1K
Replies
18
Views
2K
  • Introductory Physics Homework Help
Replies
12
Views
2K
Back
Top