Merge pull request #26 from FOSS-Supremacy/coords-fix

Fix coordinates system
This commit is contained in:
Atirut Wattanamongkol 2025-03-04 17:26:19 +07:00 committed by GitHub
commit f34f6df3ec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 48 additions and 29 deletions

View file

@ -32,11 +32,12 @@ func _init(file: FileAccess):
class TBase: class TBase:
func read_vector3(file: FileAccess) -> Vector3: func read_vector3(file: FileAccess) -> Vector3:
var result := Vector3() # GTA3 uses a different coordinate system, so we need to convert
result.x = file.get_float() # the coordinates to Godot's coordinate system.
result.y = file.get_float() var x := file.get_float()
result.z = file.get_float() var y := file.get_float()
return result var z := file.get_float()
return Vector3(x, z, -y)
class TBounds extends TBase: class TBounds extends TBase:
var radius: float var radius: float

View file

@ -57,10 +57,11 @@ func _read_ide_line(section: String, tokens: Array[String]):
items[id] = item items[id] = item
"2dfx": "2dfx":
var parent := tokens[0].to_int() var parent := tokens[0].to_int()
# Convert GTA to Godot coordinate system
var position := Vector3( var position := Vector3(
tokens[1].to_float(), tokens[1].to_float(),
tokens[2].to_float(), tokens[3].to_float(),
tokens[3].to_float() ) -tokens[2].to_float() )
var color := Color( var color := Color(
tokens[4].to_float() / 255, tokens[4].to_float() / 255,
tokens[5].to_float() / 255, tokens[5].to_float() / 255,
@ -84,18 +85,21 @@ func _read_ipl_line(section: String, tokens: Array[String]):
var placement := ItemPlacement.new() var placement := ItemPlacement.new()
placement.id = tokens[0].to_int() placement.id = tokens[0].to_int()
placement.model_name = tokens[1].to_lower() placement.model_name = tokens[1].to_lower()
# Convert GTA to Godot coordinate system
placement.position = Vector3( placement.position = Vector3(
tokens[2].to_float(), tokens[2].to_float(),
tokens[3].to_float(), tokens[4].to_float(),
tokens[4].to_float(), ) -tokens[3].to_float(), )
# Scale conversion follows the same pattern
placement.scale = Vector3( placement.scale = Vector3(
tokens[5].to_float(), tokens[5].to_float(),
tokens[6].to_float(), tokens[7].to_float(),
tokens[7].to_float(), ) tokens[6].to_float(), )
# Quaternion conversion requires negating components
placement.rotation = Quaternion( placement.rotation = Quaternion(
-tokens[8].to_float(), -tokens[8].to_float(),
-tokens[9].to_float(),
-tokens[10].to_float(), -tokens[10].to_float(),
-tokens[9].to_float(),
tokens[11].to_float(), ) tokens[11].to_float(), )
placements.append(placement) placements.append(placement)
@ -115,7 +119,6 @@ func _read_map_data(path: String, line_handler: Callable) -> void:
func clear_map() -> void: func clear_map() -> void:
map = Node3D.new() map = Node3D.new()
map.rotation.x = deg_to_rad(-90.0)
func spawn_placement(ipl: ItemPlacement) -> Node3D: func spawn_placement(ipl: ItemPlacement) -> Node3D:
return spawn(ipl.id, ipl.model_name, ipl.position, ipl.scale, ipl.rotation) return spawn(ipl.id, ipl.model_name, ipl.position, ipl.scale, ipl.rotation)
@ -147,13 +150,31 @@ func spawn(id: int, model_name: String, position: Vector3, scale: Vector3, rotat
var colshape := CollisionShape3D.new() var colshape := CollisionShape3D.new()
if collision is ColFile.TBox: if collision is ColFile.TBox:
var aabb := AABB() var aabb := AABB()
aabb.position = collision.min # Get min and max positions from collision box
aabb.end = collision.max 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() var shape := BoxShape3D.new()
shape.size = aabb.size shape.size = aabb.size
colshape.shape = shape colshape.shape = shape
colshape.position = aabb.get_center() colshape.position = aabb.get_center()
sb.add_child(colshape) sb.add_child(colshape)
else:
sb.add_child(colshape)
if item.colfile.vertices.size() > 0: if item.colfile.vertices.size() > 0:
var colshape := CollisionShape3D.new() var colshape := CollisionShape3D.new()
var shape := ConcavePolygonShape3D.new() var shape := ConcavePolygonShape3D.new()

View file

@ -4,7 +4,6 @@ extends Node
var suzanne := preload("res://prefabs/suzanne.tscn") var suzanne := preload("res://prefabs/suzanne.tscn")
func _ready() -> void: func _ready() -> void:
world.rotation.x = deg_to_rad(-90.0)
var start := Time.get_ticks_msec() var start := Time.get_ticks_msec()
var target = MapBuilder.placements.size() var target = MapBuilder.placements.size()
var count := 0 var count := 0

View file

@ -9,7 +9,6 @@ func _ready() -> void:
spinbox.rounded = true spinbox.rounded = true
spinbox.max_value = 0 spinbox.max_value = 0
misc = RWTextureDict.new(GameManager.open_file("models/misc.txd", FileAccess.READ)) misc = RWTextureDict.new(GameManager.open_file("models/misc.txd", FileAccess.READ))
meshinstance.rotation.x = deg_to_rad(-90.0)
func _ld_dff() -> void: func _ld_dff() -> void:
var dialog := FileDialog.new() var dialog := FileDialog.new()

View file

@ -110,17 +110,17 @@ func _init(file: FileAccess):
morph_t.has_normals = file.get_32() != 0 morph_t.has_normals = file.get_32() != 0
if morph_t.has_vertices: if morph_t.has_vertices:
for j in vert_count: for j in vert_count:
var vert := Vector3() # Convert GTA to Godot coordinate system
vert.x = file.get_float() var x := file.get_float()
vert.y = file.get_float() var y := file.get_float()
vert.z = file.get_float() var z := file.get_float()
morph_t.vertices.append(vert) morph_t.vertices.append(Vector3(x, z, -y))
if morph_t.has_normals: if morph_t.has_normals:
for j in vert_count: for j in vert_count:
var normal := Vector3() var normal := Vector3()
normal.x = file.get_float() normal.x = file.get_float()
normal.y = file.get_float()
normal.z = file.get_float() normal.z = file.get_float()
normal.y = file.get_float()
morph_t.normals.append(normal) morph_t.normals.append(normal)
morph_targets.append(morph_t) morph_targets.append(morph_t)
material_list = RWMaterialList.new(file) material_list = RWMaterialList.new(file)

View file

@ -7,7 +7,6 @@ extends Node
var car := preload("res://scenes/car.tscn") var car := preload("res://scenes/car.tscn")
func _ready() -> void: func _ready() -> void:
world.rotation.x = deg_to_rad(-90.0)
var start := Time.get_ticks_msec() var start := Time.get_ticks_msec()
var target = MapBuilder.placements.size() var target = MapBuilder.placements.size()
var count := 0 var count := 0