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](https://forum.godotengine.org/uploads/default/original/3X/5/b/5bdd256bc59774ede4f504b41f70eb87b9a74f90.png)
If the “size_mult” is set to 0.8, the CollisionShape2D is smaller than the object:
![size_mult_08](https://forum.godotengine.org/uploads/default/original/3X/6/4/64d98167956a1362a17e712e2ce51ec960acc0a4.png)
elif, the “size_mult” is set to 1.2, the CollisionShape2D is bigger than the object:
![size_mult_12](https://forum.godotengine.org/uploads/default/original/3X/b/b/bbf2964238d99eb55d184f248c4bea13025d2696.png)
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,
- Multiply the scale first, then scale down to the smaller size using subtraction (
size_mult
- Vector2(0.5, 0.5)
) or
- 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))