public static DReal RandomRange(DReal min, DReal max) { var range = DReal.Max(0, max - min); var n = RandomValue() / (uint)0xFFFFFFFF; return(n * range + min); }
// This could be smarter. If dest is too close & perpendicular, then the tank // can end up circling around. public void MoveTowards(DVector2 dest) { ComSat.Trace(this, "MoveTowards"); var dir = dest - entity.position; // also vector to dest. var targetAngle = DVector2.ToAngle(dir); var baseAngle = Utility.CalculateNewAngle(entity.rotation, targetAngle, DReal.Radians(turnSpeed)); entity.rotation = baseAngle; // Move along current heading. Ramp speed up as the angle gets closer. // Augh. // [-pi,pi] => [0,2pi] if (targetAngle < 0) { targetAngle += DReal.TwoPI; } // Get targetAngle within +/- pi of baseAngle. if (targetAngle < baseAngle - DReal.PI) { targetAngle += DReal.TwoPI; } else if (targetAngle > baseAngle + DReal.PI) { targetAngle -= DReal.TwoPI; } var diff = DReal.Abs(baseAngle - targetAngle); if (canMoveWithoutTurning || diff < maxMoveAngle) { var distance = dir.magnitude; //print("Distance: " + distance + " speed is: " + tickSpeed); var speed = minSpeed + (maxSpeed - minSpeed) * (1 - (diff / DReal.PI)); if (distance < speed) { speed = DReal.Max(minSpeed, distance); } entity.velocity = canMoveWithoutTurning ? dir.normalized * speed : DVector2.FromAngle(baseAngle) * speed; } else { Stop(); } }