From 4b064db838e34e255ee1a72b6f06a9dbe5cfaac1 Mon Sep 17 00:00:00 2001 From: copygirl Date: Tue, 30 Sep 2025 14:33:23 +0200 Subject: [PATCH] Fix bug in shape definition Previously, `points` would be reused and (for example) rotated multiple times, rather than once by the desired angle, which resulted in nonsensical variant order. Now, variants with indices 0 to 3 correctly correspond to rotations of 0, 90, 180 and 270 degrees. --- world/layers/matter.gd | 1 + world/layers/shape.gd | 28 ++++++++++++++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) diff --git a/world/layers/matter.gd b/world/layers/matter.gd index dd05d74..59be054 100644 --- a/world/layers/matter.gd +++ b/world/layers/matter.gd @@ -5,6 +5,7 @@ static var REGISTRY := Registry.new() static var NONE := Matter.new("none") static var PLASTIC := Matter.new("plastic") +## Default matter to return when chunk or layer is not found. static var DEFAULT := NONE var id : int diff --git a/world/layers/shape.gd b/world/layers/shape.gd index ee79702..65e0563 100644 --- a/world/layers/shape.gd +++ b/world/layers/shape.gd @@ -9,6 +9,7 @@ static func lookup(id: int) -> Shape: static var EMPTY : Shape static var FULL : Shape +## Default shape to return when chunk or layer is not found. static var DEFAULT : Shape var id : int @@ -34,6 +35,8 @@ func _init(base: Base, mirror: bool, angle: int, shape: Shape2D, points: PackedV uvs.append((Vector2.ONE / 2) + point) +## Represents a base shape, before it has been rotated and mirrored +## (if applicable) to create a number of variants based on that shape. class Base: static var EMPTY := Base.new("empty") static var FULL := Base.new("full" , _rect()) @@ -60,28 +63,25 @@ class Base: shape = _rotate(shape) # rotate by 90 degrees (swap x and y components) variants.append(Shape.new(self, false, 0, shape, _points(shape))) elif shape is Array: - # Convert points (pairs of ints) in array to convex shape. + # Convert points (pairs of `int`s) in `Array` to convex shape. var factor := float(shape.max()) - var points := PackedVector2Array() + var points_orig := PackedVector2Array() # untransformed points for i in range(0, shape.size(), 2): var point := Vector2(shape[i] / factor, shape[i + 1] / factor) - points.append(point - (Vector2.ONE / 2)) # ensure shape is centered + points_orig.append(point - (Vector2.ONE / 2)) # ensure shape is centered # Add variations of the shape (rotated, mirrored), including the standard shape. for mirror in [ false, true ] if mirrored else [ false ]: - if mirror: - # Mirror (flip) points along the X axis. - points = points.duplicate() - for i in points.size(): - points[i] = points[i] * Transform2D.FLIP_X - for angle in [ 0, 90, 180, 270 ] if rotated else [ 0 ]: - if angle != 0: - # Rotate points around the center. + var points := points_orig.duplicate() # transformed points + # Technically we could skip `duplicate()` if not mirrored and not rotated, but eh. + + if mirror: # Mirror (flip) points along the X axis. + for i in points.size(): points[i] *= Transform2D.FLIP_X + + if angle != 0: # Rotate points around the center. var transform := Transform2D(deg_to_rad(angle), Vector2.ZERO) - points = points.duplicate() - for i in points.size(): - points[i] = points[i] * transform + for i in points.size(): points[i] *= transform var shape_points: PackedVector2Array for point in points: shape_points.append(point * Block.SIZE)