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, 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, 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
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
end
# I think it's always square root not 1.0/size
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
def normalize
magnitude = self.magnitude
self / magnitude
self.div!(magnitude)
end
end

View File

@ -139,11 +139,26 @@ class Vessel(N)
end
class Pilot(N) < Station(N)
property low_thrust_save, low_thrust_pressed, computer_input_thurst
property permanent_mode, temporary_mode
# these properties handle manual override of trust (either analogic or exact computed)
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?
@temporary_mode : Symbol?
enum Mode
Constant
Pulse
AntiGrav
AntiSpeed
end
@permanent_mode : Mode?
@temporary_mode : Mode?
def initialize(*p, **o)
super
@ -151,37 +166,53 @@ class Vessel(N)
@temporary_mode = nil
@low_thrust_save = 0.0
@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
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
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|
set_exact_acceleration_axis!(index, value)
set_exact_acceleration_axis(index, value)
end
end
# 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)))
# 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)))
tuples.each do |tuple|
set_exact_acceleration_axis!(*tuple)
set_exact_acceleration_axis(*tuple)
end
end
def immediate_mode
@temporary_mode || @permanent_mode
end
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:
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
@temporary_mode = nil # each time we apply the temporary_mode, reset it
end
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

View File

@ -175,7 +175,7 @@ class Game
end
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
# can double the vector each second
Log.debug "accelerate #{@joystick_moved}"

View File

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