Compare commits

..

2 Commits

Author SHA1 Message Date
926abae31b
add basic anti-speed (relative to the star only for now) 2023-11-25 17:40:00 +01:00
5e8d6b76af
Improve fly modes 2023-11-25 16:08:30 +01:00
5 changed files with 111 additions and 48 deletions

View File

@ -137,7 +137,7 @@ describe Vector do
Vector[1, 1].magnitude.should eq(Math.sqrt(2)) Vector[1, 1].magnitude.should eq(Math.sqrt(2))
Vector[1, 2].magnitude.should eq(Math.sqrt(1**2 + 2**2)) Vector[1, 2].magnitude.should eq(Math.sqrt(1**2 + 2**2))
Vector[2, 2].magnitude.should eq(Math.sqrt(2**2 + 2**2)) Vector[2, 2].magnitude.should eq(Math.sqrt(2**2 + 2**2))
Vector[2, 2, 2].magnitude.should eq((2**2 + 2**2 + 2**2)**(1.0/3.0)) Vector[2, 2, 2].magnitude.should eq((2**2 + 2**2 + 2**2)**(1.0/2.0))
end end
it "normalized vector" do it "normalized vector" do

View File

@ -169,13 +169,16 @@ class Vector(T, N) # should be a struct since it's a simple pointer inside, the
T T
end end
# I think it's always square root not 1.0/size
def magnitude : Number def magnitude : Number
reduce(T.zero) { |base, elem| base + elem**2 } ** (1.0 / size) reduce(T.zero) { |base, elem| base + elem**2 } ** (1.0 / 2.0)
end
# put the vector on a trigonomic circle so sqrt(x**2 + y**2) = 1
def normalize
self / magnitude
end end
def normalize def normalize
magnitude = self.magnitude self.div!(magnitude)
self / magnitude
end end
end end

View File

@ -139,11 +139,26 @@ class Vessel(N)
end end
class Pilot(N) < Station(N) class Pilot(N) < Station(N)
property low_thrust_save, low_thrust_pressed, computer_input_thurst # these properties handle manual override of trust (either analogic or exact computed)
property permanent_mode, temporary_mode property low_thrust_save, low_thrust_pressed, computer_input_thrust
# on the interface, the pilot can select a body to create relative vectors
property current_selected_body : Gravity::Body(N)?
# this is a hard limit for thrust so the pilot can avoid killing all the people aboard
property max_thrust
# permanent mode is the default mode to apply at all time
property permanent_mode
# temporary mode must be set at each tick and will be reset each time it is applied
property temporary_mode
@permanent_mode : Symbol? enum Mode
@temporary_mode : Symbol? Constant
Pulse
AntiGrav
AntiSpeed
end
@permanent_mode : Mode?
@temporary_mode : Mode?
def initialize(*p, **o) def initialize(*p, **o)
super super
@ -151,37 +166,53 @@ class Vessel(N)
@temporary_mode = nil @temporary_mode = nil
@low_thrust_save = 0.0 @low_thrust_save = 0.0
@low_thrust_pressed = false @low_thrust_pressed = false
@computer_input_thurst = Vector(Float64, N).zero @computer_input_thrust = Vector(Float64, N).zero
@max_thrust = 10.0
@current_selected_body = nil
end end
def set_exact_acceleration_axis!(index : Int32, value : Float64) # Modify immediatly one elemnt of the acceleration vector
def set_exact_acceleration_axis(index : Int32, value : Float64)
vessel.gravity_body.acceleration[index] = value vessel.gravity_body.acceleration[index] = value
end end
def set_exact_acceleration_axis!(axis : Vector(Float64, N)) # Modify immediatly the acceleration vector
def set_exact_acceleration_axis(axis : Vector(Float64, N))
axis.each_with_index do |value, index| axis.each_with_index do |value, index|
set_exact_acceleration_axis!(index, value) set_exact_acceleration_axis(index, value)
end end
end end
# set_exact_acceleration_axis!({0, 23.0}, {1, 11.2}) # to set x;y at once # set_exact_acceleration_axis({0, 23.0}, {1, 11.2}) # to set x;y at once
def set_exact_acceleration_axis!(*tuples : Array(Tuple(Int32, Float64))) def set_exact_acceleration_axis(*tuples : Array(Tuple(Int32, Float64)))
tuples.each do |tuple| tuples.each do |tuple|
set_exact_acceleration_axis!(*tuple) set_exact_acceleration_axis(*tuple)
end end
end end
def immediate_mode
@temporary_mode || @permanent_mode
end
def apply_current_mode def apply_current_mode
if (@permanent_mode == :antigrav && @temporary_mode.nil?) || @temporary_mode == :antigrav if immediate_mode == Mode::AntiGrav
# somehow it doesn't work as expected... :thinking: # somehow it doesn't work as expected... :thinking:
set_exact_acceleration_axis!(-vessel.gravity) set_exact_acceleration_axis(-vessel.gravity)
elsif immediate_mode == Mode::AntiSpeed
if (current_selected_body = @current_selected_body)
relative_speed = vessel.gravity_body.speed - current_selected_body.speed
if relative_speed.magnitude > @max_thrust
relative_speed.normalize.mult!(@max_thrust)
end
set_exact_acceleration_axis(-relative_speed)
end
end end
@temporary_mode = nil # each time we apply the temporary_mode, reset it @temporary_mode = nil # each time we apply the temporary_mode, reset it
end end
def accelerate_current_vector!(coef : Float64) def accelerate_current_vector!(coef : Float64)
set_exact_acceleration_axis!(vessel.gravity_body.speed * coef) set_exact_acceleration_axis(vessel.gravity_body.speed * coef)
end end
end end

