Skip to content

Commit

Permalink
orbitalkiller: implemented variable dt!
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrey Borunov committed Jun 22, 2016
1 parent 807e04f commit 16b97ac
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 13 deletions.
Expand Up @@ -126,6 +126,7 @@ object OrbitDataUpdater {
drawSlidingLines(yy, orbit_color)
if(InterfaceHolder.realTrajectorySwitcher.showRealTrajectory && RealTrajectory.realTrajectory.nonEmpty) {
drawSlidingLines(RealTrajectory.realTrajectory, orbit_color)
//drawSlidingLines(RealTrajectory2.realTrajectory, orbit_color)
}
}
/*drawLine(new_o.f*scale, new_o.center*scale, GRAY)
Expand Down Expand Up @@ -245,6 +246,7 @@ object OrbitDataUpdater {
openglLocalTransform {
openglMove(planet_state.coord * scale)
drawSlidingLines(RealTrajectory.realTrajectory, orbit_color)
//drawSlidingLines(RealTrajectory2.realTrajectory, orbit_color)
}
}
if(InterfaceHolder.namesSwitcher.showNames) {
Expand Down
Expand Up @@ -164,11 +164,13 @@ object OrbitalKiller extends ScageScreenAppDMT("Orbital Killer", property("scree
system_cache.clear()
_update_orbits = true
RealTrajectory.init()
//RealTrajectory2.init()
}
}

actionDynamicPeriodIgnorePause(500 / timeMultiplier) {
RealTrajectory.continue()
//RealTrajectory2.continue()
}

val sun = new Star(
Expand Down Expand Up @@ -952,7 +954,7 @@ object OrbitalKiller extends ScageScreenAppDMT("Orbital Killer", property("scree

keyIgnorePause(KEY_N, 100, onKeyDown = {
if (InterfaceHolder.realTrajectorySwitcher.showRealTrajectory) {
InterfaceHolder.realTrajectorySwitcher.numPoints += 3600
InterfaceHolder.realTrajectorySwitcher.numPoints += 24*3600
}
})

Expand All @@ -961,7 +963,7 @@ object OrbitalKiller extends ScageScreenAppDMT("Orbital Killer", property("scree
if(RealTrajectory.curPoints < InterfaceHolder.realTrajectorySwitcher.numPoints) {
InterfaceHolder.realTrajectorySwitcher.numPoints = RealTrajectory.curPoints
} else {
InterfaceHolder.realTrajectorySwitcher.numPoints = 3600
InterfaceHolder.realTrajectorySwitcher.numPoints = 24*3600
}
needToUpdateOrbits("reset real trajectory num points")
}
Expand Down
Expand Up @@ -6,12 +6,19 @@ import scala.collection.immutable
import scala.collection.mutable.ArrayBuffer
import OrbitalKiller._

object RealTrajectory {
object RealTrajectory extends RealTrajectoryC
/*object RealTrajectory2 extends RealTrajectoryC {
override protected def chooseDt:Double = base_dt
}*/

