From e3c1371871834bedabec53190a1212725179ea1c Mon Sep 17 00:00:00 2001 From: Atirut Wattanamongkol Date: Sun, 2 Mar 2025 14:37:28 +0700 Subject: [PATCH 1/2] Fix coordinate system --- scripts/classes/col_file.gd | 3 ++- scripts/map_builder.gd | 15 +++++++-------- scripts/map_test.gd | 1 - scripts/model_test.gd | 1 - scripts/renderware/rw_geometry.gd | 4 ++-- scripts/world.gd | 1 - 6 files changed, 11 insertions(+), 14 deletions(-) diff --git a/scripts/classes/col_file.gd b/scripts/classes/col_file.gd index 58a04f9..2ca2102 100644 --- a/scripts/classes/col_file.gd +++ b/scripts/classes/col_file.gd @@ -34,8 +34,8 @@ class TBase: func read_vector3(file: FileAccess) -> Vector3: var result := Vector3() result.x = file.get_float() - result.y = file.get_float() result.z = file.get_float() + result.y = file.get_float() return result class TBounds extends TBase: @@ -88,6 +88,7 @@ class TVertex extends TBase: func _init(file: FileAccess): position = read_vector3(file) + position.z = -position.z class TFace extends TBase: var a: int diff --git a/scripts/map_builder.gd b/scripts/map_builder.gd index ce8ab30..8cc1cc0 100644 --- a/scripts/map_builder.gd +++ b/scripts/map_builder.gd @@ -59,8 +59,8 @@ func _read_ide_line(section: String, tokens: Array[String]): var parent := tokens[0].to_int() var position := Vector3( tokens[1].to_float(), - tokens[2].to_float(), - tokens[3].to_float() ) + tokens[3].to_float(), + -tokens[2].to_float() ) var color := Color( tokens[4].to_float() / 255, tokens[5].to_float() / 255, @@ -86,16 +86,16 @@ func _read_ipl_line(section: String, tokens: Array[String]): placement.model_name = tokens[1].to_lower() placement.position = Vector3( tokens[2].to_float(), - tokens[3].to_float(), - tokens[4].to_float(), ) + tokens[4].to_float(), + -tokens[3].to_float(), ) placement.scale = Vector3( tokens[5].to_float(), - tokens[6].to_float(), - tokens[7].to_float(), ) + tokens[7].to_float(), + tokens[6].to_float(), ) placement.rotation = Quaternion( -tokens[8].to_float(), - -tokens[9].to_float(), -tokens[10].to_float(), + -tokens[9].to_float(), tokens[11].to_float(), ) placements.append(placement) @@ -115,7 +115,6 @@ func _read_map_data(path: String, line_handler: Callable) -> void: func clear_map() -> void: map = Node3D.new() - map.rotation.x = deg_to_rad(-90.0) func spawn_placement(ipl: ItemPlacement) -> Node3D: return spawn(ipl.id, ipl.model_name, ipl.position, ipl.scale, ipl.rotation) diff --git a/scripts/map_test.gd b/scripts/map_test.gd index 2578c6e..9f17294 100644 --- a/scripts/map_test.gd +++ b/scripts/map_test.gd @@ -4,7 +4,6 @@ extends Node var suzanne := preload("res://prefabs/suzanne.tscn") func _ready() -> void: - world.rotation.x = deg_to_rad(-90.0) var start := Time.get_ticks_msec() var target = MapBuilder.placements.size() var count := 0 diff --git a/scripts/model_test.gd b/scripts/model_test.gd index 90d135f..acedeec 100644 --- a/scripts/model_test.gd +++ b/scripts/model_test.gd @@ -9,7 +9,6 @@ func _ready() -> void: spinbox.rounded = true spinbox.max_value = 0 misc = RWTextureDict.new(GameManager.open_file("models/misc.txd", FileAccess.READ)) - meshinstance.rotation.x = deg_to_rad(-90.0) func _ld_dff() -> void: var dialog := FileDialog.new() diff --git a/scripts/renderware/rw_geometry.gd b/scripts/renderware/rw_geometry.gd index a073324..7244fc2 100644 --- a/scripts/renderware/rw_geometry.gd +++ b/scripts/renderware/rw_geometry.gd @@ -112,15 +112,15 @@ func _init(file: FileAccess): for j in vert_count: var vert := Vector3() vert.x = file.get_float() + vert.z = -file.get_float() vert.y = file.get_float() - vert.z = file.get_float() morph_t.vertices.append(vert) if morph_t.has_normals: for j in vert_count: var normal := Vector3() normal.x = file.get_float() - normal.y = file.get_float() normal.z = file.get_float() + normal.y = file.get_float() morph_t.normals.append(normal) morph_targets.append(morph_t) material_list = RWMaterialList.new(file) diff --git a/scripts/world.gd b/scripts/world.gd index 252c6fc..eea0795 100644 --- a/scripts/world.gd +++ b/scripts/world.gd @@ -7,7 +7,6 @@ extends Node var car := preload("res://scenes/car.tscn") func _ready() -> void: - world.rotation.x = deg_to_rad(-90.0) var start := Time.get_ticks_msec() var target = MapBuilder.placements.size() var count := 0 From ad37be19b78c20c966e2ac29c2df42ba211e8904 Mon Sep 17 00:00:00 2001 From: Atirut Wattanamongkol Date: Tue, 4 Mar 2025 14:25:47 +0700 Subject: [PATCH 2/2] Stuff --- scripts/classes/col_file.gd | 12 +++++------ scripts/map_builder.gd | 36 +++++++++++++++++++++++++------ scripts/renderware/rw_geometry.gd | 10 ++++----- 3 files changed, 40 insertions(+), 18 deletions(-) diff --git a/scripts/classes/col_file.gd b/scripts/classes/col_file.gd index 2ca2102..857c5f2 100644 --- a/scripts/classes/col_file.gd +++ b/scripts/classes/col_file.gd @@ -32,11 +32,12 @@ func _init(file: FileAccess): class TBase: func read_vector3(file: FileAccess) -> Vector3: - var result := Vector3() - result.x = file.get_float() - result.z = file.get_float() - result.y = file.get_float() - return result + # GTA3 uses a different coordinate system, so we need to convert + # the coordinates to Godot's coordinate system. + var x := file.get_float() + var y := file.get_float() + var z := file.get_float() + return Vector3(x, z, -y) class TBounds extends TBase: var radius: float @@ -88,7 +89,6 @@ class TVertex extends TBase: func _init(file: FileAccess): position = read_vector3(file) - position.z = -position.z class TFace extends TBase: var a: int diff --git a/scripts/map_builder.gd b/scripts/map_builder.gd index 8cc1cc0..89af055 100644 --- a/scripts/map_builder.gd +++ b/scripts/map_builder.gd @@ -57,6 +57,7 @@ func _read_ide_line(section: String, tokens: Array[String]): items[id] = item "2dfx": var parent := tokens[0].to_int() + # Convert GTA to Godot coordinate system var position := Vector3( tokens[1].to_float(), tokens[3].to_float(), @@ -84,14 +85,17 @@ func _read_ipl_line(section: String, tokens: Array[String]): var placement := ItemPlacement.new() placement.id = tokens[0].to_int() placement.model_name = tokens[1].to_lower() + # Convert GTA to Godot coordinate system placement.position = Vector3( tokens[2].to_float(), tokens[4].to_float(), -tokens[3].to_float(), ) + # Scale conversion follows the same pattern placement.scale = Vector3( tokens[5].to_float(), tokens[7].to_float(), tokens[6].to_float(), ) + # Quaternion conversion requires negating components placement.rotation = Quaternion( -tokens[8].to_float(), -tokens[10].to_float(), @@ -146,13 +150,31 @@ func spawn(id: int, model_name: String, position: Vector3, scale: Vector3, rotat var colshape := CollisionShape3D.new() if collision is ColFile.TBox: var aabb := AABB() - aabb.position = collision.min - aabb.end = collision.max - var shape := BoxShape3D.new() - shape.size = aabb.size - colshape.shape = shape - colshape.position = aabb.get_center() - sb.add_child(colshape) + # Get min and max positions from collision box + var min_pos := collision.min as Vector3 + var max_pos := collision.max as Vector3 + + # Ensure AABB has positive size by sorting min/max for each axis + aabb.position = Vector3( + min(min_pos.x, max_pos.x), + min(min_pos.y, max_pos.y), + min(min_pos.z, max_pos.z) + ) + aabb.end = Vector3( + max(min_pos.x, max_pos.x), + max(min_pos.y, max_pos.y), + max(min_pos.z, max_pos.z) + ) + + # Only create the shape if size is valid + if aabb.size.x > 0 and aabb.size.y > 0 and aabb.size.z > 0: + var shape := BoxShape3D.new() + shape.size = aabb.size + colshape.shape = shape + colshape.position = aabb.get_center() + sb.add_child(colshape) + else: + sb.add_child(colshape) if item.colfile.vertices.size() > 0: var colshape := CollisionShape3D.new() var shape := ConcavePolygonShape3D.new() diff --git a/scripts/renderware/rw_geometry.gd b/scripts/renderware/rw_geometry.gd index 7244fc2..0927de1 100644 --- a/scripts/renderware/rw_geometry.gd +++ b/scripts/renderware/rw_geometry.gd @@ -110,11 +110,11 @@ func _init(file: FileAccess): morph_t.has_normals = file.get_32() != 0 if morph_t.has_vertices: for j in vert_count: - var vert := Vector3() - vert.x = file.get_float() - vert.z = -file.get_float() - vert.y = file.get_float() - morph_t.vertices.append(vert) + # Convert GTA to Godot coordinate system + var x := file.get_float() + var y := file.get_float() + var z := file.get_float() + morph_t.vertices.append(Vector3(x, z, -y)) if morph_t.has_normals: for j in vert_count: var normal := Vector3()