Skip to content

Commit fed698d

Browse files
authored
Add a 3D Rigidbody character demo (#675)
1 parent db01a21 commit fed698d

25 files changed

+2695
-0
lines changed

3d/rigidbody_character/README.md

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Kinematic Character 3D
2+
3+
RigidBody character demo for 3D using a capsule for the character.
4+
Cubes as RigidBodies spawn in the map from above to show interaction
5+
with the player (jump on them, gently push them), which would be
6+
impossible with a KinematicBody.
7+
8+
Language: GDScript
9+
10+
Renderer: GLES 3
11+
12+
## How does it work?
13+
14+
This demo uses a [`RigidBody`](https://docs.godotengine.org/en/stable/classes/class_rigidbody.html)
15+
for the player, and [`StaticBody`](https://docs.godotengine.org/en/latest/classes/class_staticbody.html)
16+
for the level. Each has colliders, the player moves itself via
17+
`apply_central_impulse()` in `_physics_process()`, and collides with the level.
18+
19+
## Screenshots
20+
21+
![Screenshot](screenshots/ingame.png)
22+
23+
![Screenshot](screenshots/editor.png)
24+
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[gd_scene load_steps=5 format=2]
2+
3+
[ext_resource path="res://models/white_wood.png" type="Texture" id=1]
4+
5+
[sub_resource type="BoxShape" id=1]
6+
extents = Vector3( 0.5, 0.5, 0.5 )
7+
8+
[sub_resource type="SpatialMaterial" id=3]
9+
resource_name = "Material"
10+
albedo_color = Color( 0.917647, 0.384314, 0.0823529, 1 )
11+
albedo_texture = ExtResource( 1 )
12+
roughness = 0.75
13+
14+
[sub_resource type="CubeMesh" id=2]
15+
material = SubResource( 3 )
16+
size = Vector3( 1, 1, 1 )
17+
18+
[node name="cube_rigidbody" type="RigidBody"]
19+
20+
[node name="CollisionShape" type="CollisionShape" parent="."]
21+
shape = SubResource( 1 )
22+
23+
[node name="MeshInstance" type="MeshInstance" parent="."]
24+
mesh = SubResource( 2 )
25+
material/0 = null

3d/rigidbody_character/cubelib.tres

Lines changed: 27 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
[gd_resource type="Environment" load_steps=2 format=2]
2+
3+
[sub_resource type="ProceduralSky" id=1]
4+
radiance_size = 1
5+
sky_top_color = Color( 0.219882, 0.193725, 0.366471, 1 )
6+
sky_horizon_color = Color( 0.342622, 0.0655002, 0.558935, 1 )
7+
sky_curve = 0.0490365
8+
ground_bottom_color = Color( 0.0342205, 0.0333383, 0.0322154, 1 )
9+
ground_horizon_color = Color( 0.148289, 0.138067, 0.125119, 1 )
10+
ground_curve = 0.25
11+
sun_latitude = 55.0
12+
sun_longitude = -80.0
13+
texture_size = 0
14+
15+
[resource]
16+
background_mode = 2
17+
background_sky = SubResource( 1 )
18+
tonemap_mode = 2
19+
tonemap_white = 6.0
20+
ssao_blur = 1
21+
glow_levels/7 = true
22+
glow_strength = 0.79
23+
glow_bloom = 1.0
24+
glow_blend_mode = 0
25+
glow_bicubic_upscale = true
576 KB
Binary file not shown.

3d/rigidbody_character/icon.png

17.1 KB
Loading
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
[remap]
2+
3+
importer="texture"
4+
type="StreamTexture"
5+
path="res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex"
6+
metadata={
7+
"vram_texture": false
8+
}
9+
10+
[deps]
11+
12+
source_file="res://icon.png"
13+
dest_files=[ "res://.import/icon.png-487276ed1e3a0c39cad0279d744ee560.stex" ]
14+
15+
[params]
16+
17+
compress/mode=0
18+
compress/lossy_quality=0.7
19+
compress/hdr_mode=0
20+
compress/bptc_ldr=0
21+
compress/normal_map=0
22+
flags/repeat=0
23+
flags/filter=true
24+
flags/mipmaps=false
25+
flags/anisotropic=false
26+
flags/srgb=2
27+
process/fix_alpha_border=true
28+
process/premult_alpha=false
29+
process/HDR_as_SRGB=false
30+
process/invert_color=false
31+
process/normal_map_invert_y=false
32+
stream=false
33+
size_limit=0
34+
detect_3d=true
35+
svg/scale=1.0

3d/rigidbody_character/level.gd

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
extends Spatial
2+
3+
# Random spawn of Rigidbody cubes.
4+
func _on_SpawnTimer_timeout():
5+
var new_rb = preload("res://cube_rigidbody.tscn").instance()
6+
new_rb.translation.y = 15
7+
new_rb.translation.x = rand_range(-5, 5)
8+
new_rb.translation.z = rand_range(-5, 5)
9+
add_child(new_rb)

3d/rigidbody_character/level.tscn

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
[gd_scene load_steps=11 format=2]
2+
3+
[ext_resource path="res://cubelib.tres" type="MeshLibrary" id=1]
4+
[ext_resource path="res://player/cubio.tscn" type="PackedScene" id=2]
5+
[ext_resource path="res://models/cube.mesh" type="ArrayMesh" id=3]
6+
[ext_resource path="res://models/mushroom.glb" type="PackedScene" id=5]
7+
[ext_resource path="res://level.gd" type="Script" id=6]
8+
[ext_resource path="res://cube_rigidbody.tscn" type="PackedScene" id=7]
9+
10+
[sub_resource type="BoxShape" id=1]
11+
margin = 0.001
12+
extents = Vector3( 0.5, 0.5, 0.5 )
13+
14+
[sub_resource type="Animation" id=2]
15+
length = 10.0
16+
loop = true
17+
tracks/0/type = "value"
18+
tracks/0/path = NodePath(".:translation")
19+
tracks/0/interp = 1
20+
tracks/0/loop_wrap = true
21+
tracks/0/imported = false
22+
tracks/0/enabled = true
23+
tracks/0/keys = {
24+
"times": PoolRealArray( 0, 1, 4, 6, 9, 10 ),
25+
"transitions": PoolRealArray( 1, -2, 1, -2, 1, 1 ),
26+
"update": 0,
27+
"values": [ Vector3( 0.5, 4.5, -2.5 ), Vector3( 0.5, 4.5, -2.5 ), Vector3( 0.5, 8.5, -2.5 ), Vector3( 0.5, 8.5, -2.5 ), Vector3( 0.5, 4.5, -2.5 ), Vector3( 0.5, 4.5, -2.5 ) ]
28+
}
29+
30+
[sub_resource type="Animation" id=3]
31+
length = 10.0
32+
loop = true
33+
tracks/0/type = "value"
34+
tracks/0/path = NodePath(".:translation")
35+
tracks/0/interp = 1
36+
tracks/0/loop_wrap = true
37+
tracks/0/imported = false
38+
tracks/0/enabled = true
39+
tracks/0/keys = {
40+
"times": PoolRealArray( 0, 2, 4.5, 6, 9 ),
41+
"transitions": PoolRealArray( 1, -2, 1, -2, 1 ),
42+
"update": 0,
43+
"values": [ Vector3( -3.5, 8.5, 4.5 ), Vector3( -3.5, 8.5, 4.5 ), Vector3( 3.5, 8.5, 4.5 ), Vector3( 3.5, 8.5, 4.5 ), Vector3( -3.5, 8.5, 4.5 ) ]
44+
}
45+
46+
[sub_resource type="BoxShape" id=4]
47+
48+
[node name="World" type="Spatial"]
49+
script = ExtResource( 6 )
50+
__meta__ = {
51+
"__editor_plugin_screen__": "3D"
52+
}
53+
54+
[node name="GridMap" type="GridMap" parent="."]
55+
mesh_library = ExtResource( 1 )
56+
cell_size = Vector3( 1, 1, 1 )
57+
data = {
58+
"cells": PoolIntArray( 0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 65530, 0, 0, 65531, 0, 0, 65532, 0, 0, 65533, 0, 0, 65534, 0, 0, 65535, 0, 0, 196603, 0, 0, 196604, 0, 0, 524292, 0, 0, 589820, 0, 0, 786432, 0, 0, 851967, 0, 0, 0, 1, 0, 1, 1, 0, 2, 1, 0, 3, 1, 0, 4, 1, 0, 65530, 1, 0, 65531, 1, 0, 65532, 1, 0, 65533, 1, 0, 65534, 1, 0, 65535, 1, 0, 131075, 1, 0, 196603, 1, 0, 196604, 1, 0, 524292, 1, 0, 589820, 1, 0, 786432, 1, 0, 851967, 1, 0, 0, 2, 0, 1, 2, 0, 2, 2, 0, 3, 2, 0, 4, 2, 0, 65530, 2, 0, 65531, 2, 0, 65532, 2, 0, 65533, 2, 0, 65534, 2, 0, 65535, 2, 0, 131075, 2, 0, 196603, 2, 0, 196604, 2, 0, 524292, 2, 0, 589820, 2, 0, 786432, 2, 0, 786433, 2, 0, 851966, 2, 0, 851967, 2, 0, 0, 3, 0, 1, 3, 0, 2, 3, 0, 3, 3, 0, 4, 3, 0, 65530, 3, 0, 65531, 3, 0, 65532, 3, 0, 65533, 3, 0, 65534, 3, 0, 65535, 3, 0, 196603, 3, 0, 524291, 3, 0, 524292, 3, 0, 589820, 3, 0, 786432, 3, 0, 786433, 3, 0, 851966, 3, 0, 851967, 3, 0, 0, 4, 0, 1, 4, 0, 2, 4, 0, 3, 4, 0, 4, 4, 0, 65530, 4, 0, 65531, 4, 0, 65532, 4, 0, 65533, 4, 0, 65534, 4, 0, 65535, 4, 0, 196603, 4, 0, 0, 5, 0, 1, 5, 0, 2, 5, 0, 3, 5, 0, 4, 5, 0, 65530, 5, 0, 65531, 5, 0, 65532, 5, 0, 65533, 5, 0, 65534, 5, 0, 65535, 5, 0, 131075, 5, 0, 0, 6, 0, 1, 6, 0, 2, 6, 0, 3, 6, 0, 4, 6, 0, 65530, 6, 0, 65531, 6, 0, 65532, 6, 0, 65533, 6, 0, 65534, 6, 0, 65535, 6, 0, 131075, 6, 0, 196603, 6, 0, 0, 7, 0, 1, 7, 0, 2, 7, 0, 3, 7, 0, 4, 7, 0, 65530, 7, 0, 65531, 7, 0, 65532, 7, 0, 65533, 7, 0, 65534, 7, 0, 65535, 7, 0, 131075, 7, 0, 196603, 7, 0, 0, 8, 0, 1, 8, 0, 2, 8, 0, 3, 8, 0, 4, 8, 0, 65530, 8, 0, 65531, 8, 0, 65532, 8, 0, 65533, 8, 0, 65534, 8, 0, 65535, 8, 0, 131075, 8, 0, 196603, 8, 0, 196604, 8, 0, 0, 9, 0, 1, 9, 0, 2, 9, 0, 3, 9, 0, 4, 9, 0, 65530, 9, 0, 65531, 9, 0, 65532, 9, 0, 65533, 9, 0, 65534, 9, 0, 65535, 9, 0, 131073, 9, 0, 131074, 9, 0, 131075, 9, 0, 196603, 9, 0, 196604, 9, 0, 196605, 9, 0, 196608, 9, 0, 262142, 9, 0, 0, 10, 0, 1, 10, 0, 2, 10, 0, 3, 10, 0, 4, 10, 0, 65530, 10, 0, 65531, 10, 0, 65532, 10, 0, 65533, 10, 0, 65534, 10, 0, 65535, 10, 0, 0, 11, 0, 1, 11, 0, 2, 11, 0, 3, 11, 0, 4, 11, 0, 65530, 11, 0, 65531, 11, 0, 65532, 11, 0, 65533, 11, 0, 65534, 11, 0, 65535, 11, 0, 0, 65532, 0, 1, 65532, 0, 2, 65532, 0, 3, 65532, 0, 4, 65532, 0, 65530, 65532, 0, 65531, 65532, 0, 65532, 65532, 0, 65533, 65532, 0, 65534, 65532, 0, 65535, 65532, 0, 0, 65533, 0, 1, 65533, 0, 2, 65533, 0, 3, 65533, 0, 4, 65533, 0, 65530, 65533, 0, 65531, 65533, 0, 65532, 65533, 0, 65533, 65533, 0, 65534, 65533, 0, 65535, 65533, 0, 262145, 65533, 0, 262146, 65533, 0, 262147, 65533, 0, 589822, 65533, 0, 589823, 65533, 0, 655363, 65533, 0, 655364, 65533, 0, 720897, 65533, 0, 720898, 65533, 0, 786432, 65533, 0, 851967, 65533, 0, 0, 65534, 0, 1, 65534, 0, 2, 65534, 0, 3, 65534, 0, 4, 65534, 0, 65530, 65534, 0, 65531, 65534, 0, 65532, 65534, 0, 65533, 65534, 0, 65534, 65534, 0, 65535, 65534, 0, 65536, 65534, 0, 131071, 65534, 0, 196603, 65534, 0, 196604, 65534, 0, 196605, 65534, 0, 196606, 65534, 0, 196607, 65534, 0, 589822, 65534, 0, 589828, 65534, 0, 786432, 65534, 0, 851967, 65534, 0, 0, 65535, 0, 1, 65535, 0, 2, 65535, 0, 3, 65535, 0, 4, 65535, 0, 65530, 65535, 0, 65531, 65535, 0, 65532, 65535, 0, 65533, 65535, 0, 65534, 65535, 0, 65535, 65535, 0, 196603, 65535, 0, 196604, 65535, 0, 196611, 65535, 0, 589820, 65535, 0, 589821, 65535, 0, 589822, 65535, 0, 589828, 65535, 0, 786432, 65535, 0, 851967, 65535, 0 )
59+
}
60+
__meta__ = {
61+
"_editor_clip_": 0,
62+
"_editor_floor_": Vector3( 0, 12, 0 )
63+
}
64+
65+
[node name="DirectionalLight" type="DirectionalLight" parent="."]
66+
transform = Transform( -0.173649, 0.806707, -0.564863, 0, 0.573576, 0.819152, 0.984808, 0.142244, -0.0996007, 0, 0, 0 )
67+
light_energy = 1.3
68+
shadow_enabled = true
69+
shadow_bias = -0.02
70+
shadow_reverse_cull_face = true
71+
directional_shadow_mode = 0
72+
directional_shadow_normal_bias = 0.0
73+
directional_shadow_bias_split_scale = 0.0
74+
directional_shadow_max_distance = 20.0
75+
76+
[node name="Cubio" parent="." instance=ExtResource( 2 )]
77+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -1.5, 2, 4 )
78+
79+
[node name="Elevator1" type="KinematicBody" parent="."]
80+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.5, 4.5, -2.5 )
81+
input_capture_on_drag = true
82+
83+
[node name="Mesh" type="MeshInstance" parent="Elevator1"]
84+
mesh = ExtResource( 3 )
85+
material/0 = null
86+
87+
[node name="CollisionShape" type="CollisionShape" parent="Elevator1"]
88+
shape = SubResource( 1 )
89+
90+
[node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator1"]
91+
autoplay = "updown1"
92+
playback_process_mode = 0
93+
anims/updown1 = SubResource( 2 )
94+
95+
[node name="Elevator2" type="KinematicBody" parent="."]
96+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, -3.5, 8.5, 4.5 )
97+
98+
[node name="Mesh" type="MeshInstance" parent="Elevator2"]
99+
mesh = ExtResource( 3 )
100+
material/0 = null
101+
102+
[node name="CollisionShape" type="CollisionShape" parent="Elevator2"]
103+
shape = SubResource( 1 )
104+
105+
[node name="AnimationPlayer" type="AnimationPlayer" parent="Elevator2"]
106+
autoplay = "side"
107+
playback_process_mode = 0
108+
anims/side = SubResource( 3 )
109+
anims/updown1 = SubResource( 2 )
110+
111+
[node name="Princess" type="Area" parent="."]
112+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 13.25, 3 )
113+
114+
[node name="Mushroom" parent="Princess" instance=ExtResource( 5 )]
115+
116+
[node name="CollisionShape" type="CollisionShape" parent="Princess"]
117+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 1, 0 )
118+
shape = SubResource( 4 )
119+
120+
[node name="SpawnTimer" type="Timer" parent="."]
121+
wait_time = 2.0
122+
autostart = true
123+
124+
[node name="cube_rigidbody" parent="." instance=ExtResource( 7 )]
125+
transform = Transform( 1, 0, 0, 0, 1, 0, 0, 0, 1, 0.416964, 3.3565, 2.6332 )
126+
127+
[connection signal="body_entered" from="Princess" to="Cubio" method="_on_tcube_body_entered"]
128+
[connection signal="timeout" from="SpawnTimer" to="." method="_on_SpawnTimer_timeout"]
3.29 KB
Binary file not shown.

0 commit comments

Comments
 (0)