Detect tile under foot

This commit is contained in:
2025-01-02 21:36:24 -06:00
parent 95b13a7f55
commit 4303aafb59
11 changed files with 84 additions and 54 deletions

Binary file not shown.

Binary file not shown.

File diff suppressed because one or more lines are too long

View File

@@ -6,11 +6,10 @@
[ext_resource type="Texture2D" uid="uid://i34vweii6joy" path="res://textures/rosa.png" id="4_qdjyy"] [ext_resource type="Texture2D" uid="uid://i34vweii6joy" path="res://textures/rosa.png" id="4_qdjyy"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_ls7r8"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_ls7r8"]
radius = 9.35148 radius = 0.331611
height = 31.3716 height = 0.804274
[sub_resource type="QuadMesh" id="QuadMesh_dyaax"] [sub_resource type="QuadMesh" id="QuadMesh_dyaax"]
size = Vector2(32, 32)
[sub_resource type="ShaderMaterial" id="ShaderMaterial_fegux"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_fegux"]
render_priority = 0 render_priority = 0
@@ -23,15 +22,15 @@ shader_parameter/npcTexture = ExtResource("4_qdjyy")
script = ExtResource("1_3g383") script = ExtResource("1_3g383")
[node name="Rosa Camera" type="Camera3D" parent="."] [node name="Rosa Camera" type="Camera3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.130388, 0.991463, 0, -0.991463, 0.130388, 0, 30.9255, 0) transform = Transform3D(1, 0, 0, 0, 0.130388, 0.991463, 0, -0.991463, 0.130388, 0, 3.14994, 0.404846)
fov = 30.0 fov = 30.0
script = ExtResource("2_tr66j") script = ExtResource("2_tr66j")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5.93694) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.126485)
shape = SubResource("CapsuleShape3D_ls7r8") shape = SubResource("CapsuleShape3D_ls7r8")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.173648, 0.984808, 0, -0.984808, 0.173648, 0, 0, 0) transform = Transform3D(1, 0, 0, 0, 0.173648, 0.984808, 0, -0.984808, 0.173648, 0.00989294, 0, 0)
mesh = SubResource("QuadMesh_dyaax") mesh = SubResource("QuadMesh_dyaax")
surface_material_override/0 = SubResource("ShaderMaterial_fegux") surface_material_override/0 = SubResource("ShaderMaterial_fegux")

View File

@@ -4,14 +4,13 @@
[ext_resource type="Shader" path="res://shaders/NPC Shader.gdshader" id="1_xgcv1"] [ext_resource type="Shader" path="res://shaders/NPC Shader.gdshader" id="1_xgcv1"]
[ext_resource type="Texture2D" uid="uid://i34vweii6joy" path="res://textures/rosa.png" id="2_shls4"] [ext_resource type="Texture2D" uid="uid://i34vweii6joy" path="res://textures/rosa.png" id="2_shls4"]
[sub_resource type="CapsuleShape3D" id="CapsuleShape3D_32cew"] [sub_resource type="CapsuleShape3D" id="CapsuleShape3D_dw1sy"]
radius = 9.35148 radius = 0.331611
height = 31.3716 height = 0.804274
[sub_resource type="QuadMesh" id="QuadMesh_evcrc"] [sub_resource type="QuadMesh" id="QuadMesh_xhkdv"]
size = Vector2(32, 32)
[sub_resource type="ShaderMaterial" id="ShaderMaterial_mgbpu"] [sub_resource type="ShaderMaterial" id="ShaderMaterial_l4utx"]
render_priority = 0 render_priority = 0
shader = ExtResource("1_xgcv1") shader = ExtResource("1_xgcv1")
shader_parameter/frame = 0 shader_parameter/frame = 0
@@ -22,10 +21,10 @@ shader_parameter/npcTexture = ExtResource("2_shls4")
script = ExtResource("1_61g2d") script = ExtResource("1_61g2d")
[node name="CollisionShape3D" type="CollisionShape3D" parent="."] [node name="CollisionShape3D" type="CollisionShape3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 5.93694) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0.126485)
shape = SubResource("CapsuleShape3D_32cew") shape = SubResource("CapsuleShape3D_dw1sy")
[node name="MeshInstance3D" type="MeshInstance3D" parent="."] [node name="MeshInstance3D" type="MeshInstance3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 0.173648, 0.984808, 0, -0.984808, 0.173648, 0, 0, 0) transform = Transform3D(1, 0, 0, 0, 0.173648, 0.984808, 0, -0.984808, 0.173648, 0.00989294, 0, 0)
mesh = SubResource("QuadMesh_evcrc") mesh = SubResource("QuadMesh_xhkdv")
surface_material_override/0 = SubResource("ShaderMaterial_mgbpu") surface_material_override/0 = SubResource("ShaderMaterial_l4utx")

View File

@@ -23,10 +23,10 @@ sky = SubResource("Sky_weucl")
environment = SubResource("Environment_18twt") environment = SubResource("Environment_18twt")
[node name="Rosa" parent="." instance=ExtResource("1_3u2u0")] [node name="Rosa" parent="." instance=ExtResource("1_3u2u0")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -24.3489, 25.4112, -33.4617) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -0.651853, 0.684572, -0.0593004)
[node name="TestNpc" parent="." instance=ExtResource("2_6f3lj")] [node name="TestNpc" parent="." instance=ExtResource("2_6f3lj")]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 15.919, 28.7961, 27.6146) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, 0.545907, 0.459578, -0.399262)
[node name="OmniLight3D" type="OmniLight3D" parent="."] [node name="OmniLight3D" type="OmniLight3D" parent="."]
transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -38.1432, 54.7591, -25.7675) transform = Transform3D(1, 0, 0, 0, 1, 0, 0, 0, 1, -38.1432, 54.7591, -25.7675)

