- Home /
m/s "Meters" convert to au/s "Astronomical Units"
Hello,
How would i go about converting units of measurements from m/s to au/s
So far my speed measurements are in meters a second "m/s" but the numbers become overwhelming at high speeds, light-speed, hyper-speed and so on.
So as soon as i get to the speed of 1,495,978,707 meters it then start to convert units? Thanks.
1,495,978,707 m/s = 0.01 au/s
14,959,787,070 m/s = 0.1 au/s
149,597,870,700 m/s = 1.0 au/s
I am sure there is a better way than what i am trying to do here, Any suggestions?
void Update(){
au = velocity/149597870700;
if(au <= 0.01f)
{
shipVelocity.text = "Velocity: " + velocity + " m/s";
}
if(au >= 0.0099f){
shipVelocity.text = "Velocity: " + au + " au/s";
}
}
Uhm this seems pretty ok? Are you worried about the division? If you would like to sacrifice precision, you can use bit shift to obtain a faster division. (I suggest shifting 30 bits to the right for approximating a division by 1495978707.)
As a concept there's nothing wrong with what you are doing. But the condition should just be an if-else. You don't need 2 if's to check if a number is greater than X. If the value is under 0.01au, you show speed in m/s. In all other cases you want au, right?
if(au <= 0.01f)
{
shipVelocity.text = "Velocity: " + velocity + " m/s";
}
else
{
shipVelocity.text = "Velocity: " + au + " au/s";
}
You could also micro optimize by calculating the threshold speed in m/s in the beginning of the game and then calculating au only if you need to show it. But there's no need for that unless you have hundreds of objects calculating this in Update()
Spot on, thanks for the suggestions they really do help allot,
For the if statements, I originally had 3 speeds m/s, $$anonymous$$/s, au/s so it was if, else if, else, but thanks for pointing that out i didn't notice it until i read your comment.
Your answer
Follow this Question
Related Questions
Convert Canvas space to World coordinates 2 Answers
Space unit size 2 Answers
Convert Units to Pixels and back 1 Answer
How to convert pixels to Unity units? 3 Answers
How to calculate distance in meters? 1 Answer