View File

@ -175,7 +175,7 @@ class Game
end end
if joystick_moved_modified == :vector if joystick_moved_modified == :vector
@vessel.pilot.set_exact_acceleration_axis!(@joystick_moved) @vessel.pilot.set_exact_acceleration_axis(@joystick_moved)
elsif joystick_moved_modified == :accelerate elsif joystick_moved_modified == :accelerate
# can double the vector each second # can double the vector each second
Log.debug "accelerate #{@joystick_moved}" Log.debug "accelerate #{@joystick_moved}"

View File

@ -59,8 +59,8 @@ module PhysicsSandboxDraw
), ),
) do |value| ) do |value|
@vessel.pilot.order(priority: 10, group: :acceleration_x) do @vessel.pilot.order(priority: 10, group: :acceleration_x) do
@vessel.pilot.set_exact_acceleration_axis!(0, value) @vessel.pilot.set_exact_acceleration_axis(0, value)
@vessel.pilot.permanent_mode = :constant @vessel.pilot.permanent_mode = Vessel::Pilot::Mode::Constant
@vessel.pilot.low_thrust_pressed = false @vessel.pilot.low_thrust_pressed = false
@vessel.pilot.low_thrust_save = value @vessel.pilot.low_thrust_save = value
end end
@ -75,8 +75,8 @@ module PhysicsSandboxDraw
), ),
) do |value| ) do |value|
@vessel.pilot.order(priority: 10, group: :acceleration_y) do @vessel.pilot.order(priority: 10, group: :acceleration_y) do
@vessel.pilot.set_exact_acceleration_axis!(1, value) @vessel.pilot.set_exact_acceleration_axis(1, value)
@vessel.pilot.permanent_mode = :constant @vessel.pilot.permanent_mode = Vessel::Pilot::Mode::Constant
@vessel.pilot.low_thrust_pressed = false @vessel.pilot.low_thrust_pressed = false
@vessel.pilot.low_thrust_save = value @vessel.pilot.low_thrust_save = value
end end
@ -94,12 +94,12 @@ module PhysicsSandboxDraw
data: @vessel.gravity_body.acceleration[0], data: @vessel.gravity_body.acceleration[0],
) do |value| ) do |value|
@vessel.pilot.order(priority: 10, group: :maneuvers) do @vessel.pilot.order(priority: 10, group: :maneuvers) do
@vessel.pilot.temporary_mode = :pulse @vessel.pilot.temporary_mode = Vessel::Pilot::Mode::Pulse
if !@vessel.pilot.low_thrust_pressed if !@vessel.pilot.low_thrust_pressed
@vessel.pilot.low_thrust_save = @vessel.gravity_body.acceleration[0] @vessel.pilot.low_thrust_save = @vessel.gravity_body.acceleration[0]
end end
@vessel.pilot.low_thrust_pressed = true @vessel.pilot.low_thrust_pressed = true
@vessel.pilot.set_exact_acceleration_axis!(0, value) @vessel.pilot.set_exact_acceleration_axis(0, value)
lock_callback = @ui_events_handler.add(SF::Event::KeyPressed) do |callback, event| lock_callback = @ui_events_handler.add(SF::Event::KeyPressed) do |callback, event|
if event.as(SF::Event::KeyPressed).code == SF::Keyboard::Key::L if event.as(SF::Event::KeyPressed).code == SF::Keyboard::Key::L
@vessel.pilot.low_thrust_save = value @vessel.pilot.low_thrust_save = value
@ -109,7 +109,7 @@ module PhysicsSandboxDraw
@ui_events_handler.remove(SF::Event::MouseButtonReleased, callback) @ui_events_handler.remove(SF::Event::MouseButtonReleased, callback)
@ui_events_handler.remove(SF::Event::MouseButtonReleased, lock_callback) @ui_events_handler.remove(SF::Event::MouseButtonReleased, lock_callback)
@vessel.pilot.low_thrust_pressed = false @vessel.pilot.low_thrust_pressed = false
@vessel.pilot.set_exact_acceleration_axis!(0, @vessel.pilot.low_thrust_save) @vessel.pilot.set_exact_acceleration_axis(0, @vessel.pilot.low_thrust_save)
end end
end end
end end
@ -120,12 +120,12 @@ module PhysicsSandboxDraw
data: @vessel.gravity_body.acceleration[1], data: @vessel.gravity_body.acceleration[1],
) do |value| ) do |value|
@vessel.pilot.order(priority: 10, group: :maneuvers) do @vessel.pilot.order(priority: 10, group: :maneuvers) do
@vessel.pilot.temporary_mode = :pulse @vessel.pilot.temporary_mode = Vessel::Pilot::Mode::Pulse
if !@vessel.pilot.low_thrust_pressed if !@vessel.pilot.low_thrust_pressed
@vessel.pilot.low_thrust_save = @vessel.gravity_body.acceleration[1] @vessel.pilot.low_thrust_save = @vessel.gravity_body.acceleration[1]
end end
@vessel.pilot.low_thrust_pressed = true @vessel.pilot.low_thrust_pressed = true
@vessel.pilot.set_exact_acceleration_axis!(1, value) @vessel.pilot.set_exact_acceleration_axis(1, value)
lock_callback = @ui_events_handler.add(SF::Event::KeyPressed) do |callback, event| lock_callback = @ui_events_handler.add(SF::Event::KeyPressed) do |callback, event|
if event.as(SF::Event::KeyPressed).code == SF::Keyboard::Key::L if event.as(SF::Event::KeyPressed).code == SF::Keyboard::Key::L
@vessel.pilot.low_thrust_save = value @vessel.pilot.low_thrust_save = value
@ -135,7 +135,7 @@ module PhysicsSandboxDraw
@ui_events_handler.remove(SF::Event::MouseButtonReleased, callback) @ui_events_handler.remove(SF::Event::MouseButtonReleased, callback)
@ui_events_handler.remove(SF::Event::MouseButtonReleased, lock_callback) @ui_events_handler.remove(SF::Event::MouseButtonReleased, lock_callback)
@vessel.pilot.low_thrust_pressed = false @vessel.pilot.low_thrust_pressed = false
@vessel.pilot.set_exact_acceleration_axis!(1, @vessel.pilot.low_thrust_save) @vessel.pilot.set_exact_acceleration_axis(1, @vessel.pilot.low_thrust_save)
end end
end end
end end
@ -148,65 +148,94 @@ module PhysicsSandboxDraw
->{ ->{
draw_input( draw_input(
label: "###acceleration_digit_x", label: "###acceleration_digit_x",
data: @vessel.pilot.computer_input_thurst[0], data: @vessel.pilot.computer_input_thrust[0],
) do |value| ) do |value|
@vessel.pilot.order(priority: 9, group: :maneuvers) do @vessel.pilot.order(priority: 9, group: :maneuvers) do
@vessel.pilot.computer_input_thurst[0] = value @vessel.pilot.computer_input_thrust[0] = value
end end
end end
}, },
->{ ->{
draw_input( draw_input(
label: "###acceleration_digit_y", label: "###acceleration_digit_y",
data: @vessel.pilot.computer_input_thurst[1], data: @vessel.pilot.computer_input_thrust[1],
) do |value| ) do |value|
@vessel.pilot.order(priority: 9, group: :maneuvers) do @vessel.pilot.order(priority: 9, group: :maneuvers) do
@vessel.pilot.computer_input_thurst[1] = value @vessel.pilot.computer_input_thrust[1] = value
end end
end end
}, },
"m/s²", "m/s²",
) )
# Manual reset
draw_table_line( draw_table_line(
"", "",
->{ ->{
if ImGui.button("Comfirm") if ImGui.button("Comfirm")
@vessel.pilot.order(priority: 100, group: :maneuvers) do @vessel.pilot.order(priority: 100, group: :maneuvers) do
@vessel.pilot.permanent_mode = :constant @vessel.pilot.permanent_mode = Vessel::Pilot::Mode::Constant
@vessel.pilot.set_exact_acceleration_axis!(@vessel.pilot.computer_input_thurst) @vessel.pilot.set_exact_acceleration_axis(@vessel.pilot.computer_input_thrust)
# @vessel.pilot.set_exact_acceleration_axis!(0, @vessel.pilot.computer_input_thurst[0]) # @vessel.pilot.set_exact_acceleration_axis(0, @vessel.pilot.computer_input_thrust[0])
# @vessel.pilot.set_exact_acceleration_axis!(1, @vessel.pilot.computer_input_thurst[1]) # @vessel.pilot.set_exact_acceleration_axis(1, @vessel.pilot.computer_input_thrust[1])
end end
end end
},
->{
if ImGui.button("Reset") if ImGui.button("Reset")
@vessel.pilot.order(priority: 100, group: :prepare) do @vessel.pilot.order(priority: 100, group: :prepare) do
@vessel.pilot.set_exact_acceleration_axis!(@vessel.pilot.computer_input_thurst) @vessel.pilot.set_exact_acceleration_axis(@vessel.pilot.computer_input_thrust)
# @vessel.pilot.computer_input_thurst[0] = @vessel.gravity_body.acceleration[0] # @vessel.pilot.computer_input_thrust[0] = @vessel.gravity_body.acceleration[0]
# @vessel.pilot.computer_input_thurst[1] = @vessel.gravity_body.acceleration[1] # @vessel.pilot.computer_input_thrust[1] = @vessel.gravity_body.acceleration[1]
end end
end end
}, },
->{ ->{
if ImGui.button("Zero") if ImGui.button("Zero")
@vessel.pilot.order(priority: 10, group: :prepare) do @vessel.pilot.order(priority: 10, group: :prepare) do
@vessel.pilot.computer_input_thurst[0] = 0.0 @vessel.pilot.computer_input_thrust[0] = 0.0
@vessel.pilot.computer_input_thurst[1] = 0.0 @vessel.pilot.computer_input_thrust[1] = 0.0
end end
end end
}, },
)
draw_table_line(
"",
# AntiGrav
->{ ->{
if @vessel.pilot.permanent_mode != :antigrav if @vessel.pilot.permanent_mode != Vessel::Pilot::Mode::AntiGrav
if ImGui.button("Anti-grav") if ImGui.button("Anti-grav")
@vessel.pilot.order(priority: 10, group: :fly_mode) do @vessel.pilot.order(priority: 10, group: :fly_mode) do
@vessel.pilot.permanent_mode = :antigrav @vessel.pilot.permanent_mode = Vessel::Pilot::Mode::AntiGrav
@vessel.gravity_body.acceleration[0] = @vessel.gravity[0] # @vessel.gravity_body.acceleration[0] = @vessel.gravity[0]
@vessel.gravity_body.acceleration[1] = @vessel.gravity[1] # @vessel.gravity_body.acceleration[1] = @vessel.gravity[1]
end end
end end
else else
if ImGui.button("Disable anti-grav") if ImGui.button("Disable anti-grav")
@vessel.pilot.order(priority: 11, group: :fly_mode) do @vessel.pilot.order(priority: 11, group: :fly_mode) do
if @vessel.pilot.permanent_mode == :antigrav if @vessel.pilot.permanent_mode == Vessel::Pilot::Mode::AntiGrav
@vessel.pilot.permanent_mode = nil
end
end
end
end
},
# AntiSpeed
->{
if @vessel.pilot.permanent_mode != Vessel::Pilot::Mode::AntiSpeed
if ImGui.button("Anti-speed")
@vessel.pilot.order(priority: 10, group: :fly_mode) do
@vessel.pilot.permanent_mode = Vessel::Pilot::Mode::AntiSpeed
@vessel.pilot.current_selected_body = @star.gravity_body
# @vessel.antispeed[0] = @vessel.speed[0]
# @vessel.antispeed[1] = @vessel.speed[1]
end
end
else
if ImGui.button("Disable anti-speed")
@vessel.pilot.order(priority: 11, group: :fly_mode) do
if @vessel.pilot.permanent_mode == Vessel::Pilot::Mode::AntiSpeed
@vessel.pilot.permanent_mode = nil @vessel.pilot.permanent_mode = nil
end end
end end