![space flight simulator rockets space flight simulator rockets](https://imag.malavida.com/mvimgbig/download-fs/spaceflight-simulator-22863-8.jpg)
Sometime brute force is the best solution.Ever needed to construct your personal house rocket, set off for Mars and go away this planet for good? Nicely, quickly you’ll (fake to) just do that, as Spaceflight Simulator is headed to Nintendo Swap. You don't need conics, or advanced math, or any of that. I recommend the former if you have less than a dozen planets. Either apply the gravity of all planets, or simply the gravity of the closest planet taking its mass into account. If you want more than one-body gravity simulation, it's simple.
![space flight simulator rockets space flight simulator rockets](https://media2.spaceq.ca/wp-content/uploads/2020/08/29110517/kerbal_space_flight_simulator_fig3.jpg)
No need for fancy calculations when you got all the computing power you need.
#Space flight simulator rockets code
Note that this code is untested, I probably made a mistake here or there, but that's the gist of it.ĭon't worry about getting any difference in trajectory, this is exactly what unity does to predict positions. Replace by a TrailRenderer you reset each frameĭebug.DrawLine(prevPos, currentPos, Color.Red, ltaTime) Var forceMag = Gravity / distance.sqrMagnitude ĬurrentVelocity += forces * Time.fixedDeltaTime ĬurrentPos += currentVelocity * Time.fixedDeltaTime Var currentVelocity = _rigidBody.velocity Now that's all out the way, you can do a very simple approximation by doing something like var currentPos = _rigidBody.position Satelite.GetComponent().AddForce(forces, ForceMode.Acceleration) ForceMode.Force divides the force by the mass of the rigidbody it is applied on. Or better yet, assuming Mass is the mass of the satellite, and gravity is not a general constant, but just the local gravity of your planet. Satelite.GetComponent().AddForce(forces, ForceMode.Force) What you want here is to switch to ForceMode.Impulse, or remove the deltaTime. ForceMode.Force already multiplies the Force by ltaTime. Now that the code is much simpler, I can find a crucial mistake. You can further reduce it by doing: var distance = sateliteCords - planetCords Here's what your code is actually doing: var sateliteCords = ĭistanceFromSatelite = distance.magnitude įorces = distance / distanceFromSatellite * -F I'm a bit of a noob when it comes to physics simulations so can one of you geniuses please help?
#Space flight simulator rockets how to
I read somewhere that I need a copy of the solar system running independently form the unity updates, and calculate the rocket trajectory from there but I don't know how to approach that. I have tried "speeding up the simulation" with Time.timeScale = 50 but it isn't fast enough. Satelite.GetComponent().AddForce(forces * ltaTime, ForceMode.Force) īut I don't know how to predict future points in time to give me the orbit shape. Var distance = sateliteCords - planetCords ĭistanceFromSatelite = Vector3.Distance(, ) į = (Gravity * Mass) / Mathf.Pow(distanceFromSatelite,2) įorces = (distance / Mathf.Sqrt(Mathf.Pow(distance.x, 2)+ Mathf.Pow(distance.y, 2)+ Mathf.Pow(distance.z, 2))) * -F I have managed to get realistic physics and orbits when adding gravitational forces to a rigidBody: var sateliteCords = I am trying to make a game with Unity that uses rockets that are in orbit and can transition to different planets, but I need to predict the rocket path in real-time, depending on the current velocity/direction of the rocket allot like spaceflight simulator does.