View File

@@ -7,12 +7,14 @@ enum Direction {
EAST EAST
} }
var speed:float = 3200; var speed:float = 150;
var friction:float = 7; var friction:float = 8.5;
var gravity:float = 150; var gravity:float = 30;
var direction = Direction.SOUTH; var direction = Direction.SOUTH;
var meshInstance:MeshInstance3D; var meshInstance:MeshInstance3D;
var underFootTile:int = -1;
var underFootPosition:Vector3;
func getDirectionVector() -> Vector3: func getDirectionVector() -> Vector3:
match direction: match direction:
@@ -33,9 +35,58 @@ func updateMovement(delta) -> void:
func updateOverworldLogic(delta) -> void: func updateOverworldLogic(delta) -> void:
pass pass
# Private methods
func _updateTileData() -> void:
# ray cast down
var offset = Vector3(0, 0, 0.426);
var query = PhysicsRayQueryParameters3D.create(
position + offset,
position + Vector3(0, -1, 0) + offset
)
query.collide_with_areas = true
query.exclude = [self]
var result = get_world_3d().direct_space_state.intersect_ray(query)
if !result or !result.collider:
return;
var collider = result.collider;
var colliderMesh = collider.get_node("../");
if !colliderMesh or !colliderMesh.mesh or colliderMesh.mesh.get_surface_count() == 0:
return;
# Get the face index (triangle)
var arrays = colliderMesh.mesh.surface_get_arrays(0);
var indiceIdx = result.face_index * 3;
# Get each indice of the triangle
var index0 = arrays[Mesh.ARRAY_INDEX][indiceIdx+0];
var index1 = arrays[Mesh.ARRAY_INDEX][indiceIdx+1];
var index2 = arrays[Mesh.ARRAY_INDEX][indiceIdx+2];
# Get each uv of each indice
var uv0:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index0];
var uv1:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index1];
var uv2:Vector2 = arrays[Mesh.ARRAY_TEX_UV][index2];
# Determine the lowest texture coordinate
var min = Vector2(min(uv0.x, uv1.x, uv2.x), min(uv0.y, uv1.y, uv2.y));
# Convert to column/row
var w = 768;
var h = w;
var tw = 48;
var th = tw;
var column = int(roundf(min.x * w)) / 48;
var row = int(roundf(min.y * h)) / 48;
var columns = 768 / 48;
underFootPosition = result.position;
underFootTile = column % columns + row * columns;
# Events # Events
func _ready() -> void: func _ready() -> void:
meshInstance = get_node("MeshInstance3D") meshInstance = get_node("MeshInstance3D")
_updateTileData();
pass pass
func _process(delta:float) -> void: func _process(delta:float) -> void:
@@ -46,16 +97,17 @@ func _process(delta:float) -> void:
var material:ShaderMaterial = meshInstance.get_surface_override_material(0) var material:ShaderMaterial = meshInstance.get_surface_override_material(0)
material.set_shader_parameter("direction", direction) material.set_shader_parameter("direction", direction)
func _physics_process(delta: float) -> void: func _physics_process(delta: float) -> void:
# Update movement
updateMovement(delta);
# Gravity and friction # Gravity and friction
if !is_on_floor(): if !is_on_floor():
velocity.y -= gravity * delta; velocity.y -= gravity * delta;
else: else:
velocity += -(velocity * friction * delta); velocity += -(velocity * friction * delta);
if velocity.length() != 0:
# Update movement _updateTileData();
updateMovement(delta)
# Update character controller. # Update character controller.
move_and_slide(); move_and_slide();

