Skip to main content

Indie game storeFree gamesFun gamesHorror games
Game developmentAssetsComics
SalesBundles
Jobs
TagsGame Engines
(1 edit)

In general I think the predictor should mimic the physics engine as much as possible. For velocity & position, your Predict seems to be doing a variant of Størmer-Verlet, while (if you're on Unity) PhysX apparently does semi-implicit Euler, which my physics engine also uses, so that's what I use in my prediction code:

  1. forceAtPosition, acceleration as before
  2. currentVelocity += acceleration * deltaTime
  3. nextPosition = currentPosition + currentVelocity * deltaTime
  4. (assign nextPosition to positions[i], currentPosition)

Also make deltaTime the same as what PhysX uses. Oh and if your planets move, make sure to use their future (not current) position for the force (and if your planets also move under gravity... you need to predict everyone at the same time :) ).

Thank you so much for your response, Patrick! I truly appreciate your effort to help me, and I'm learning a lot through this process. Unfortunately, the issue still persists.

Unity 2D physics is based on  Box2D, which uses the same Semi-Implicit Euler integration you talked. I adjusted my algorithm to match this, but it didn't resolve the problem.

The main challenge lies in my virtual gravity algorithm. It calculates totalForceApplied as a vector and then applies it using Unity's AddForce method. However, AddForce only updates the Rigidbody's velocity in the next physics frame, which makes it unsuitable for immediate trajectory predictions.

Because of this limitation, I'm trying to simulate the physics myself. Despite my efforts, it hasn't been working, and I've been stuck on this for over a week.

I think it's best to take a break from this problem and focus on other tasks for now. Sometimes a fresh perspective helps! Thank you so much for your support—I truly appreciate it!

Piece of code showing how I manage the virtual gravity

Hey sure, happy to help! Good luck with replicating the physics in the predictor, I don't know Unity but I know that can be tricky :)