Scale an object and a CollisionShape2D, and make sure they are the exact same size as each other

Godot Version

Godot 4.0

Problem

Hello! I’ve got a bit of a math problem:
I have an object that is 128x128 in size, and on top of that object I have an Area2D with a CollisionShape2D, that is also 128x128 in size.
I have an export variable called “size_mult”, which should multiply both of the sizes equally.

When “size_mult” is set to 1, they are the same size:
size_mult_1

If the “size_mult” is set to 0.8, the CollisionShape2D is smaller than the object:
size_mult_08

elif, the “size_mult” is set to 1.2, the CollisionShape2D is bigger than the object:
size_mult_12

The resize code is quite simple and I don’t know what should be changed in order to make it work perfectly:

obj_tile.scale = Vector2(1, 1) * size_mult
obj_area.scale = Vector2(0.5, 0.5) * size_mult

Question

How can I make sure that the object and CollisionShape2D are the same size?

When you multiply the size_mult variable by Vector2(0.5, 0.5), the amount you scale it by changes as well (if size_mult were, say, 2, (1 × 2 = 2), the scale for the base object, but (0.5 × 2 = 1), the scale for the area.).

Instead,

  1. Multiply the scale first, then scale down to the smaller size using subtraction (size_mult - Vector2(0.5, 0.5)) or
  2. My favorite choice, Set the obj_area.size to be the same size as obj_tile.size as a base so the scales are the same.

Hi, thanks for helping out! I got it working like this:

obj_tile.scale = Vector2(1, 1) * size_mult
obj_area.scale = obj_tile.scale
obj_area.scale -= Vector2(1, 1) * size_mult - Vector2(0.5, 0.5)

(For possible future readers that have the same problem, my size_mult variable is a float, which is why I multiply it with a Vector2(1, 1). If you have it as a Vector2, (which is smart, because then you can control x- and y-scaling separately) you don’t need to multiply it with a Vector2(1, 1))