View File

@@ -1,6 +1,7 @@
extends Camera3D extends Camera3D
var PIXEL_SCALE:float = 4.0; const PIXEL_SCALE:float = 4.0;
const WORLD_UNITS:float = 32.0;
func _ready() -> void: func _ready() -> void:
pass pass
@@ -10,7 +11,7 @@ func _process(delta: float) -> void:
var z:float = ( var z:float = (
tan((deg_to_rad(180) - deg_to_rad(fov)) / 2.0) * tan((deg_to_rad(180) - deg_to_rad(fov)) / 2.0) *
(get_viewport().size.y / 2.0) (get_viewport().size.y / 2.0)
) / PIXEL_SCALE; ) / PIXEL_SCALE / WORLD_UNITS;
var rosa = get_node(".."); var rosa = get_node("..");
var look = rosa.position; var look = rosa.position;

View File

@@ -1,6 +1,6 @@
extends "res://scripts/OverworldEntity.gd" extends "res://scripts/OverworldEntity.gd"
var interactRange = 22; var interactRange = 0.7;
func updateOverworldLogic(delta) -> void: func updateOverworldLogic(delta) -> void:
# Check if interact button is pressed # Check if interact button is pressed

View File

@@ -4,11 +4,10 @@ render_mode cull_disabled;
uniform sampler2D tileset : filter_nearest; uniform sampler2D tileset : filter_nearest;
void vertex() { void vertex() {
// Called for every vertex the material is visible on.
} }
void fragment() { void fragment() {
vec4 textureColor = texture(tileset, UV); vec4 textureColor = texture(tileset, UV) * COLOR;
if(textureColor.a == 0.0) if(textureColor.a == 0.0)
discard; discard;

View File

@@ -7,27 +7,8 @@ uniform int direction;
const int FRAMES = 3; const int FRAMES = 3;
const int DIRECTIONS = 4; const int DIRECTIONS = 4;
//const bool BILLBOARD_ROTATION_LOCK = false;
void vertex() { void vertex() {
//if(BILLBOARD_ROTATION_LOCK) {
//mat4 modified_model_view = VIEW_MATRIX * mat4(
//INV_VIEW_MATRIX[0],
//INV_VIEW_MATRIX[1],
//INV_VIEW_MATRIX[2],
//MODEL_MATRIX[3]
//);
//MODELVIEW_MATRIX = modified_model_view;
//} else {
//mat4 modified_model_view = VIEW_MATRIX * mat4(
//INV_VIEW_MATRIX[0],
//MODEL_MATRIX[1],
//MODEL_MATRIX[2],
//MODEL_MATRIX[3]
//);
//MODELVIEW_MATRIX = modified_model_view;
//}
vec2 tileSize = vec2(1.0 / float(FRAMES), 1.0 / float(DIRECTIONS)); vec2 tileSize = vec2(1.0 / float(FRAMES), 1.0 / float(DIRECTIONS));
vec2 topLeft = tileSize * vec2(float(frame % FRAMES), float(direction % DIRECTIONS)); vec2 topLeft = tileSize * vec2(float(frame % FRAMES), float(direction % DIRECTIONS));
vec2 bottomRight = topLeft + tileSize; vec2 bottomRight = topLeft + tileSize;