CollisionShape2D not following RigidBody2D

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By ondesic

In my “Main.tscn” scene there is RigidBody2D named “Ship”. It is an instance of Ship.tscn. The Ship visually starts at position (20,20) and has the following children Nodes:

RigidBody2D (named "Ship")

in the code I initialize the Ship’s position with the following code:

$Ship.position = Vector2(300,300)

However, even though the Ship object visually moves to (300,300), the collision occurs at position (20,20) and not at (300,300).

Any ideas as to what is happening?

A few things:

  1. In the editor, toggle on the “Visible Collision Shapes” debug
    option. In v2.x it is in located at the top of the editor to the
    right of the play and stop buttons. In v3.x it is located under the
    Debug menu option in the top left. This will show the collision
    shapes while you run in the editor. You may want to hide your ship
    Sprite to make it easier to see.

  2. What code are you using to determine the collision position?

  3. Are there any other physics objects near the ship’s start position
    at the start of the game?

2D||!2D | 2018-03-17 12:48

Correct me if I’m wrong, but the CollisionShape and Sprite are children of the RigidBody. Therefore if I move the RigidBody, shouldn’t the children move with it?

I don’t have separate code to move the CollisionShape because it is a child of the RigidBody

ondesic | 2018-03-17 19:20

:bust_in_silhouette: Reply From: Pieter-Jan Briers

You shouldn’t set the position of a RigidBody2D directly. See this question for an answer:

Then what node should I use if I want to sometimes change the position manually and still detect collisions?

ondesic | 2018-03-19 13:19

You still can change the position manually, you just have to do it through _integrate_forces(). The answer I linked explains it.

Pieter-Jan Briers | 2018-03-19 14:38