Compare commits

..

4 Commits

4 changed files with 40 additions and 15 deletions

View File

@ -25,7 +25,8 @@ deps_update:
deps_opt: deps_opt:
@[ -d lib/ ] || make deps @[ -d lib/ ] || make deps
doc: doc:
crystal docs ./src/zero_epsilon.cr ./src/tests/physics_sandbox.cr ./lib/crsfml/src/crsfml.cr ./lib/imgui/src/imgui.cr crystal docs ./src/zero_epsilon.cr ./src/tests/physics_sandbox.cr ./lib/crsfml/src/crsfml.cr ./lib/imgui/src/imgui.cr ./lib/imgui-sfml/src/imgui-sfml.cr
clean: clean:
rm $(NAME) rm $(NAME)

View File

@ -11,9 +11,15 @@ module Gravity
EARTH2 = Gravity::Body(2).new(mass: 5972200000000000000000000.0, position: Vector(Float64, 2).zero) EARTH2 = Gravity::Body(2).new(mass: 5972200000000000000000000.0, position: Vector(Float64, 2).zero)
EARTH3 = Gravity::Body(3).new(mass: 5972200000000000000000000.0, position: Vector(Float64, 3).zero) EARTH3 = Gravity::Body(3).new(mass: 5972200000000000000000000.0, position: Vector(Float64, 3).zero)
getter :mass, :position, :g getter :mass, :position, :g, :name
def initialize(@mass : Float64, @position : Vector(Float64, N) = Vector(Float64, N).zero, @g : Float64 = G) @@name_idx = 0
def self.next_name
@@name_idx += 1
"default~#{@@name_idx}"
end
def initialize(@mass : Float64, @position : Vector(Float64, N) = Vector(Float64, N).zero, @g : Float64 = G, @name : String = next_name)
end end
def acceleration(position : Vector(Float64, N)) : Vector(Float64, N) def acceleration(position : Vector(Float64, N)) : Vector(Float64, N)

View File

