Esempio n. 1
0
    public static bool TryRayCast(Vector3 global_position, Vector3 direction, out RayCast3DResult result, uint mask = 1, bool body = true, bool area = false, bool debug = false)
    {
        var outcome = space.IntersectRay(global_position, global_position + direction, null, mask, body, area);

        result = new RayCast3DResult();
        var hit = outcome.Count > 0;

        if (hit)
        {
            result.normal   = (Vector3)outcome["normal"];
            result.point    = (Vector3)outcome["position"];
            result.collider = (Node)outcome["collider"];
        }
        if (debug)
        {
            if (hit)
            {
                Debug.DrawLine3D(global_position, result.point, Colors.Red);
                Debug.DrawLine3D(result.point, result.point + result.normal, Colors.Cyan);
            }
            else
            {
                Debug.DrawLine3D(global_position, global_position + direction, Colors.Green);
            }
        }
        return(hit);
    }
Esempio n. 2
0
    public static bool TrySphereCast(Vector3 global_position, Vector3 direction, float radius, out RayCast3DResult result, uint mask = 1, bool body = true, bool area = false, bool debug = false)
    {
        result        = new RayCast3DResult();
        sphere.Radius = radius;
        volumeParams.CollideWithBodies = body;
        volumeParams.CollideWithAreas  = area;
        volumeParams.SetShape(sphere);
        volumeParams.CollisionMask = (int)mask;
        var transform = Transform.Identity;

        transform.origin       = global_position;
        volumeParams.Transform = transform;

        var cast = space.CastMotion(volumeParams, direction);

        if (cast.Count == 0)
        {
            return(false);
        }
        transform.origin       = global_position + direction * (float)cast[1];
        volumeParams.Transform = transform;
        var info = space.GetRestInfo(volumeParams);

        var hit = info.Count > 0;

        if (hit)
        {
            result.normal = (Vector3)info["normal"];
            result.point  = (Vector3)info["point"];
            var col = (Godot.Collections.Dictionary)space.IntersectShape(volumeParams, 1)[0];
            result.collider = (Node)col["collider"];
        }

        if (debug)
        {
            var color = Colors.Green;
            if (hit)
            {
                color = Colors.Red;
                Debug.DrawLine3D(result.point, transform.origin, Colors.Cyan);
                Debug.DrawCircle3D(transform.origin, cam.GlobalTransform.basis.z, radius, color);
            }
            else
            {
                Debug.DrawCircle3D(global_position + direction, cam.GlobalTransform.basis.z, radius, color);
            }
            Debug.DrawLine3D(global_position, global_position + direction, color);
        }
        return(hit);
    }