![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | 3DWizard |
Hi,
I am new to Godot so I might be missing something obvious here, but the following behavior seems like a bug to me:
I have a RigidBody2D scene node with a CollisionShape2D subnode. I am using a capsule shape, but this seems to be irrelevant for the problem, I tried it with other shapes, too. The RigidBody2D has a GDScript assigned, which listens to input_event from CollisionShape2D to detect a mouse click inside the collision shape, the connected function looks like this:
func _on_input_event(viewport, event, shape_idx):
if event is InputEventMouseButton and event.button_index == MOUSE_BUTTON_LEFT and event.pressed:
print("clicked on TestRigidBody")
self.position += Vector2(0.0, 50.0)
I then have my main scene derived from Node which instantiates this RigidBody2D scene (named TestRigidBody) and just moves it at a start position like this:
func _ready():
var screen_size = DisplayServer.window_get_size()
$TestRigidBody.position = 0.5 * Vector2(screen_size.x, screen_size.y)
The problem now arises when the user clicks on the TestRigidBody: At first everything works fine and the rigid body and its collision shape seem to be moved by Vector2(0.0, 50.0) as intended. I enabled “Debug->Visible Collision Shapes”, which shows the collision shape is moving, too. But when trying to click inside the collision shape again to move the object once more, only a click inside the initial collision shape triggers the _on_input_event function not a click inside the actual visualized collision shape. So essentially it seems that the collision shape of TestRigidBody has not moved at all when clicking on the object, although godot debugger shows the visualized collision shape in the intended place.
Am I right in assuming this is a bug and do you know how to circumvent it?
I tested this in Godot 3.5.2, Godot 4.0.3 and Godot 4.1 dev3 running on an Intel mac. I attached a test project with the above setup for you to see the problem in action:
collision_shape_movement_test
I’d guess you’re getting some unintended influence from the physics system. As noted in the docs, ideally you should not be directly changing the position
of a Rigidbody2D
as it’s managed by the physics system. Instead, you should be using _integrate_forces()
.
For instance, this statement from the above link:
Note: You should not change a RigidBody2D’s position or linear_velocity every frame or even very often. If you need to directly affect the body’s state, use _integrate_forces, which allows you to directly access the physics state.
jgodfrey | 2023-06-01 20:51