-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
146 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1 @@ | ||
/* | ||
* This file was generated by the Gradle 'init' task. | ||
*/ | ||
|
||
rootProject.name = "Galactifun2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 47 additions & 15 deletions
62
src/main/kotlin/io/github/addoncommunity/galactifun/api/objects/properties/Orbit.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,55 @@ | ||
package io.github.addoncommunity.galactifun.api.objects.properties | ||
|
||
import io.github.addoncommunity.galactifun.api.objects.UniversalObject | ||
import io.github.addoncommunity.galactifun.util.Constants | ||
import io.github.addoncommunity.galactifun.util.LazyDouble | ||
import io.github.addoncommunity.galactifun.util.units.Distance | ||
import kotlin.math.PI | ||
import kotlin.time.Duration | ||
import kotlin.time.DurationUnit | ||
import io.github.addoncommunity.galactifun.util.units.Distance.Companion.meters | ||
import io.github.addoncommunity.galactifun.util.units.doubleSeconds | ||
import kotlinx.datetime.Instant | ||
import java.util.* | ||
import kotlin.math.acos | ||
import kotlin.math.cos | ||
import kotlin.math.sin | ||
import kotlin.math.sqrt | ||
|
||
class Orbit(val semimajorAxis: Distance, year: Duration) { | ||
data class Orbit( | ||
val parent: UniversalObject, | ||
val semimajorAxis: Distance, | ||
val eccentricity: Double, | ||
// Our orbits are always flat, so inclination is always 0, and we don't need to store it | ||
val argumentOfPeriapsis: Double, | ||
val timeOfPeriapsis: Instant | ||
) { | ||
val meanMotion by LazyDouble { | ||
val a = semimajorAxis.meters | ||
sqrt(parent.gravitationalParameter / (a * a * a)) | ||
} | ||
|
||
private val year = | ||
EARTH_YEAR * year.toDouble(DurationUnit.DAYS) / 365.25 * 1200000 // why 1200000? it was in the original code | ||
fun meanAnomaly(time: Instant): Double { | ||
val t = (time - timeOfPeriapsis) / Constants.ORBIT_TIME_SCALE | ||
return meanMotion * t.doubleSeconds | ||
} | ||
|
||
val trueAnomaly: Double | ||
get() { | ||
if (year == 0.0) return 0.0 | ||
return (System.currentTimeMillis() % year) * PI * 2 / year | ||
} | ||
private val eccentricAnomalyCache = WeakHashMap<Instant, Double>() | ||
|
||
fun eccentricAnomaly(time: Instant): Double { | ||
return eccentricAnomalyCache.getOrPut(time) { kelpersEquation(meanAnomaly(time), eccentricity) } | ||
} | ||
|
||
fun trueAnomaly(time: Instant): Double { | ||
val cosE = cos(eccentricAnomaly(time)) | ||
return acos((cosE - eccentricity) / (1 - eccentricity * cosE)) | ||
} | ||
|
||
fun radius(time: Instant): Distance { | ||
val e = eccentricAnomaly(time) | ||
val a = semimajorAxis.meters | ||
return (a * (1 - eccentricity * cos(e))).meters | ||
} | ||
} | ||
|
||
/** | ||
* The number of Minecraft days in an Earth year | ||
*/ | ||
private const val EARTH_YEAR = 30 | ||
tailrec fun kelpersEquation(m: Double, e: Double, guessE: Double = m): Double { | ||
val nextGuess = m + e * sin(guessE) | ||
return if (nextGuess == guessE) nextGuess else kelpersEquation(m, e, nextGuess) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
21 changes: 21 additions & 0 deletions
21
src/main/kotlin/io/github/addoncommunity/galactifun/util/LazyDouble.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package io.github.addoncommunity.galactifun.util | ||
|
||
import kotlin.reflect.KProperty | ||
|
||
class LazyDouble(private val supplier: () -> Double) { | ||
|
||
private var value = Double.NaN | ||
private var initialized = false | ||
operator fun getValue(thisRef: Any?, property: KProperty<*>): Double { | ||
if (!initialized) { | ||
value = supplier() | ||
initialized = true | ||
} | ||
return value | ||
} | ||
|
||
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: Double) { | ||
this.value = value | ||
initialized = true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/main/kotlin/io/github/addoncommunity/galactifun/util/units/Time.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package io.github.addoncommunity.galactifun.util.units | ||
|
||
import kotlin.time.Duration | ||
import kotlin.time.Duration.Companion.days | ||
import kotlin.time.DurationUnit | ||
|
||
inline val Double.years: Duration | ||
get() = (this * 365.25).days | ||
|
||
inline val Int.years: Duration | ||
get() = this.toDouble().years | ||
|
||
inline val Duration.doubleSeconds: Double | ||
get() = toDouble(DurationUnit.SECONDS) |