In the below code, is the method I use to move a bullet to its destination and stop when bullet is near to destination. But the problem is the bullet is stopping not accurately, sometimes there's a small offset or span or sometimes is accurate depends on the angle.
public Vector2 getVelocity(Vector2 currentPosition, Vector2 targetPosition) {
Vector2 targetDirection = targetPosition.cpy().sub(currentPosition);
return targetDirection
.nor();
}
final float SPEED = 6;
float pos = bulletPosition.len();
float des = bullet.destination.len();
float tol = Constants.TIME_STEP * SPEED;
if(MathUtils.isEqual(pos,des, tol)) {
body.setLinearVelocity(0,0);
} else {
body.setLinearVelocity(getVelocity(bulletPosition, destination).scl(SPEED));
}
[SOLVED]
if(MathUtils.isEqual(pos, des, tol)) {
// quick & dirty way
body.setTransform(destination, angle);
// better way
...
}
MathUtils.isEqual
do exactly, is it a direct comparision or an approximation? \$\endgroup\$MathUtils
java doc link \$\endgroup\$