class RealTrajectoryC {
private var real_trajectory:ArrayBuffer[DVec] = ArrayBuffer[DVec]()
var curPoints:Int = 0
var curPoints:Long = 0
private var dropped = 0
private var system_evolution_copy:SystemEvolution = _
private val seconds_per_iteration = 100
private var celestials:Seq[(CelestialBody, MutableBodyState)] = _

private val angle_diff = 1

def init(): Unit = {
real_trajectory.clear()
Expand All @@ -21,16 +28,37 @@ object RealTrajectory {
base_dt,
exclude = immutable.Set(station.index, sat1.index, sat2.index, cargo1.index),
collisions_enabled = false)
celestials = system_evolution_copy.allBodyStates.filter(kv => planet_indices.contains(kv._1)).flatMap(kv => {
planets.get(kv._1).map(planet => (kv._1, (planet, kv._2)))
}).values.toSeq
}

protected def chooseDt:Double = {
// выберем dt по критерию расстояния корабля до ближайшей поверхности: в 100 км от поверхности объект, движущийся со скоростью 30 км/сек
// за dt должен пролететь не более 500 метров (это справедливо для base_dt). Чем дальше, тем dt может быть больше.
// но пока работают двигатели, dt должен быть равен base_dt, иначе неверно работают формулы.
if(player_ship.engines.exists(_.stopMomentTacts > system_evolution_copy.tacts)) {
base_dt
} else {
system_evolution_copy.bodyState(player_ship.thisOrActualProxyShipIndex).map(bs => {
val min_dist_to_any_surface = celestials.map(x => bs.coord.dist(x._2.coord) - x._1.radius).min
//0.005 * math.log(min_dist_to_any_surface / bs.vel.norma) / math.log(10 / 3) * 10 / 3
0.005 * min_dist_to_any_surface / bs.vel.norma
}).getOrElse(base_dt)
}
}

def continue(): Unit = {
if(InterfaceHolder.realTrajectorySwitcher.showRealTrajectory && InterfaceHolder.realTrajectorySwitcher.numPoints > curPoints) {
val seconds_in_this_iteration = math.min(seconds_per_iteration, InterfaceHolder.realTrajectorySwitcher.numPoints - curPoints)
val last_step = (seconds_in_this_iteration*(1/OrbitalKiller.base_dt)).toInt
val new_base_dt = chooseDt
system_evolution_copy.base_dt = new_base_dt
// выбираем так, чтобы в цикле было ровно 100 итераций.
val seconds_in_this_iteration = math.min((6300*system_evolution_copy.base_dt).toInt, InterfaceHolder.realTrajectorySwitcher.numPoints - curPoints)
val last_step = math.max(1, (seconds_in_this_iteration*(1/system_evolution_copy.base_dt)).toInt)
if(real_trajectory.length >= 3) {
val prev_line = real_trajectory(real_trajectory.length-2) - real_trajectory(real_trajectory.length-3)
val cur_line = real_trajectory(real_trajectory.length-1) - real_trajectory(real_trajectory.length-2)
if (cur_line.absDeg(prev_line) <= 10) {
if (cur_line.absDeg(prev_line) <= angle_diff) {
real_trajectory.remove(real_trajectory.length-1)
dropped += 1
}
Expand Down Expand Up @@ -59,7 +87,7 @@ object RealTrajectory {
} else {
val prev_line = real_trajectory.last - real_trajectory.init.last
val cur_line = next_point - real_trajectory.last
if(cur_line.absDeg(prev_line) > 10) {
if(cur_line.absDeg(prev_line) > angle_diff) {
real_trajectory += next_point
} else {
dropped += 1
Expand All @@ -69,7 +97,7 @@ object RealTrajectory {
}
})
curPoints += seconds_in_this_iteration
println(s"real trajectory curPoints/numPoints dropped/length: $curPoints/${InterfaceHolder.realTrajectorySwitcher.numPoints} $dropped/${real_trajectory.length}")
println(f"real trajectory dt ${new_base_dt/base_dt}%.2f*base_dt curPoints/numPoints $curPoints/${InterfaceHolder.realTrajectorySwitcher.numPoints} dropped/length $dropped/${real_trajectory.length}")
}
}

Expand Down
Expand Up @@ -85,7 +85,7 @@ case class MutableSystemPart(body: MutableBodyState,
force: (Long, EvolutionHelper) => DVec,
torque: (Long, EvolutionHelper) => Double)

class SystemEvolution(val base_dt: Double = 1.0 / 63,
class SystemEvolution(var base_dt: Double = 1.0 / 63,
system_center: DVec = DVec.zero,
init_tacts: Long = 0,
collisions_enabled:Boolean = true) {
Expand Down Expand Up @@ -296,7 +296,7 @@ class SystemEvolution(val base_dt: Double = 1.0 / 63,
}

def copy(dt:Double = base_dt, exclude:Set[Int] = Set.empty, collisions_enabled:Boolean = true): SystemEvolution = {
val x = new SystemEvolution(dt, system_center, tacts)
val x = new SystemEvolution(dt, system_center, tacts, collisions_enabled)
mutable_system.foreach(p => {
if(p._2.body.active && !exclude.contains(p._1)) {
x.addBody(p._2.copy(body = p._2.body.copy))
Expand Down
Expand Up @@ -15,7 +15,7 @@ class RealTrajectorySwitcher extends InterfaceSwitcher {

def showRealTrajectory:Boolean = selectedVariant != 0

var numPoints:Int = 3600
var numPoints:Long = 24*3600

override def switchForward(): Unit = {
super.switchForward()
Expand Down

0 comments on commit 16b97ac

Please sign in to comment.