ruby_rpg 0.0.4 → 0.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +21 -21
- data/bin/build.bash +13 -13
- data/bin/import +44 -44
- data/glfw-3.3.9.bin.MACOS/LICENSE.md +23 -23
- data/glfw-3.3.9.bin.MACOS/README.md +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/build_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/build_guide.html +199 -199
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compat_guide.html +153 -153
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/compile_guide.html +223 -223
- data/glfw-3.3.9.bin.MACOS/docs/html/context_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/context_guide.html +259 -259
- data/glfw-3.3.9.bin.MACOS/docs/html/deprecated.html +88 -88
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_08423ce7729c4554356a7de7171ba263.html +85 -85
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_2dcebcdf14b42e92a6ed85c61bda6b9d.html +93 -93
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_62ce056722ee97c199075aa1d6eca309.html +95 -95
- data/glfw-3.3.9.bin.MACOS/docs/html/dir_dddf4cf62655095a666cf715de3a31a2.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/doc.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/docd.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.css +1685 -1685
- data/glfw-3.3.9.bin.MACOS/docs/html/doxygen.svg +28 -28
- data/glfw-3.3.9.bin.MACOS/docs/html/dynsections.js +192 -192
- data/glfw-3.3.9.bin.MACOS/docs/html/extra.css +1 -1
- data/glfw-3.3.9.bin.MACOS/docs/html/files.html +91 -91
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosed.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderclosedd.svg +11 -11
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopen.svg +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/folderopend.svg +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h.html +1661 -1661
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3_8h_source.html +1175 -1175
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h.html +167 -167
- data/glfw-3.3.9.bin.MACOS/docs/html/glfw3native_8h_source.html +303 -303
- data/glfw-3.3.9.bin.MACOS/docs/html/group__buttons.html +282 -282
- data/glfw-3.3.9.bin.MACOS/docs/html/group__context.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__errors.html +304 -304
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__axes.html +202 -202
- data/glfw-3.3.9.bin.MACOS/docs/html/group__gamepad__buttons.html +410 -410
- data/glfw-3.3.9.bin.MACOS/docs/html/group__hat__state.html +234 -234
- data/glfw-3.3.9.bin.MACOS/docs/html/group__init.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/group__input.html +2255 -2255
- data/glfw-3.3.9.bin.MACOS/docs/html/group__joysticks.html +362 -362
- data/glfw-3.3.9.bin.MACOS/docs/html/group__keys.html +2034 -2034
- data/glfw-3.3.9.bin.MACOS/docs/html/group__mods.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__monitor.html +848 -848
- data/glfw-3.3.9.bin.MACOS/docs/html/group__native.html +812 -812
- data/glfw-3.3.9.bin.MACOS/docs/html/group__shapes.html +198 -198
- data/glfw-3.3.9.bin.MACOS/docs/html/group__vulkan.html +359 -359
- data/glfw-3.3.9.bin.MACOS/docs/html/group__window.html +3434 -3434
- data/glfw-3.3.9.bin.MACOS/docs/html/index.html +100 -100
- data/glfw-3.3.9.bin.MACOS/docs/html/input_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/input_guide.html +570 -570
- data/glfw-3.3.9.bin.MACOS/docs/html/internal_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/internals_guide.html +132 -132
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/intro_guide.html +351 -351
- data/glfw-3.3.9.bin.MACOS/docs/html/jquery.js +33 -33
- data/glfw-3.3.9.bin.MACOS/docs/html/main_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/menu.js +136 -136
- data/glfw-3.3.9.bin.MACOS/docs/html/menudata.js +29 -29
- data/glfw-3.3.9.bin.MACOS/docs/html/minus.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/minusd.svg +8 -8
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/monitor_guide.html +229 -229
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/moving_guide.html +374 -374
- data/glfw-3.3.9.bin.MACOS/docs/html/news.html +634 -634
- data/glfw-3.3.9.bin.MACOS/docs/html/news_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/pages.html +99 -99
- data/glfw-3.3.9.bin.MACOS/docs/html/plus.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/plusd.svg +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/quick_guide.html +397 -397
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_0.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_10.js +65 -65
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_11.js +26 -26
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_12.js +44 -44
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_13.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_14.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_15.js +52 -52
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_16.js +69 -69
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_17.js +51 -51
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_18.js +14 -14
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_19.js +36 -36
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1a.js +98 -98
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_1b.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_3.js +9 -9
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_4.js +57 -57
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_5.js +22 -22
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_6.js +103 -103
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_7.js +23 -23
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_8.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_9.js +63 -63
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_a.js +505 -505
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_b.js +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_c.js +50 -50
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_d.js +17 -17
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_e.js +12 -12
- data/glfw-3.3.9.bin.MACOS/docs/html/search/all_f.js +35 -35
- data/glfw-3.3.9.bin.MACOS/docs/html/search/classes_0.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/close.svg +18 -18
- data/glfw-3.3.9.bin.MACOS/docs/html/search/defines_0.js +34 -34
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_3.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/files_8.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/functions_0.js +146 -146
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_0.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_2.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_3.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_5.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_7.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_8.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_9.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_a.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_b.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_d.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_e.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/groups_f.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_d.svg +24 -24
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_sel.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/mag_seld.svg +31 -31
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_1.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_10.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_4.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_5.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_7.js +7 -7
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_8.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_9.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_a.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_b.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_c.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_d.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_e.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/pages_f.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.css +291 -291
- data/glfw-3.3.9.bin.MACOS/docs/html/search/search.js +840 -840
- data/glfw-3.3.9.bin.MACOS/docs/html/search/searchdata.js +39 -39
- data/glfw-3.3.9.bin.MACOS/docs/html/search/typedefs_0.js +32 -32
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_0.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_1.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_2.js +5 -5
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_3.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_4.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_5.js +6 -6
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_6.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/search/variables_7.js +4 -4
- data/glfw-3.3.9.bin.MACOS/docs/html/spaces.svg +877 -877
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgamepadstate.html +134 -134
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wgammaramp.html +170 -170
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wimage.html +151 -151
- data/glfw-3.3.9.bin.MACOS/docs/html/struct_g_l_f_wvidmode.html +204 -204
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/vulkan_guide.html +196 -196
- data/glfw-3.3.9.bin.MACOS/docs/html/window_8dox.html +81 -81
- data/glfw-3.3.9.bin.MACOS/docs/html/window_guide.html +763 -763
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3.h +5932 -5932
- data/glfw-3.3.9.bin.MACOS/include/GLFW/glfw3native.h +634 -634
- data/glfw-3.3.9.bin.MACOS/lib-arm64/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-universal/libglfw.3.dylib +0 -0
- data/glfw-3.3.9.bin.MACOS/lib-x86_64/libglfw.3.dylib +0 -0
- data/lib/engine/autoloader.rb +11 -11
- data/lib/engine/camera.rb +12 -12
- data/lib/engine/component.rb +63 -63
- data/lib/engine/components/audio_source.rb +41 -41
- data/lib/engine/components/direction_light.rb +22 -22
- data/lib/engine/components/font_renderer.rb +134 -134
- data/lib/engine/components/mesh_renderer.rb +31 -31
- data/lib/engine/components/orthographic_camera.rb +54 -54
- data/lib/engine/components/perspective_camera.rb +53 -53
- data/lib/engine/components/point_light.rb +23 -23
- data/lib/engine/components/sprite_renderer.rb +141 -141
- data/lib/engine/components/ui_font_renderer.rb +140 -140
- data/lib/engine/components/ui_sprite_clickbox.rb +62 -0
- data/lib/engine/components/ui_sprite_renderer.rb +101 -101
- data/lib/engine/compute_shader.rb +93 -0
- data/lib/engine/cursor.rb +43 -43
- data/lib/engine/debugging.rb +41 -41
- data/lib/engine/engine.rb +131 -131
- data/lib/engine/font.rb +74 -74
- data/lib/engine/game_object.rb +234 -234
- data/lib/engine/importers/font_importer.rb +69 -69
- data/lib/engine/importers/obj_file.rb +244 -244
- data/lib/engine/importers/obj_importer.rb +33 -33
- data/lib/engine/input.rb +105 -104
- data/lib/engine/material.rb +79 -79
- data/lib/engine/mesh.rb +43 -43
- data/lib/engine/path.rb +92 -92
- data/lib/engine/physics/collision.rb +12 -12
- data/lib/engine/physics/components/cube_collider.rb +6 -6
- data/lib/engine/physics/components/rigidbody.rb +103 -103
- data/lib/engine/physics/components/sphere_collider.rb +93 -93
- data/lib/engine/physics/physics_resolver.rb +37 -37
- data/lib/engine/polygon_mesh.rb +45 -45
- data/lib/engine/quaternion.rb +234 -234
- data/lib/engine/rendering/instance_renderer.rb +153 -153
- data/lib/engine/rendering/render_pipeline.rb +35 -35
- data/lib/engine/screenshoter.rb +34 -34
- data/lib/engine/shader.rb +119 -91
- data/lib/engine/shaders/colour_frag.glsl +8 -8
- data/lib/engine/shaders/colour_vertex.glsl +13 -13
- data/lib/engine/shaders/mesh_frag.glsl +102 -102
- data/lib/engine/shaders/mesh_vertex.glsl +25 -25
- data/lib/engine/shaders/skybox_frag.glsl +12 -12
- data/lib/engine/shaders/skybox_vert.glsl +14 -14
- data/lib/engine/shaders/sprite_frag.glsl +17 -17
- data/lib/engine/shaders/sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/text_frag.glsl +15 -15
- data/lib/engine/shaders/text_vertex.glsl +31 -31
- data/lib/engine/shaders/ui_sprite_frag.glsl +15 -15
- data/lib/engine/shaders/ui_sprite_vertex.glsl +15 -15
- data/lib/engine/shaders/vertex_lit_frag.glsl +89 -89
- data/lib/engine/shaders/vertex_lit_vertex.glsl +28 -28
- data/lib/engine/tangent_calculator.rb +42 -42
- data/lib/engine/texture.rb +53 -53
- data/lib/engine/video_mode.rb +37 -37
- data/lib/engine/window.rb +126 -126
- data/lib/ruby_rpg.rb +58 -56
- metadata +5 -6
@@ -1,103 +1,103 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine::Physics::Components
|
4
|
-
class Rigidbody < Engine::Component
|
5
|
-
attr_accessor :velocity,
|
6
|
-
:angular_velocity,
|
7
|
-
:force,
|
8
|
-
:impulses,
|
9
|
-
:mass,
|
10
|
-
:coefficient_of_restitution,
|
11
|
-
:coefficient_of_friction
|
12
|
-
|
13
|
-
def initialize(
|
14
|
-
velocity: Vector[0, 0, 0],
|
15
|
-
angular_velocity: Vector[0, 0, 0],
|
16
|
-
mass: 1,
|
17
|
-
inertia_tensor: nil,
|
18
|
-
gravity: Vector[0, -9.81, 0],
|
19
|
-
coefficient_of_restitution: 1,
|
20
|
-
coefficient_of_friction: 0
|
21
|
-
)
|
22
|
-
@velocity = velocity
|
23
|
-
@angular_velocity = angular_velocity
|
24
|
-
@force = gravity * mass
|
25
|
-
@gravity = gravity
|
26
|
-
@impulses = []
|
27
|
-
@angular_impulses = []
|
28
|
-
@mass = mass
|
29
|
-
@inertia_tensor = inertia_tensor || Matrix[
|
30
|
-
[1, 0, 0],
|
31
|
-
[0, 1, 0],
|
32
|
-
[0, 0, 1]
|
33
|
-
] * mass
|
34
|
-
@coefficient_of_restitution = coefficient_of_restitution
|
35
|
-
@coefficient_of_friction = coefficient_of_friction
|
36
|
-
end
|
37
|
-
|
38
|
-
def update(delta_time)
|
39
|
-
@velocity += acceleration * delta_time
|
40
|
-
@velocity += @impulses.reduce(Vector[0, 0, 0]) { |acc, impulse| acc + impulse } / @mass
|
41
|
-
@impulses = []
|
42
|
-
@game_object.pos += @velocity * delta_time
|
43
|
-
|
44
|
-
total_angular_impulse = @angular_impulses.reduce(Vector[0, 0, 0]) { |acc, impulse| acc + impulse }
|
45
|
-
if total_angular_impulse.magnitude > 0
|
46
|
-
angular_inertia =
|
47
|
-
if moment_of_inertia == 0
|
48
|
-
impulse_direction = total_angular_impulse.normalize
|
49
|
-
impulse_direction.dot(@inertia_tensor * impulse_direction)
|
50
|
-
else
|
51
|
-
moment_of_inertia
|
52
|
-
end
|
53
|
-
@angular_velocity += total_angular_impulse / angular_inertia
|
54
|
-
end
|
55
|
-
@angular_impulses = []
|
56
|
-
@game_object.rotate_around(angular_velocity, delta_time * angular_velocity.magnitude) if angular_velocity.magnitude > 0
|
57
|
-
end
|
58
|
-
|
59
|
-
def apply_impulse(impulse, point)
|
60
|
-
@impulses << impulse
|
61
|
-
@angular_impulses << (game_object.pos - point).cross(impulse)
|
62
|
-
end
|
63
|
-
|
64
|
-
def apply_angular_impulse(impulse)
|
65
|
-
@angular_impulses << impulse
|
66
|
-
end
|
67
|
-
|
68
|
-
def colliders
|
69
|
-
@game_object.components.select { |c| c.is_a?(SphereCollider) }
|
70
|
-
end
|
71
|
-
|
72
|
-
def kinetic_energy
|
73
|
-
0.5 * @mass * @velocity.magnitude ** 2 +
|
74
|
-
0.5 * moment_of_inertia * @angular_velocity.magnitude ** 2
|
75
|
-
end
|
76
|
-
|
77
|
-
def momentum
|
78
|
-
@mass * @velocity
|
79
|
-
end
|
80
|
-
|
81
|
-
def angular_momentum(origin = Vector[0, 0, 0])
|
82
|
-
moment_of_inertia * @angular_velocity +
|
83
|
-
(game_object.pos - origin).cross(momentum)
|
84
|
-
end
|
85
|
-
|
86
|
-
def velocity_at_point(point)
|
87
|
-
velocity + (angular_velocity.cross(game_object.pos - point) * Math::PI / 180)
|
88
|
-
end
|
89
|
-
|
90
|
-
private
|
91
|
-
|
92
|
-
def acceleration
|
93
|
-
@force / @mass
|
94
|
-
end
|
95
|
-
|
96
|
-
def moment_of_inertia
|
97
|
-
return 0 if @angular_velocity.magnitude == 0
|
98
|
-
|
99
|
-
angular_velocity_direction = @angular_velocity.normalize
|
100
|
-
angular_velocity_direction.dot(@inertia_tensor * angular_velocity_direction)
|
101
|
-
end
|
102
|
-
end
|
103
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine::Physics::Components
|
4
|
+
class Rigidbody < Engine::Component
|
5
|
+
attr_accessor :velocity,
|
6
|
+
:angular_velocity,
|
7
|
+
:force,
|
8
|
+
:impulses,
|
9
|
+
:mass,
|
10
|
+
:coefficient_of_restitution,
|
11
|
+
:coefficient_of_friction
|
12
|
+
|
13
|
+
def initialize(
|
14
|
+
velocity: Vector[0, 0, 0],
|
15
|
+
angular_velocity: Vector[0, 0, 0],
|
16
|
+
mass: 1,
|
17
|
+
inertia_tensor: nil,
|
18
|
+
gravity: Vector[0, -9.81, 0],
|
19
|
+
coefficient_of_restitution: 1,
|
20
|
+
coefficient_of_friction: 0
|
21
|
+
)
|
22
|
+
@velocity = velocity
|
23
|
+
@angular_velocity = angular_velocity
|
24
|
+
@force = gravity * mass
|
25
|
+
@gravity = gravity
|
26
|
+
@impulses = []
|
27
|
+
@angular_impulses = []
|
28
|
+
@mass = mass
|
29
|
+
@inertia_tensor = inertia_tensor || Matrix[
|
30
|
+
[1, 0, 0],
|
31
|
+
[0, 1, 0],
|
32
|
+
[0, 0, 1]
|
33
|
+
] * mass
|
34
|
+
@coefficient_of_restitution = coefficient_of_restitution
|
35
|
+
@coefficient_of_friction = coefficient_of_friction
|
36
|
+
end
|
37
|
+
|
38
|
+
def update(delta_time)
|
39
|
+
@velocity += acceleration * delta_time
|
40
|
+
@velocity += @impulses.reduce(Vector[0, 0, 0]) { |acc, impulse| acc + impulse } / @mass
|
41
|
+
@impulses = []
|
42
|
+
@game_object.pos += @velocity * delta_time
|
43
|
+
|
44
|
+
total_angular_impulse = @angular_impulses.reduce(Vector[0, 0, 0]) { |acc, impulse| acc + impulse }
|
45
|
+
if total_angular_impulse.magnitude > 0
|
46
|
+
angular_inertia =
|
47
|
+
if moment_of_inertia == 0
|
48
|
+
impulse_direction = total_angular_impulse.normalize
|
49
|
+
impulse_direction.dot(@inertia_tensor * impulse_direction)
|
50
|
+
else
|
51
|
+
moment_of_inertia
|
52
|
+
end
|
53
|
+
@angular_velocity += total_angular_impulse / angular_inertia
|
54
|
+
end
|
55
|
+
@angular_impulses = []
|
56
|
+
@game_object.rotate_around(angular_velocity, delta_time * angular_velocity.magnitude) if angular_velocity.magnitude > 0
|
57
|
+
end
|
58
|
+
|
59
|
+
def apply_impulse(impulse, point)
|
60
|
+
@impulses << impulse
|
61
|
+
@angular_impulses << (game_object.pos - point).cross(impulse)
|
62
|
+
end
|
63
|
+
|
64
|
+
def apply_angular_impulse(impulse)
|
65
|
+
@angular_impulses << impulse
|
66
|
+
end
|
67
|
+
|
68
|
+
def colliders
|
69
|
+
@game_object.components.select { |c| c.is_a?(SphereCollider) }
|
70
|
+
end
|
71
|
+
|
72
|
+
def kinetic_energy
|
73
|
+
0.5 * @mass * @velocity.magnitude ** 2 +
|
74
|
+
0.5 * moment_of_inertia * @angular_velocity.magnitude ** 2
|
75
|
+
end
|
76
|
+
|
77
|
+
def momentum
|
78
|
+
@mass * @velocity
|
79
|
+
end
|
80
|
+
|
81
|
+
def angular_momentum(origin = Vector[0, 0, 0])
|
82
|
+
moment_of_inertia * @angular_velocity +
|
83
|
+
(game_object.pos - origin).cross(momentum)
|
84
|
+
end
|
85
|
+
|
86
|
+
def velocity_at_point(point)
|
87
|
+
velocity + (angular_velocity.cross(game_object.pos - point) * Math::PI / 180)
|
88
|
+
end
|
89
|
+
|
90
|
+
private
|
91
|
+
|
92
|
+
def acceleration
|
93
|
+
@force / @mass
|
94
|
+
end
|
95
|
+
|
96
|
+
def moment_of_inertia
|
97
|
+
return 0 if @angular_velocity.magnitude == 0
|
98
|
+
|
99
|
+
angular_velocity_direction = @angular_velocity.normalize
|
100
|
+
angular_velocity_direction.dot(@inertia_tensor * angular_velocity_direction)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -1,93 +1,93 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine::Physics::Components
|
4
|
-
class SphereCollider < Engine::Component
|
5
|
-
attr_accessor :radius
|
6
|
-
|
7
|
-
def initialize(radius)
|
8
|
-
@radius = radius
|
9
|
-
end
|
10
|
-
|
11
|
-
def collision_for(other_collider)
|
12
|
-
distance = (game_object.pos - other_collider.game_object.pos).magnitude
|
13
|
-
min_distance = radius + other_collider.radius
|
14
|
-
|
15
|
-
return nil if distance >= min_distance
|
16
|
-
|
17
|
-
direction = (other_collider.game_object.pos - game_object.pos).normalize
|
18
|
-
collision_point = collision_point(direction, min_distance - distance)
|
19
|
-
magnitude = normal_impulse_magnitude(direction, other_collider, collision_point)
|
20
|
-
|
21
|
-
return nil if magnitude <= 0
|
22
|
-
|
23
|
-
normal_impulse = -direction * magnitude
|
24
|
-
tangential_impulse = tangential_impulse(other_collider, normal_impulse, collision_point)
|
25
|
-
|
26
|
-
puts "normal_impulse: #{normal_impulse}"
|
27
|
-
puts "tangential_impulse: #{tangential_impulse}"
|
28
|
-
puts "total impulse: #{normal_impulse + tangential_impulse}"
|
29
|
-
|
30
|
-
Engine::Physics::Collision.new(normal_impulse + tangential_impulse, collision_point)
|
31
|
-
end
|
32
|
-
|
33
|
-
def static?
|
34
|
-
rigidbody.nil?
|
35
|
-
end
|
36
|
-
|
37
|
-
def inverse_mass
|
38
|
-
1.0 / rigidbody.mass
|
39
|
-
end
|
40
|
-
|
41
|
-
def velocity(pos)
|
42
|
-
rigidbody&.velocity_at_point(pos) || Vector[0, 0, 0]
|
43
|
-
end
|
44
|
-
|
45
|
-
def rigidbody
|
46
|
-
game_object.components.find { |c| c.is_a?(Rigidbody) }
|
47
|
-
end
|
48
|
-
|
49
|
-
private
|
50
|
-
|
51
|
-
def combined_coefficient_of_restitution(other_collider)
|
52
|
-
return rigidbody.coefficient_of_restitution if other_collider.static?
|
53
|
-
|
54
|
-
rigidbody.coefficient_of_restitution * other_collider.rigidbody.coefficient_of_restitution
|
55
|
-
end
|
56
|
-
|
57
|
-
def combined_coefficient_of_friction(other_collider)
|
58
|
-
return rigidbody.coefficient_of_friction if other_collider.static?
|
59
|
-
|
60
|
-
rigidbody.coefficient_of_friction * other_collider.rigidbody.coefficient_of_friction
|
61
|
-
end
|
62
|
-
|
63
|
-
def collision_point(direction, overlap)
|
64
|
-
game_object.pos + direction * (radius - overlap / 2)
|
65
|
-
end
|
66
|
-
|
67
|
-
def normal_impulse_magnitude(direction, other_collider, collision_point)
|
68
|
-
(
|
69
|
-
if other_collider.static?
|
70
|
-
v_r = velocity(collision_point)
|
71
|
-
(v_r.dot(direction) * 2) / inverse_mass
|
72
|
-
else
|
73
|
-
v_r = velocity(collision_point) - other_collider.velocity(collision_point)
|
74
|
-
(v_r.dot(direction) * 2) / (inverse_mass + other_collider.inverse_mass)
|
75
|
-
end) * combined_coefficient_of_restitution(other_collider)
|
76
|
-
end
|
77
|
-
|
78
|
-
def tangential_impulse(other_collider, normal_impulse, collision_point)
|
79
|
-
normal_direction = normal_impulse.normalize
|
80
|
-
v_t = if other_collider.static?
|
81
|
-
velocity(collision_point)
|
82
|
-
else
|
83
|
-
velocity(collision_point) - other_collider.velocity(collision_point)
|
84
|
-
end
|
85
|
-
|
86
|
-
v_t_without_normal = -((v_t - v_t.dot(normal_direction) * normal_direction))
|
87
|
-
return Vector[0, 0, 0] if v_t_without_normal.magnitude == 0
|
88
|
-
|
89
|
-
relevant_speed = [v_t_without_normal.magnitude, normal_impulse.magnitude * combined_coefficient_of_friction(other_collider)].min
|
90
|
-
v_t_without_normal.normalize * relevant_speed / 2
|
91
|
-
end
|
92
|
-
end
|
93
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine::Physics::Components
|
4
|
+
class SphereCollider < Engine::Component
|
5
|
+
attr_accessor :radius
|
6
|
+
|
7
|
+
def initialize(radius)
|
8
|
+
@radius = radius
|
9
|
+
end
|
10
|
+
|
11
|
+
def collision_for(other_collider)
|
12
|
+
distance = (game_object.pos - other_collider.game_object.pos).magnitude
|
13
|
+
min_distance = radius + other_collider.radius
|
14
|
+
|
15
|
+
return nil if distance >= min_distance
|
16
|
+
|
17
|
+
direction = (other_collider.game_object.pos - game_object.pos).normalize
|
18
|
+
collision_point = collision_point(direction, min_distance - distance)
|
19
|
+
magnitude = normal_impulse_magnitude(direction, other_collider, collision_point)
|
20
|
+
|
21
|
+
return nil if magnitude <= 0
|
22
|
+
|
23
|
+
normal_impulse = -direction * magnitude
|
24
|
+
tangential_impulse = tangential_impulse(other_collider, normal_impulse, collision_point)
|
25
|
+
|
26
|
+
puts "normal_impulse: #{normal_impulse}"
|
27
|
+
puts "tangential_impulse: #{tangential_impulse}"
|
28
|
+
puts "total impulse: #{normal_impulse + tangential_impulse}"
|
29
|
+
|
30
|
+
Engine::Physics::Collision.new(normal_impulse + tangential_impulse, collision_point)
|
31
|
+
end
|
32
|
+
|
33
|
+
def static?
|
34
|
+
rigidbody.nil?
|
35
|
+
end
|
36
|
+
|
37
|
+
def inverse_mass
|
38
|
+
1.0 / rigidbody.mass
|
39
|
+
end
|
40
|
+
|
41
|
+
def velocity(pos)
|
42
|
+
rigidbody&.velocity_at_point(pos) || Vector[0, 0, 0]
|
43
|
+
end
|
44
|
+
|
45
|
+
def rigidbody
|
46
|
+
game_object.components.find { |c| c.is_a?(Rigidbody) }
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
|
51
|
+
def combined_coefficient_of_restitution(other_collider)
|
52
|
+
return rigidbody.coefficient_of_restitution if other_collider.static?
|
53
|
+
|
54
|
+
rigidbody.coefficient_of_restitution * other_collider.rigidbody.coefficient_of_restitution
|
55
|
+
end
|
56
|
+
|
57
|
+
def combined_coefficient_of_friction(other_collider)
|
58
|
+
return rigidbody.coefficient_of_friction if other_collider.static?
|
59
|
+
|
60
|
+
rigidbody.coefficient_of_friction * other_collider.rigidbody.coefficient_of_friction
|
61
|
+
end
|
62
|
+
|
63
|
+
def collision_point(direction, overlap)
|
64
|
+
game_object.pos + direction * (radius - overlap / 2)
|
65
|
+
end
|
66
|
+
|
67
|
+
def normal_impulse_magnitude(direction, other_collider, collision_point)
|
68
|
+
(
|
69
|
+
if other_collider.static?
|
70
|
+
v_r = velocity(collision_point)
|
71
|
+
(v_r.dot(direction) * 2) / inverse_mass
|
72
|
+
else
|
73
|
+
v_r = velocity(collision_point) - other_collider.velocity(collision_point)
|
74
|
+
(v_r.dot(direction) * 2) / (inverse_mass + other_collider.inverse_mass)
|
75
|
+
end) * combined_coefficient_of_restitution(other_collider)
|
76
|
+
end
|
77
|
+
|
78
|
+
def tangential_impulse(other_collider, normal_impulse, collision_point)
|
79
|
+
normal_direction = normal_impulse.normalize
|
80
|
+
v_t = if other_collider.static?
|
81
|
+
velocity(collision_point)
|
82
|
+
else
|
83
|
+
velocity(collision_point) - other_collider.velocity(collision_point)
|
84
|
+
end
|
85
|
+
|
86
|
+
v_t_without_normal = -((v_t - v_t.dot(normal_direction) * normal_direction))
|
87
|
+
return Vector[0, 0, 0] if v_t_without_normal.magnitude == 0
|
88
|
+
|
89
|
+
relevant_speed = [v_t_without_normal.magnitude, normal_impulse.magnitude * combined_coefficient_of_friction(other_collider)].min
|
90
|
+
v_t_without_normal.normalize * relevant_speed / 2
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
@@ -1,37 +1,37 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine::Physics
|
4
|
-
module PhysicsResolver
|
5
|
-
def self.resolve
|
6
|
-
rigidbodies.each do |rb|
|
7
|
-
apply_collisions(rb)
|
8
|
-
end
|
9
|
-
end
|
10
|
-
|
11
|
-
private
|
12
|
-
|
13
|
-
def self.apply_collisions(rigidbody)
|
14
|
-
other_colliders = colliders.reject { |c| rigidbody.colliders.include?(c) }
|
15
|
-
|
16
|
-
rigidbody.colliders.map do |collider|
|
17
|
-
other_colliders.map do |other_collider|
|
18
|
-
collider.collision_for(other_collider)
|
19
|
-
end.compact
|
20
|
-
end.flatten.each do |collision|
|
21
|
-
rigidbody.apply_impulse(collision.impulse, collision.point)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def self.rigidbodies
|
26
|
-
Engine::GameObject.objects.map do |go|
|
27
|
-
go.components.find { |c| c.is_a?(Engine::Physics::Components::Rigidbody) }
|
28
|
-
end.compact
|
29
|
-
end
|
30
|
-
|
31
|
-
def self.colliders
|
32
|
-
Engine::GameObject.objects.map do |go|
|
33
|
-
go.components.select { |c| c.is_a?(Components::SphereCollider) }
|
34
|
-
end.flatten
|
35
|
-
end
|
36
|
-
end
|
37
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine::Physics
|
4
|
+
module PhysicsResolver
|
5
|
+
def self.resolve
|
6
|
+
rigidbodies.each do |rb|
|
7
|
+
apply_collisions(rb)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
private
|
12
|
+
|
13
|
+
def self.apply_collisions(rigidbody)
|
14
|
+
other_colliders = colliders.reject { |c| rigidbody.colliders.include?(c) }
|
15
|
+
|
16
|
+
rigidbody.colliders.map do |collider|
|
17
|
+
other_colliders.map do |other_collider|
|
18
|
+
collider.collision_for(other_collider)
|
19
|
+
end.compact
|
20
|
+
end.flatten.each do |collision|
|
21
|
+
rigidbody.apply_impulse(collision.impulse, collision.point)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.rigidbodies
|
26
|
+
Engine::GameObject.objects.map do |go|
|
27
|
+
go.components.find { |c| c.is_a?(Engine::Physics::Components::Rigidbody) }
|
28
|
+
end.compact
|
29
|
+
end
|
30
|
+
|
31
|
+
def self.colliders
|
32
|
+
Engine::GameObject.objects.map do |go|
|
33
|
+
go.components.select { |c| c.is_a?(Components::SphereCollider) }
|
34
|
+
end.flatten
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
data/lib/engine/polygon_mesh.rb
CHANGED
@@ -1,45 +1,45 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module Engine
|
4
|
-
class PolygonMesh
|
5
|
-
attr_reader :points, :uvs
|
6
|
-
|
7
|
-
def initialize(points, uvs)
|
8
|
-
@points = points
|
9
|
-
@uvs = uvs
|
10
|
-
end
|
11
|
-
|
12
|
-
def vertex_data
|
13
|
-
@vertex_data ||= generate_vertex_data
|
14
|
-
end
|
15
|
-
|
16
|
-
def index_data
|
17
|
-
@index_data ||= (0..vertices.length - 1).to_a
|
18
|
-
end
|
19
|
-
|
20
|
-
private
|
21
|
-
|
22
|
-
def generate_vertex_data
|
23
|
-
vertices.map do |point|
|
24
|
-
uv = uvs[points.index(point)]
|
25
|
-
[point[0], point[1], 0.0, uv[0], uv[1]]
|
26
|
-
end.flatten
|
27
|
-
end
|
28
|
-
|
29
|
-
def vertices
|
30
|
-
@vertices ||=
|
31
|
-
begin
|
32
|
-
path = Path.new(@points)
|
33
|
-
vertices = []
|
34
|
-
until path.length == 3
|
35
|
-
ear, new_path = path.find_ear
|
36
|
-
ear = ear.reverse!
|
37
|
-
vertices << ear
|
38
|
-
path = new_path
|
39
|
-
end
|
40
|
-
vertices += path.points.reverse
|
41
|
-
vertices.flatten(1)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
45
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Engine
|
4
|
+
class PolygonMesh
|
5
|
+
attr_reader :points, :uvs
|
6
|
+
|
7
|
+
def initialize(points, uvs)
|
8
|
+
@points = points
|
9
|
+
@uvs = uvs
|
10
|
+
end
|
11
|
+
|
12
|
+
def vertex_data
|
13
|
+
@vertex_data ||= generate_vertex_data
|
14
|
+
end
|
15
|
+
|
16
|
+
def index_data
|
17
|
+
@index_data ||= (0..vertices.length - 1).to_a
|
18
|
+
end
|
19
|
+
|
20
|
+
private
|
21
|
+
|
22
|
+
def generate_vertex_data
|
23
|
+
vertices.map do |point|
|
24
|
+
uv = uvs[points.index(point)]
|
25
|
+
[point[0], point[1], 0.0, uv[0], uv[1]]
|
26
|
+
end.flatten
|
27
|
+
end
|
28
|
+
|
29
|
+
def vertices
|
30
|
+
@vertices ||=
|
31
|
+
begin
|
32
|
+
path = Path.new(@points)
|
33
|
+
vertices = []
|
34
|
+
until path.length == 3
|
35
|
+
ear, new_path = path.find_ear
|
36
|
+
ear = ear.reverse!
|
37
|
+
vertices << ear
|
38
|
+
path = new_path
|
39
|
+
end
|
40
|
+
vertices += path.points.reverse
|
41
|
+
vertices.flatten(1)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|