- Home /

# problems with single body Newtonian physics emulation

I've made a class that holds all the parameters for an orbit

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Orbit
{
public Transform target;
public Vector3 centreOffset;
public Vector3 axisA;
public Vector3 axisB;
public float M;
public float semiMajorAxis;
public float semiMinorAxis;
public float apoapse;
public float periapse;
public float phase;
public float period;
public Orbit(Vector3 offset, Vector3 tangent, Transform target, float M, float periapse, float eccentricity, float phase)
{
this.target = target;
this.M = M;
this.phase = phase;
axisA = offset.normalized;
axisB = (tangent - Vector3.Dot(tangent, axisA) * axisA).normalized;
this.periapse = periapse;
apoapse = periapse * ((1 + eccentricity) / (1 - eccentricity));
semiMajorAxis = (periapse + apoapse) / 2;
semiMinorAxis = Mathf.Sqrt(semiMajorAxis * semiMajorAxis - (semiMajorAxis - periapse) * (semiMajorAxis - periapse));
centreOffset = axisA * Mathf.Lerp(-periapse, apoapse, 0.5f);
period = 2 * Mathf.PI * Mathf.Sqrt((semiMajorAxis * semiMajorAxis * semiMajorAxis) / M);
}
public PosVel Solve(float t)
{
return new PosVel(Vector3.zero, Vector3.zero);
}
}
public class PosVel
{
public Vector3 position;
public Vector3 velocity;
public PosVel(Vector3 position, Vector3 velocity)
{
this.position = position;
this.velocity = velocity;
}
}
```

note that target is the transform at the focus of the orbit and M is equal to the mass of the body * the gravitational constant. I'm not sure how to go about writing the solve function. t is supposed to be a given time, and solve should return a vector3 for the satellite's position at time t, and a vector3 velocity in m/s.

I can't find anything online about implementing this kind of thing, and although I know about kepler's laws, my lack of much formal math education makes them difficult to implement.

I've also written a monobehavior as follows for visualizing all the data in the above class so if you're interested in helping you needn't go to that effort.

```
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteInEditMode]
public class DrawOrbit : MonoBehaviour
{
Orbit orbit;
[Range(0,1)]
public float ecc = 0.2f;
public float per = 1f;
public Vector3 h1;
public Vector3 h2;
public float sma;
private void OnValidate()
{
orbit = new Orbit(h1, h2, transform, 1f, per, ecc, 0f);
sma = (orbit.apoapse + orbit.periapse)/2;
}
private void OnDrawGizmos()
{
Gizmos.color = Color.black;
float incr = Mathf.PI / 200f;
for (float i = 0; i < 2 * Mathf.PI; i += incr)
{
Vector3 from = orbit.axisA * orbit.semiMajorAxis * Mathf.Sin(i) + orbit.axisB * orbit.semiMinorAxis * Mathf.Cos(i);
Vector3 to = orbit.axisA * orbit.semiMajorAxis * Mathf.Sin(i + incr) + orbit.axisB * orbit.semiMinorAxis * Mathf.Cos(i + incr);
Gizmos.DrawLine(from + orbit.centreOffset, to + orbit.centreOffset);
}
Gizmos.color = Color.yellow;
Gizmos.DrawWireSphere(h1, 0.15f);
Gizmos.DrawWireSphere(h2, 0.15f);
Gizmos.color = Color.red;
Gizmos.DrawLine(Vector3.zero, orbit.axisA);
Gizmos.color = Color.green;
Gizmos.DrawLine(Vector3.zero, orbit.axisB);
Gizmos.color = Color.blue;
Gizmos.DrawWireSphere(orbit.axisA * -orbit.periapse, 0.2f);
Gizmos.DrawWireSphere(orbit.axisA * orbit.apoapse, 0.2f);
}
}
```

as an aside I also want to write a constructor for orbit that takes M of the body and the relative position and velocity of the satellite, and I still need to handle the case if the satellite is at or above escape velocity, but both of those will come after the solve function works.

if you attempt to tackle this problem then thank you in advance, regardless of if you're successful or not.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.