Godot Hearing Objects

Viewed 30

enter image description here

Is there a way in Godot to calculate when a sound collides with an object or how low the sound is when it collides with an object? For example, in the picture below, there is an AudioStreamPlayer3D in the middle of the floor and three yellow cubes at different spots on the floor. Would it be possible to have the three cubes determine when/how loud the noise that the AudioStreamPlayer3d emits is so that the sound could be triangulated? Similarly, the farthest cub away might not hear it while the other two can.

1 Answers

No, this is not available.

If you are making AI, I'm going to advice against this approach, instead resource to Area. However to answer the question as posted…

You could replicate the logic Godot uses to compute the volume in your code (source):

float AudioStreamPlayer3D::_get_attenuation_db(float p_distance) const {
    float att = 0;
    switch (attenuation_model) {
        case ATTENUATION_INVERSE_DISTANCE: {
            att = Math::linear2db(1.0 / ((p_distance / unit_size) + CMP_EPSILON));
        } break;
        case ATTENUATION_INVERSE_SQUARE_DISTANCE: {
            float d = (p_distance / unit_size);
            d *= d;
            att = Math::linear2db(1.0 / (d + CMP_EPSILON));
        } break;
        case ATTENUATION_LOGARITHMIC: {
            att = -20 * Math::log(p_distance / unit_size + CMP_EPSILON);
        } break;
        case ATTENUATION_DISABLED:
            break;
        default: {
            ERR_PRINT("Unknown attenuation type");
            break;
        }
    }

    att += unit_db;
    if (att > max_db) {
        att = max_db;
    }

    return att;
}
Related