Thanks so much! It uses a variant of the missile algorithm :) The game basically looks into the future by calculating where everything will be - both the enemy ships and where your bullet would go if you fired now - in a simplified gravity-only simulation. When it sees the paths cross, that's when your aiming line turns orange. (Sorry for the late reply, crunch time at work!)
Viewing post in Bullet path prediction (v0.0.10) comments
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:
- forceAtPosition, acceleration as before
- currentVelocity += acceleration * deltaTime
- nextPosition = currentPosition + currentVelocity * deltaTime
- (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