Hi. I’m new here and this is my first post.
I’m making a simulation of the solar system,using an approximation for orbit.it’s just the beginning of the project
There is a sphere orbiting around the origin and i have added a script component to the sphere,that simulates the orbit.
The script is below.
The application works for a few minutes and then crashes and i don’t know why.
I’ve tried to use “deltatime”,but the result is the same
Any ideas to solve the problem?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Stride.Core.Mathematics;
using Stride.Input;
using Stride.Engine;
namespace MyGame
{
public class Script3 : SyncScript
{
// Declared public member fields and properties will show in the game studio
float semiMajorAxis,eccentricity,orbitalPeriod,rotationPeriod;
float x, y, z, e1, r, m0,n,p, trueAnomaly0, trueAnomaly1, cosang, remainder,deltaT,tm,rotationDelta;
int retrogradeOrbit, retrogradeRotation;
public override void Start()
{
// Initialization of the script.
deltaT = 5;
retrogradeOrbit =1;
rotationDelta = 0.0f;
tm = 0;
e1 = 0;
r = 0;
x = y = z = 0;
orbitalPeriod = 1.77f;
rotationPeriod = 1.77f;
eccentricity = 0.4f;
semiMajorAxis =7;
n =(float)( 2 * Math.PI / Math.Abs(orbitalPeriod));
m0 = n;
trueAnomaly0 = m0;
p = semiMajorAxis * (1 - eccentricity * eccentricity);
}
public override void Update()
{
// Do stuff every new frame
var deltaTime = (float)Game.UpdateTime.Elapsed.TotalSeconds;
tm = tm + deltaT*deltaTime;
m0 = n * tm;
trueAnomaly1 = m0 + eccentricity * (float)Math.Sin(trueAnomaly0);
while (Math.Abs(trueAnomaly1 - trueAnomaly0) > 0.001f)
{
trueAnomaly0 = trueAnomaly1;
trueAnomaly1 = m0 + eccentricity * (float)Math.Sin(trueAnomaly0);
}
r = semiMajorAxis * (1 - eccentricity * (float)Math.Cos(trueAnomaly1));
cosang = (p / r - 1) / eccentricity;
x = r * cosang;
z = r * (float)Math.Sqrt(Math.Abs(1 - cosang * cosang));
remainder = trueAnomaly1 % (float) (Math.PI * 2);
if (remainder > Math.PI && remainder < System.Math.PI * 2)
z = -z;
y =Entity.Transform.Position.Y;
Entity.Transform.Position = (new Vector3(x, y, z ));
}
}
}