@ -27,7 +27,7 @@ class Game
@selected_body : Gravity::Body(2) @selected_body : Gravity::Body(2)
SECOND_IN_NANO = 1_000_000_000 SECOND_IN_NANO = 1_000_000_000
DEFAULT_ZOOM = 5/16 DEFAULT_ZOOM = 3/16
def accuracy_frequency_nano_rate def accuracy_frequency_nano_rate
((SECOND_IN_NANO * @timescale) // @framerate).to_u64 ((SECOND_IN_NANO * @timescale) // @framerate).to_u64
@ -93,11 +93,11 @@ class Game
sun_offset = Vector[3*10.0**11, 3*10.0**11] sun_offset = Vector[3*10.0**11, 3*10.0**11]
@bodies = { @bodies = {
vessel: Gravity::MovingBody(2).new(mass: 1.0, g: 0.1, position: Vector[10.0, 10.0]), vessel: Gravity::MovingBody(2).new(name: "vessel", mass: 1.0, g: 0.1, position: Vector[10.0, 10.0]),
star: Gravity::MovingBody(2).new(mass: 300000.0, g: 0.1, position: Vector[1000.0, 1000.0], speed: Vector[0.0, 0.0]), star: Gravity::MovingBody(2).new(name: "star", mass: 300000.0, g: 0.1, position: Vector[1000.0, 1000.0], speed: Vector[0.0, 0.0]),
pla1: Gravity::MovingBody(2).new(mass: 15000.0, g: 0.1, position: Vector[500.0, 500.0], speed: Vector[12.0, 0.0]), pla1: Gravity::MovingBody(2).new(name: "pla1", mass: 15000.0, g: 0.1, position: Vector[500.0, 500.0], speed: Vector[12.0, 0.0]),
pla2: Gravity::MovingBody(2).new(mass: 18000.0, g: 0.1, position: Vector[1000.0, 1500.0], speed: Vector[-6.0, -6.0]), pla2: Gravity::MovingBody(2).new(name: "pla2", mass: 18000.0, g: 0.1, position: Vector[1000.0, 1500.0], speed: Vector[-6.0, -6.0]),
pla3: Gravity::MovingBody(2).new(mass: 12000.0, g: 0.1, position: Vector[1500.0, 1500.0], speed: Vector[-6.0, 0.0]), pla3: Gravity::MovingBody(2).new(name: "pla3", mass: 12000.0, g: 0.1, position: Vector[1500.0, 1500.0], speed: Vector[-6.0, 0.0]),
} }
@gravited = [ @gravited = [
@bodies[:pla1], @bodies[:pla2], @bodies[:pla3], @bodies[:star], @bodies[:pla1], @bodies[:pla2], @bodies[:pla3], @bodies[:star],
@ -141,6 +141,8 @@ class Game
@ui_events_handler = UI::EventHandler.new @ui_events_handler = UI::EventHandler.new
end end
MAX_DISTANCE_TO_SELECT_PROJECTABLE = 15
def handle_events def handle_events
joystick_moved_modified = false joystick_moved_modified = false
@ -154,12 +156,27 @@ class Game
when SF::Event::KeyPressed when SF::Event::KeyPressed
Log.debug "KeyPressed #{event}" Log.debug "KeyPressed #{event}"
when SF::Event::MouseMoved when SF::Event::MouseMoved
when SF::Event::MouseButtonEvent when SF::Event::MouseButtonReleased
click_coord = @window.map_pixel_to_coords({event.x, event.y}, @view) clicked_on_imgui_item = ImGui.is_any_item_hovered || ImGui.is_any_item_active
if !clicked_on_imgui_item
# don't match if click on the buttons of ImGui
# click_coord = @window.map_pixel_to_coords({event.x, event.y}, @view)
# m = (proj.position - Vector[click_coord.x, click_coord.y]).magnitude
# mmhhhh it does not work as expected, bugged # mmhhhh it does not work as expected, bugged
nearest_proj = @projectables.sort_by { |proj| (proj.position - Vector[event.x, event.y]).magnitude }.first nearest_proj = @projectables.map do |projectable|
@selected_body = nearest_proj.gravity_body pixel = @window.map_coords_to_pixel({projectable.position[0], projectable.position[1]}, @view)
Log.debug "MouseButtonEvent #{event}" {
projectable: projectable,
distance: (Vector[pixel.x, pixel.y] - Vector[event.x, event.y]).magnitude,
}
end.sort_by!{ |tuple| tuple[:distance] }.first
if nearest_proj[:distance] <= MAX_DISTANCE_TO_SELECT_PROJECTABLE
@selected_body = nearest_proj[:projectable].gravity_body
pp "Select a new body #{@selected_body.name}"
end
end
when SF::Event::MouseButtonEvent
pp "MouseButtonEvent #{event}"
when SF::Event::JoystickButtonPressed when SF::Event::JoystickButtonPressed
@joystick_moved.zero! # reset 0 @joystick_moved.zero! # reset 0
joystick_moved_modified = :vector # force exact set joystick_moved_modified = :vector # force exact set
@ -296,4 +313,4 @@ class Game
end end
end end
Game.new(framerate: 120, width: 1200, height: 1000).execute_loop Game.new(framerate: 120, width: 800, height: 600).execute_loop

View File

@ -309,6 +309,7 @@ module PhysicsSandboxDraw
draw_table_line "Speed", @vessel.gravity_body.speed[0].round(3).to_s, @vessel.gravity_body.speed[1].round(3).to_s, "m/s" draw_table_line "Speed", @vessel.gravity_body.speed[0].round(3).to_s, @vessel.gravity_body.speed[1].round(3).to_s, "m/s"
draw_table_line "Acceleration", @vessel.gravity_body.acceleration[0].round(3).to_s, @vessel.gravity_body.acceleration[1].round(3).to_s, "m/s²" draw_table_line "Acceleration", @vessel.gravity_body.acceleration[0].round(3).to_s, @vessel.gravity_body.acceleration[1].round(3).to_s, "m/s²"
draw_table_line "Selected body", "#{@selected_body.position[0].round(4)}", "#{@selected_body.position[1].round(4)}", "m" draw_table_line "Selected body", "#{@selected_body.position[0].round(4)}", "#{@selected_body.position[1].round(4)}", "m"
draw_table_line "", @selected_body.name, "", ""
draw_table_line "Relative speed", "#{relative_speed[0].round(4)}", "#{relative_speed[1].round(4)}", "m/s" draw_table_line "Relative speed", "#{relative_speed[0].round(4)}", "#{relative_speed[1].round(4)}", "m/s"
end end
ImGui.text "" ImGui.text ""