• Godot Help
  • collision with a position transformed object

I am new to game design & development.
I am now trying to collide objects that are not players.
I have a player and it holds an item. I want the item move together with the player.
So I refresh its position whenever the player changes position, like this:

var chg:= Vector2(-51,0) # item is in front of the character
item.transform = $Position2D.global_transform.translated(chg)

I want the item to collide with objects like the player who holds it.
Collision layer and mask is selected identical to the player.

But since the object is transformed (and not moved) , it does not collide. It just transforms to the new position.

How can I make it collide and stop when it hits some object in the same layer ?
(and also stop the player of course)

    ilkeraktuna I want the item move together with the player.

    If you make the item a child node of the player node, it should move with the player.

      DaveTheCoder

      thank you. that will work when I move the player. But when I make an attack with it, player won't actually move, but the item will have to move.
      how can I do that ?

      What kind of movement will the item do? Instant transport, or could it be done with the physics server?

        DaveTheCoder
        item will rotate and move in x,y axis. I do it with following code:

        if !facingright:
        rotation_degrees -= 7
        else:
        rotation_degrees += 7
        position += transform.x * (delta * 15)
        position += transform.y * (delta * 10)

        can I do this with physics ?

        If you want it to happen instantly, probably not.

        Do you really need collision detection for it? Could you simply perform the "after-collision" action explicitly?

          DaveTheCoder

          I don't need it to happen instantly.
          If possible , how can I do it with physics ?

          Do you really need collision detection for it? Could you simply perform the "after-collision" action explicitly?

          What do you mean here ? I need object 2 to block object 1 , so I think collision is necessary.

          You could do it with the right combination of physics bodies. For example, if the player is a KinematicBody2D, and the item is an Area2D, then you could use the Area2D's body_entered signal to detect if the player overlaps with the item. You would need to use code in the Area2D's _physics_process() method to control its movement (position += velocity * delta).
          https://docs.godotengine.org/en/stable/tutorials/physics/using_area_2d.html

          A Tween or AnimationPlayer might work for this instead of code in _physics_process().

          You could use a RigidBody2D instead of an Area2D, but that would be more difficult to control precisely.

            DaveTheCoder

            You would need to use code in the Area2D's _physics_process() method to control its movement (position += velocity * delta).

            Ok. But how can I rotate and/scale the item in _physics_process() ?
            (I already figured out how to overlap bodies and use body_entered signal, but I don't know how to rotate in physics_process)

            DaveTheCoder

            ok. got it.
            So whatever I do under physics_process() will take place as a movement and not just transform there. So it will collide ?