0 votes
    void godot::NavigationArea::fill_points()
{
    u32 k = 0;
    godot::CircleShape2D *shape = godot::CircleShape2D::_new();
    fp32 radius = _max(horizontal_spacing, vertical_spacing);
    godot::Physics2DShapeQueryParameters* query = godot::Physics2DShapeQueryParameters::_new();
    shape->set_radius(radius);
    //query.set_exclude([])
    query->set_shape(shape);

    godot::Physics2DDirectSpaceState* space_state = get_world_2d()->get_direct_space_state();

    for (u32 i = 0; i < width; i+= horizontal_spacing) {
        for (u32 j = 0; j < height; j += vertical_spacing) {
            godot::Vector2 pos = godot::Vector2(i, j);
            query->set_transform(godot::Transform2D(0.f, pos));
            godot::Array res = space_state->intersect_shape(query);

            boolean obstaclesIntersected = false;
            if (!res.empty()) {
                for (u32 i = 0; i < res.size(); ++i) {
                    godot::Dictionary intersection = res[i];
                    godot::Node* collider = Object::cast_to<godot::Node>(intersection["collider"].operator Object * ());
                    if (collider && collider->is_in_group("Obstacles")) {
                        obstaclesIntersected = true;
                        break;
                    }
                }
            }

            if (!obstaclesIntersected) {
                godot::Dictionary point;
                point["position"] = pos;
                points[k] = point;
                k += 1;
            }
        }
    }

    query->free();
    shape->free();
}

I'm getting a crash and this errors:

ERROR: _intersect_shape: Condition "!p_shape_query.is_valid()" is true. Returned: Array()
   At: servers/physics_2d_server.cpp:295
in Engine by (254 points)

1 Answer

0 votes
Best answer

Ok, I used it wrong.

I should use Ref instead of raw pointers:

void godot::NavigationArea::fill_points()
{
  u32 k = 0;
  godot::Ref<godot::CircleShape2D> shape;
  shape.instance();
  fp32 radius = _max(horizontal_spacing, vertical_spacing);
  godot::Ref <godot::Physics2DShapeQueryParameters> query;
  query.instance();
  shape->set_radius(radius);
  //query.set_exclude([])
  query->set_shape(shape);

  godot::Physics2DDirectSpaceState* space_state = get_world_2d()->get_direct_space_state();

  for (u32 i = 0; i < width; i+= horizontal_spacing) {
    for (u32 j = 0; j < height; j += vertical_spacing) {
      godot::Vector2 pos = godot::Vector2(i, j);
      query->set_transform(godot::Transform2D(0.f, pos));
      godot::Array res = space_state->intersect_shape(query);

      boolean obstaclesIntersected = false;
      if (!res.empty()) {
        for (u32 i = 0; i < res.size(); ++i) {
          godot::Dictionary intersection = res[i];
          godot::Node* collider = Object::cast_to<godot::Node>(intersection["collider"].operator Object * ());
          if (collider && collider->is_in_group("Obstacles")) {
            obstaclesIntersected = true;
            break;
          }
        }
      }

      if (!obstaclesIntersected) {
        godot::Dictionary point;
        point["position"] = pos;
        points[k] = point;
        k += 1;
      }
    }
  }
}

Thanks to https://www.reddit.com/r/godot/comments/gj1niv/gdnative_intersect_shape_crashes/

by (254 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.