patterncsharpModerate
Simulating gravitational attraction between bodies
Viewed 0 times
attractionbodiesgravitationalsimulatingbetween
Problem
I'm currently working on a simple simulator in Unity3D that involves planets orbiting each other. I wrote the following C# script to do that. It uses the classic gravitational formula,
$$F=G\frac{m_1m_2}{r^2}$$
For those who are curious, a gif of the script in action can be found here.
$$F=G\frac{m_1m_2}{r^2}$$
using UnityEngine;
///
/// Class used for calculating
/// how an object orbits another
/// object.
///
/// The parent gameObject to orbit.
/// The mass of the object.
/// The gravitational constant.
[RequireComponent(typeof(Rigidbody))]
public class OrbitObject : MonoBehaviour
{
public OrbitObject parentObject;
public float objectMass;
public float gravityConstant;
private Vector3 parentPosition;
private float parentMass;
///
/// Calculates the gravitational attraction.
///
/// The force to apply
Vector3 CalculateGravitationalForce()
{
this.parentPosition = this.parentObject.transform.position;
Vector3 force = this.parentPosition - transform.position;
float distance = force.magnitude;
float strength = (this.gravityConstant * this.parentMass * this.objectMass) / (distance * distance);
force.Normalize();
force = force * strength;
return force;
}
///
/// FixedUpdate is run once per frame.
/// FixedUpdate is used so that calculations
/// don't need to be multiplied by Time.deltaTime.
///
void FixedUpdate()
{
rigidbody.AddForce(this.CalculateGravitationalForce());
}
///
/// Start this instance.
///
void Start()
{
this.parentPosition = this.parentObject.gameObject.transform.position;
this.parentMass = this.parentObject.objectMass;
}
}For those who are curious, a gif of the script in action can be found here.
Solution
Just a note on your commenting:
Should you be breaking XML comments up across lines like that?
I feel that should be three lines:
Likewise, I feel that the following comment:
Could be rewritten as:
Personally, I think breaking comments along multiple lines like that makes it harder to read. You're eyes have to jump to unnatural places.
Likewise, the `
Again, these are all suggestions. Whether or not you like/use them is up to you, but this is how I would write it out.
Should you be breaking XML comments up across lines like that?
///
/// Class used for calculating
/// how an object orbits another
/// object.
/// I feel that should be three lines:
///
/// Class used for calculating how an object orbits another object.
/// Likewise, I feel that the following comment:
///
/// FixedUpdate is run once per frame.
/// FixedUpdate is used so that calculations
/// don't need to be multiplied by Time.deltaTime.
/// Could be rewritten as:
///
/// FixedUpdate is run once per frame.
///
///
/// FixedUpdate is used so that calculations don't need to be multiplied by Time.deltaTime.
/// Personally, I think breaking comments along multiple lines like that makes it harder to read. You're eyes have to jump to unnatural places.
Likewise, the `
bit is nice because those comments aren't shown in Intellisense, but they can still be placed in the generated XML documentation files.
The only other bit I would mention, is the use of this. You should either use it consistently, or try to limit it. Sometimes (especially in larger cases) it helps to clear things up. But if you use it for some of the properties and not others, it can have a negative impact.
I'm referring to the following:
Vector3 CalculateGravitationalForce()
{
this.parentPosition = this.parentObject.transform.position;
Vector3 force = this.parentPosition - transform.position;
float distance = force.magnitude;
float strength = (this.gravityConstant * this.parentMass * this.objectMass) / (distance * distance);
force.Normalize();
force = force * strength;
return force;
}
Where does transform.position come from? The fact that you use this` on all the other properties/fields of the class but not that one throws confusion into the mix. It's not a local variable, that much is true. But where exactly does it come from, is a little harder to notice right out of the box.Again, these are all suggestions. Whether or not you like/use them is up to you, but this is how I would write it out.
Code Snippets
/// <summary>
/// Class used for calculating
/// how an object orbits another
/// object.
/// </summary>/// <summary>
/// Class used for calculating how an object orbits another object.
/// </summary>/// <summary>
/// FixedUpdate is run once per frame.
/// FixedUpdate is used so that calculations
/// don't need to be multiplied by Time.deltaTime.
/// </summary>/// <summary>
/// FixedUpdate is run once per frame.
/// </summary>
/// <remarks>
/// FixedUpdate is used so that calculations don't need to be multiplied by Time.deltaTime.
/// </remarks>Vector3 CalculateGravitationalForce()
{
this.parentPosition = this.parentObject.transform.position;
Vector3 force = this.parentPosition - transform.position;
float distance = force.magnitude;
float strength = (this.gravityConstant * this.parentMass * this.objectMass) / (distance * distance);
force.Normalize();
force = force * strength;
return force;
}Context
StackExchange Code Review Q#96395, answer score: 10
Revisions (0)
No revisions yet.