C
C#2w ago
Ab

How do I fix this jittery behaviour when calculating distances?

No description
3 Replies
Ab
Ab2w ago
So here, I'm calculating the direction between some target and where the projectile currently is. Then I keep moving the projectile to the target until it reaches it. The issue is, when it does reach it, it's very jittery, oscillating back and forth rapidly and correcting itself until it settles onto the target a few seconds later. Am I doing this badly?
Marvin
Marvin2w ago
sounds like if your projectile is too fast, it is too far away in one frame, and the next frame already on the other side out of the min dist?
LPeter1997
LPeter19972w ago
@Ab If your velocity is so big that even velocity * deltaTime is greater than the distance, you'll overshoot, then keep overshooting in the other direction, getting you this oscillation A really simple fix would be to maximize the step length you take in the distance to the target Something like this (pseudocode, written in chat):
var toTarget = target.Position - projectile.Position;
var toTargetNorm = Vector2.Normalize(toTarget);
var moveMagnitude = Math.Min(projectile.Velocity * deltaTime, toTarget.Length);
projectile.Position += moveMagnitude * toTargetNorm;
var toTarget = target.Position - projectile.Position;
var toTargetNorm = Vector2.Normalize(toTarget);
var moveMagnitude = Math.Min(projectile.Velocity * deltaTime, toTarget.Length);
projectile.Position += moveMagnitude * toTargetNorm;
Want results from more Discord servers?
Add your server