├── KinematicBody.gd ├── README.md ├── default.png ├── default_env.tres ├── ground.tscn ├── icon.png ├── main.tscn └── project.godot /KinematicBody.gd: -------------------------------------------------------------------------------- 1 | extends KinematicBody 2 | 3 | var direction = Vector3() 4 | var velocity = Vector3() 5 | 6 | var gravity = -27 7 | var jump_height = 10 8 | var walk_speed = 10 9 | 10 | var fpv_camera_angle = 0 11 | var fpv_mouse_sensitivity = 0.3 12 | # == phys 13 | var phys_area_object 14 | onready var phys_area = $head/Area 15 | onready var phys_area_aim = $head/Area/aim 16 | 17 | 18 | func _ready(): 19 | Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) 20 | $player_mesh.visible = false 21 | OS.set_window_position(Vector2(0,0)) 22 | 23 | # == control 24 | func _input(event): 25 | if Input.is_action_just_pressed("ui_cancel"): 26 | get_tree().quit() 27 | if event is InputEventMouseMotion: 28 | rotate_y(deg2rad(-event.relative.x * fpv_mouse_sensitivity)) 29 | var change = -event.relative.y * fpv_mouse_sensitivity 30 | if change + fpv_camera_angle < 90 and change + fpv_camera_angle > -90: 31 | $head.rotate_x(deg2rad(change)) 32 | fpv_camera_angle += change 33 | 34 | # == phys 35 | 36 | if Input.is_action_just_pressed("left_click"): 37 | for body in phys_area.get_overlapping_bodies(): 38 | if body is RigidBody: 39 | phys_area_object = body 40 | return 41 | if Input.is_action_just_released("left_click"): 42 | phys_area_object = null 43 | if Input.is_action_just_pressed("right_click") and phys_area_object != null and weakref(phys_area_object).get_ref(): 44 | var a = phys_area_aim.get_global_transform().origin 45 | var b = phys_area_object.get_global_transform().origin 46 | 47 | phys_area_object.set_linear_velocity((b-a)*10) 48 | phys_area_object = null 49 | 50 | # == phys 51 | 52 | func _physics_process(delta): 53 | if phys_area_object != null and weakref(phys_area_object).get_ref(): 54 | var a = phys_area.get_global_transform().origin 55 | var b = phys_area_object.get_global_transform().origin 56 | phys_area_object.set_linear_velocity((a-b)*10) 57 | if phys_area_object.get("timer") != null: 58 | phys_area_object.timer = 0 59 | 60 | # == move 61 | 62 | func _process(delta): 63 | direction = Vector3() 64 | var aim = $head/Camera.get_global_transform().basis 65 | if Input.is_key_pressed(KEY_W): 66 | direction -= aim.z 67 | if Input.is_key_pressed(KEY_S): 68 | direction += aim.z 69 | if Input.is_key_pressed(KEY_A): 70 | direction -= aim.x 71 | if Input.is_key_pressed(KEY_D): 72 | direction += aim.x 73 | direction = direction.normalized() 74 | velocity.y += gravity * delta 75 | var tv = velocity 76 | tv = velocity.linear_interpolate(direction * walk_speed,6 * delta) 77 | velocity.x = tv.x 78 | velocity.z = tv.z 79 | velocity = move_and_slide(velocity,Vector3(0,1,0)) 80 | # == jumping 81 | if is_on_floor() and Input.is_key_pressed(KEY_SPACE): 82 | velocity.y = jump_height -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # physics object picking up *demo* made in godot 3.1 2 | License: LGPL-2.1 3 | 4 | |Action|Controls| 5 | |-|-| 6 | |look around|Mouse| 7 | |Walk|W,S,A,D| 8 | |Jump|Space| 9 | |Exit|Esc| 10 | |pick up|left click| 11 | |throw|right click| 12 | -------------------------------------------------------------------------------- /default.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AiTechEye/physics-object-picking-up/50af5e0b16abd19bfc9a6ca11187278f66fcd5f3/default.png -------------------------------------------------------------------------------- /default_env.tres: -------------------------------------------------------------------------------- 1 | [gd_resource type="Environment" load_steps=2 format=2] 2 | 3 | [sub_resource type="ProceduralSky" id=1] 4 | 5 | [resource] 6 | background_mode = 2 7 | background_sky = SubResource( 1 ) 8 | -------------------------------------------------------------------------------- /ground.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=4 format=2] 2 | 3 | [sub_resource type="CubeMesh" id=1] 4 | 5 | [sub_resource type="SpatialMaterial" id=2] 6 | albedo_color = Color( 0.231373, 0.764706, 0.254902, 1 ) 7 | 8 | [sub_resource type="BoxShape" id=3] 9 | 10 | [node name="Spatial" type="Spatial"] 11 | 12 | [node name="MeshInstance" type="MeshInstance" parent="."] 13 | transform = Transform( 4.92945, 0, 0, 0, 1.02, 0, 0, 0, 4.90199, 0, -1, 0 ) 14 | mesh = SubResource( 1 ) 15 | material/0 = SubResource( 2 ) 16 | 17 | [node name="StaticBody" type="StaticBody" parent="MeshInstance"] 18 | transform = Transform( 0.202862, 0, 0, 0, 3.33333, 0, 0, 0, 0.203999, 0, 0, 0 ) 19 | 20 | [node name="CollisionShape" type="CollisionShape" parent="MeshInstance/StaticBody"] 21 | transform = Transform( 4.92945, 0, 0, 0, 0.3, 0, 0, 0, 4.90199, 0, 0, 0 ) 22 | shape = SubResource( 3 ) 23 | -------------------------------------------------------------------------------- /icon.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/AiTechEye/physics-object-picking-up/50af5e0b16abd19bfc9a6ca11187278f66fcd5f3/icon.png -------------------------------------------------------------------------------- /main.tscn: -------------------------------------------------------------------------------- 1 | [gd_scene load_steps=16 format=2] 2 | 3 | [ext_resource path="res://default.png" type="Texture" id=1] 4 | [ext_resource path="res://KinematicBody.gd" type="Script" id=2] 5 | 6 | [sub_resource type="SpatialMaterial" id=1] 7 | albedo_texture = ExtResource( 1 ) 8 | uv1_scale = Vector3( 10, 1, 10 ) 9 | uv1_triplanar = true 10 | 11 | [sub_resource type="CubeMesh" id=2] 12 | 13 | [sub_resource type="BoxShape" id=3] 14 | 15 | [sub_resource type="CubeMesh" id=4] 16 | 17 | [sub_resource type="SpatialMaterial" id=5] 18 | albedo_color = Color( 1, 0, 0, 1 ) 19 | 20 | [sub_resource type="CapsuleShape" id=6] 21 | 22 | [sub_resource type="BoxShape" id=7] 23 | 24 | [sub_resource type="SphereShape" id=8] 25 | 26 | [sub_resource type="SphereMesh" id=9] 27 | 28 | [sub_resource type="SpatialMaterial" id=10] 29 | albedo_color = Color( 0, 0.670588, 0.0117647, 1 ) 30 | 31 | [sub_resource type="BoxShape" id=11] 32 | 33 | [sub_resource type="CubeMesh" id=12] 34 | 35 | [sub_resource type="SpatialMaterial" id=13] 36 | albedo_color = Color( 0, 0.0392157, 1, 1 ) 37 | 38 | [node name="Spatial" type="Spatial"] 39 | 40 | [node name="Ground" type="MeshInstance" parent="."] 41 | editor/display_folded = true 42 | transform = Transform( 20.282, 0, 0, 0, 3.00821, 0, 0, 0, 19.2005, 0, -3.90285, -3.40485 ) 43 | material_override = SubResource( 1 ) 44 | mesh = SubResource( 2 ) 45 | material/0 = null 46 | 47 | [node name="StaticBody" type="StaticBody" parent="Ground"] 48 | 49 | [node name="CollisionShape" type="CollisionShape" parent="Ground/StaticBody"] 50 | shape = SubResource( 3 ) 51 | 52 | [node name="box1" type="MeshInstance" parent="."] 53 | editor/display_folded = true 54 | transform = Transform( 6.12884, 0, 0, 0, 3.00821, 0, 0, 0, 6.826, -8.45122, -1.0583, -13.336 ) 55 | material_override = SubResource( 1 ) 56 | mesh = SubResource( 2 ) 57 | material/0 = null 58 | 59 | [node name="StaticBody" type="StaticBody" parent="box1"] 60 | 61 | [node name="CollisionShape" type="CollisionShape" parent="box1/StaticBody"] 62 | shape = SubResource( 3 ) 63 | 64 | [node name="box2" type="MeshInstance" parent="."] 65 | editor/display_folded = true 66 | transform = Transform( 3.72134, 0, 0, 0, 1.8561, 0, 0, 0, 2.94374, 3.52478, -1.08219, -12.5602 ) 67 | material_override = SubResource( 1 ) 68 | mesh = SubResource( 2 ) 69 | material/0 = null 70 | 71 | [node name="StaticBody" type="StaticBody" parent="box2"] 72 | 73 | [node name="CollisionShape" type="CollisionShape" parent="box2/StaticBody"] 74 | shape = SubResource( 3 ) 75 | 76 | [node name="player" type="Spatial" parent="."] 77 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -8.65268, 0 ) 78 | 79 | [node name="KinematicBody" type="KinematicBody" parent="player"] 80 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 8.07249, 1.84711 ) 81 | script = ExtResource( 2 ) 82 | 83 | [node name="player_mesh" type="MeshInstance" parent="player/KinematicBody"] 84 | transform = Transform( 0.404405, 0, 0, 0, 0.893384, 0, 0, 0, 0.372423, 0, 1.54014, 0 ) 85 | mesh = SubResource( 4 ) 86 | material/0 = SubResource( 5 ) 87 | 88 | [node name="CollisionShape" type="CollisionShape" parent="player/KinematicBody"] 89 | transform = Transform( 0.636057, 0, 0, 0, -4.37114e-008, -1, 0, 0.605457, -2.64654e-008, 0, 1.54517, 0 ) 90 | shape = SubResource( 6 ) 91 | 92 | [node name="head" type="Spatial" parent="player/KinematicBody"] 93 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 2.66905, 0 ) 94 | 95 | [node name="Camera" type="Camera" parent="player/KinematicBody/head"] 96 | 97 | [node name="Area" type="Area" parent="player/KinematicBody/head"] 98 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, -0.241424, -2 ) 99 | 100 | [node name="CollisionShape" type="CollisionShape" parent="player/KinematicBody/head/Area"] 101 | shape = SubResource( 7 ) 102 | 103 | [node name="aim" type="Spatial" parent="player/KinematicBody/head/Area"] 104 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 3 ) 105 | 106 | [node name="ball" type="RigidBody" parent="."] 107 | editor/display_folded = true 108 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -4.02767, 0, -2.0248 ) 109 | 110 | [node name="CollisionShape" type="CollisionShape" parent="ball"] 111 | shape = SubResource( 8 ) 112 | 113 | [node name="MeshInstance" type="MeshInstance" parent="ball"] 114 | mesh = SubResource( 9 ) 115 | material/0 = SubResource( 10 ) 116 | 117 | [node name="ball2" type="RigidBody" parent="."] 118 | editor/display_folded = true 119 | transform = Transform( 0.63416, 0, 0, 0, 0.63416, 0, 0, 0, 0.63416, -0.7296, 0, -2.0248 ) 120 | 121 | [node name="CollisionShape" type="CollisionShape" parent="ball2"] 122 | shape = SubResource( 8 ) 123 | 124 | [node name="MeshInstance" type="MeshInstance" parent="ball2"] 125 | mesh = SubResource( 9 ) 126 | material/0 = SubResource( 10 ) 127 | 128 | [node name="cube" type="RigidBody" parent="."] 129 | editor/display_folded = true 130 | transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 5.75055, 1, -0.45817 ) 131 | 132 | [node name="CollisionShape" type="CollisionShape" parent="cube"] 133 | shape = SubResource( 11 ) 134 | 135 | [node name="MeshInstance" type="MeshInstance" parent="cube"] 136 | mesh = SubResource( 12 ) 137 | material/0 = SubResource( 13 ) 138 | 139 | [node name="cube2" type="RigidBody" parent="."] 140 | editor/display_folded = true 141 | transform = Transform( 0.408973, 0, 0, 0, 0.408973, 0, 0, 0, 0.408973, 2.49498, 1, -3.60651 ) 142 | 143 | [node name="CollisionShape" type="CollisionShape" parent="cube2"] 144 | shape = SubResource( 11 ) 145 | 146 | [node name="MeshInstance" type="MeshInstance" parent="cube2"] 147 | mesh = SubResource( 12 ) 148 | material/0 = SubResource( 13 ) 149 | 150 | [node name="cube3" type="RigidBody" parent="."] 151 | editor/display_folded = true 152 | transform = Transform( 0.408973, 0, 0, 0, 0.408973, 0, 0, 0, 0.408973, -2.15019, 2.52685, -7.3701 ) 153 | 154 | [node name="CollisionShape" type="CollisionShape" parent="cube3"] 155 | transform = Transform( 4.82578, 0, 0, 0, 0.393227, 0, 0, 0, 0.512657, 0, 0, 0 ) 156 | shape = SubResource( 11 ) 157 | 158 | [node name="MeshInstance" type="MeshInstance" parent="cube3"] 159 | transform = Transform( 4.82578, 0, 0, 0, 0.393227, 0, 0, 0, 0.512657, 0, 0, 0 ) 160 | mesh = SubResource( 12 ) 161 | material/0 = SubResource( 13 ) 162 | -------------------------------------------------------------------------------- /project.godot: -------------------------------------------------------------------------------- 1 | ; Engine configuration file. 2 | ; It's best edited using the editor UI and not directly, 3 | ; since the parameters that go here are not all obvious. 4 | ; 5 | ; Format: 6 | ; [section] ; section goes between [] 7 | ; param=value ; assign values to parameters 8 | 9 | config_version=4 10 | 11 | _global_script_classes=[ ] 12 | _global_script_class_icons={ 13 | 14 | } 15 | 16 | [application] 17 | 18 | config/name="physics object picking up" 19 | run/main_scene="res://main.tscn" 20 | config/icon="res://icon.png" 21 | 22 | [input] 23 | 24 | ui_accept={ 25 | "deadzone": 0.5, 26 | "events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777221,"unicode":0,"echo":false,"script":null) 27 | ] 28 | } 29 | left_click={ 30 | "deadzone": 0.5, 31 | "events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":1,"pressed":false,"doubleclick":false,"script":null) 32 | ] 33 | } 34 | right_click={ 35 | "deadzone": 0.5, 36 | "events": [ Object(InputEventMouseButton,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"button_mask":0,"position":Vector2( 0, 0 ),"global_position":Vector2( 0, 0 ),"factor":1.0,"button_index":2,"pressed":false,"doubleclick":false,"script":null) 37 | ] 38 | } 39 | 40 | [rendering] 41 | 42 | environment/default_environment="res://default_env.tres" 43 | --------------------------------------------------------------------------------