As part of my own learning course I am looking at building solar system models using Unity as a platform, I know that this can be possible with the “Universe Sandbox” being developed in Unity.
I came across a C++ code that I’m using for reference, however I came across a bit of an unknown in a variable.
static const fixed SUN_MASS_TO_EARTH_MASS = fixed(332998,1);
Doing a little investigation I found that obviously “static” is a norm in programming and the “SUN_MASS_TO_EARTH_MASS” is just the variable name. However “fixed” and “const” are throwing me a little bit as was "fixed(332998, 1), but that bit I figured out to be a ratio as the ratio of the sun to the earth is roughly 333000 : 1.
So my question is, how can I set that ratio in Unity as it is used elsewhere in the code as part of a formula, and also what is “fixed” and “const” and can they be replicated in unity?
const is a constant variable in C++, meaning it cannot be changed (assigned to) and will remain the same at all times during the code’s execution.
fixed is the variable type. This simply means it is represented in fixed point notation. This is done for performance, or in case the device cannot perform floating point operations.
In short: You can just use this as a regular int or float as required. Anybody with a device capable of running Unity can do floating point calculations.
const var SUN_MASS_TO_EARTH_MASS = 332998;
Add private/public/static as needed (I’m not sure on the exact context of your script).
Well “fixed” means “fixed point” as opposed to “floating point” - i.e. the number has a fixed number of decimal digits (1 in this case).
Const means it’s a value not a variable and cannot be changed by the programme - which allows certain compiler optimisations.
Off hand in UnityScript I don’t know if you can declare a const, but I wouldn’t worry about it. You could use a System.Decimal to hold a fixed point number, but there’s no fiddling with the number of digits with a decimal - I wouldn’t bother and just define it as:
static var SUN_MASS_TO_EARTH_MASS = 332998.0;
Perhaps if calculations get very big you could declare it as a double:
static var SUN_MASS_TO_EARTH_MASS : double = 332998;
Doubles use twice the memory and are hence more accurate than floats - but you will end up having to cast things back to float to use in other parts of Unity.