Advertisement

What's wrong with this code? (intersect_shape in GDNative)

Started by February 05, 2021 03:38 AM
0 comments, last by rivoxibi 3 years, 9 months ago

I rewrote the code from GDScript to GDNative, but it throws a bunch of errors and crashes at the end. Obviously I made a mistake somewhere, but where exactly?

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();
}

What's the problem here?

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

This topic is closed to new replies.

Advertisement