-
Notifications
You must be signed in to change notification settings - Fork 0
/
physics_utils.js
58 lines (49 loc) · 2.21 KB
/
physics_utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
const gravitational_acceleration = 9.8e-6;
const gravitational_constant = 6.6743e-23;
const speed_of_light = 299.79;
function gas_particle_speed(temperature, molar_mass) {
// get root mean squared speed of gas
// temperature: Kelvin,
// molar_mass: kilograms per mole.
if (temperature <= 0.0 || molar_mass <= 0.0) {
return -1.0;
}
return sqrt((3 * GAS_CONSTANT * temperature) / molar_mass) / 1_000_000;
}
function schwarzchild_radius(planet_mass) {
// limit radius slider. no black holes
// mass is in 10^20kg, distance in 10^6 meters (million meters),
// returns in 10^6 meters (million meters).
return 10e14 * (2 * gravitational_constant * planet_mass) / Math.pow(speed_of_light, 2);
}
function schwarzchild_mass(planet_radius) {
//limit planet mass slider. no black holes
return planet_radius * Math.pow(speed_of_light, 2) / (10e14 * 2 * gravitational_constant);
}
function planet_mass(planet_radius, planet_density) {
return 4 / 3 * Math.PI * Math.pow(planet_radius, 3) * planet_density;
}
function oops_all_exosphere(planet_radius, planet_density, temperature, molar_mass) {
var gas_speed = gas_particle_speed(temperature, molar_mass);
var escape_velocity = 2 * gravitational_acceleration * planet_mass(planet_radius, planet_density) / pow(speed_of_light, 2);
if (gas_speed >= escape_velocity) { return true; }
else { return false; }
}
export function temperature_to_rgb(t) {
// t: temperature in Kelvin.
if (t <= 0) { return null; }
t /= 1000;
var red = Math.min(1, Math.max(0, (-3 / 46) * t + 1.43));
//var green = Math.max(0, Math.min(0.5 * Math.pow(t + 0.1, 2 / 5) - 0.05, 1, Math.max(0, (-2.2 / 46) * t + 1.32)));
var green = Math.max(0, Math.min(0.5 * Math.pow(t - 0.4, 1/2) - 0.05, 1, Math.max(0, (-2.2 / 46) * t + 1.32)));
var blue = Math.max(0, Math.min(1, Math.pow(t - 0.4, 1 / 3) - 0.85));
return [red, green, blue];
}
export function rgb_to_hex(rgb_list, scale_multiplier){
var c = [
Number(parseInt(rgb_list[0] * scale_multiplier)).toString(16),
Number(parseInt(rgb_list[1] * scale_multiplier)).toString(16),
Number(parseInt(rgb_list[2] * scale_multiplier)).toString(16)
];
return "#" + (c[0].length == 1 ? "0" : "") + c[0] + (c[1].length == 1 ? "0" : "") + c[1] + (c[2].length == 1 ? "0" : "") + c[2];
}