Intersecting Area and KinematicBody

mrbrogleymrbrogley Posts: 6Member

I have the multiple KinematicBody Object which I move by move_and_collide(). These correctly collide with each other, so they should be set up correctly.

Now I have an Area with a ConcavePolygonShape that I set up programmatically. I would like to get body_entered events as soon as a KinematicBody enters the area. Unfortunately no such events happen.
The Area is set for "Monitoring" and I set the "Mask" to be the same as the "Layer" of the KinematicBody objects.

Is there anything else that needs to be set so the signals are triggered?

Btw the area is actually just a flat polygon, while the KinematicBody has a BoxShape. However I set the area so they intersect.

Best Answer

  • mrbrogleymrbrogley Posts: 6
    Accepted Answer

    Thanks for the help. I already tinkered around some more and found a solution for me yesterday:

    I reproduced the problem in a minimal project and was able to find a solution that works for me: Use ConvexPolygonShape instead of ConcavePolygonShape. ConvexPolygonShape is also much easier to setup just by providing all corner vertices.

    But I think I also found the reason why Concave did not work for me (untested):

    My original Setup was like this:
    Area
    -> CollisionShape

    Then programmatically I added the ConcavePolygonShape with set_shape. This works for some shapes (BoxShape) for example, but did not work for my ConcavePolygonShape.

    Instead the solution is to delete the CollisionShape and directly set the shape on Area with
    var shape = ConvexPolygonShape.new() shape.set_points(corners) var shape_owner = $Area.create_shape_owner($Area) var $Area.shape_owner_add_shape(shape_owner, shape)

    I did not test with ConcavePolygonShape yet. For the moment ConvexPolygonShape fits my needs much better.

Answers

  • mrbrogleymrbrogley Posts: 6Member

    I narrowed the problem down: The collision is not working with flat shapes. So using a PlaneShape for example does not work.

    The same with ConcavePolygonShape: Even if I set the faces to be a closed concave shape (for example a box), no collision is detected.

    Anyone know what I'm doing wrong?

  • MagicLordMagicLord Posts: 578Unconfirmed
    edited September 2018

    I used Area succesfully in a 2.5D project.
    Have you tried a cube collision for your Area ? I don't remember if Area worls with boths RigidBody and KinematicBody ?
    Can you upload the project or something to test ?

  • mrbrogleymrbrogley Posts: 6Member
    Accepted Answer

    Thanks for the help. I already tinkered around some more and found a solution for me yesterday:

    I reproduced the problem in a minimal project and was able to find a solution that works for me: Use ConvexPolygonShape instead of ConcavePolygonShape. ConvexPolygonShape is also much easier to setup just by providing all corner vertices.

    But I think I also found the reason why Concave did not work for me (untested):

    My original Setup was like this:
    Area
    -> CollisionShape

    Then programmatically I added the ConcavePolygonShape with set_shape. This works for some shapes (BoxShape) for example, but did not work for my ConcavePolygonShape.

    Instead the solution is to delete the CollisionShape and directly set the shape on Area with
    var shape = ConvexPolygonShape.new() shape.set_points(corners) var shape_owner = $Area.create_shape_owner($Area) var $Area.shape_owner_add_shape(shape_owner, shape)

    I did not test with ConcavePolygonShape yet. For the moment ConvexPolygonShape fits my needs much better.

  • MagicLordMagicLord Posts: 578Unconfirmed

    @mrbrogley said:
    Thanks for the help. I already tinkered around some more and found a solution for me yesterday:

    I reproduced the problem in a minimal project and was able to find a solution that works for me: Use ConvexPolygonShape instead of ConcavePolygonShape. ConvexPolygonShape is also much easier to setup just by providing all corner vertices.

    But I think I also found the reason why Concave did not work for me (untested):

    My original Setup was like this:
    Area
    -> CollisionShape

    Then programmatically I added the ConcavePolygonShape with set_shape. This works for some shapes (BoxShape) for example, but did not work for my ConcavePolygonShape.

    Instead the solution is to delete the CollisionShape and directly set the shape on Area with
    var shape = ConvexPolygonShape.new() shape.set_points(corners) var shape_owner = $Area.create_shape_owner($Area) var $Area.shape_owner_add_shape(shape_owner, shape)

    I did not test with ConcavePolygonShape yet. For the moment ConvexPolygonShape fits my needs much better.

    Good news.
    I never setup collisions and physics by code because i use prefabs and it's more simple and faster in the editor :D

    Is your game procedural at run time to need to setup area collision with code ?

  • mrbrogleymrbrogley Posts: 6Member

    I think so, yes.
    I have units that can attack other units if in range. However the range is not a simple distance check, rather a trapezoid in front of the unit. If any other unit is at least partially inside that trapezoid, then it is possible to attack that unit.

    This range shape depends on the width of the unit and the attack range. So computing it programmatically was the best solution I could come up with. However this ist my first Godot or even game project, so maybe there is a better way.

    The main downside is computing all the corner vertices manually. Creating more complex shapes would be quite difficult.

  • MagicLordMagicLord Posts: 578Unconfirmed

    @mrbrogley said:
    I think so, yes.
    I have units that can attack other units if in range. However the range is not a simple distance check, rather a trapezoid in front of the unit. If any other unit is at least partially inside that trapezoid, then it is possible to attack that unit.

    This range shape depends on the width of the unit and the attack range. So computing it programmatically was the best solution I could come up with. However this ist my first Godot or even game project, so maybe there is a better way.

    The main downside is computing all the corner vertices manually. Creating more complex shapes would be quite difficult.

    Keep shape physics for player and units to keep collision between them, and calculate with math when units are in attack range. Something like : attack = distance (player, unit) < player radius + unit radius + attack radius.
    You can add player and unit forward vector dot product to check when the attack angle should be valid.
    Such checks are faster with math instead of using physics collisions.

  • mrbrogleymrbrogley Posts: 6Member

    Here is a sketch of how I want the range check to work:

    The range should consider the range from the whole front line of the rectangle. The tricky thing here for calculating the range is that I can't calculate from a single point, but rather from a line. But I think it might be possible to calculate, I never really thought about it in detail. Maybe there are some simple algorithms to do such checks.

  • MagicLordMagicLord Posts: 578Unconfirmed

    @mrbrogley said:
    Here is a sketch of how I want the range check to work:

    The range should consider the range from the whole front line of the rectangle. The tricky thing here for calculating the range is that I can't calculate from a single point, but rather from a line. But I think it might be possible to calculate, I never really thought about it in detail. Maybe there are some simple algorithms to do such checks.

    You can combine three oriented shapes, a center rectangle, and two sides triangles or two cones.
    But i think you can keep physics and stay simple, it should not hit performance , it's not something very complex.
    This is commonly used in other games , the player has some physics collision shape like a sphere with a radius to detect what units objects are inside the volume so in range.
    Physics give you a list of game objects and their properties you can work with.

  • mrbrogleymrbrogley Posts: 6Member

    Thanks. The solution with ConvexPolygonShape actually works pretty well. I can use the same input coordinates to create the collision shape and also the render the indicator for the user to see.

Leave a Comment

BoldItalicStrikethroughOrdered listUnordered list
Emoji
Image
Align leftAlign centerAlign rightToggle HTML viewToggle full pageToggle lights
Drop image/file