Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<Node> ViewportRaycast(Rect2 area)
- {
- Vector2 viewportSize = GetViewport().GetSize();
- Vector2 tl = new Vector2(area.Position.x, area.Position.y);
- Vector2 tr = new Vector2(area.Position.x + area.Size.x, area.Position.y);
- Vector2 bl = new Vector2(area.Position.x, area.Position.y + area.Size.y);
- Vector2 br = new Vector2(area.Position.x + area.Size.x, area.Position.y + area.Size.y);
- PhysicsDirectSpaceState state = GetWorld().GetDirectSpaceState();
- PhysicsShapeQueryParameters param = new PhysicsShapeQueryParameters();
- ConvexPolygonShape shape = new ConvexPolygonShape();
- shape.SetPoints(new Vector3[] {
- camera.ProjectRayOrigin(tl), // THIS IS NOT REQUIRED FOR PERSPECTIVE CAMERA. You may want to use camera.Translation
- camera.ProjectRayOrigin(tr),
- camera.ProjectRayOrigin(bl),
- camera.ProjectRayOrigin(br),
- camera.ProjectRayOrigin(tl) + camera.ProjectRayNormal(tl) * 1000,
- camera.ProjectRayOrigin(tr) + camera.ProjectRayNormal(tr) * 1000,
- camera.ProjectRayOrigin(bl) + camera.ProjectRayNormal(bl) * 1000,
- camera.ProjectRayOrigin(br) + camera.ProjectRayNormal(br) * 1000
- });
- param.SetShape(shape);
- param.CollisionMask = 2; // Very Important to prevent lag
- Godot.Array result = state.IntersectShape(param, 1000);
- List<Node> nodes = new List<Node>();
- foreach(Godot.Dictionary cast in result)
- {
- nodes.Add((Node)cast["collider"]);
- }
- return nodes;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement