Advertisement
Guest User

Untitled

a guest
Nov 5th, 2018
877
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public List<Node> ViewportRaycast(Rect2 area)
  2. {
  3. Vector2 viewportSize = GetViewport().GetSize();
  4.  
  5. Vector2 tl = new Vector2(area.Position.x, area.Position.y);
  6. Vector2 tr = new Vector2(area.Position.x + area.Size.x, area.Position.y);
  7. Vector2 bl = new Vector2(area.Position.x, area.Position.y + area.Size.y);
  8. Vector2 br = new Vector2(area.Position.x + area.Size.x, area.Position.y + area.Size.y);
  9.  
  10. PhysicsDirectSpaceState state = GetWorld().GetDirectSpaceState();
  11. PhysicsShapeQueryParameters param = new PhysicsShapeQueryParameters();
  12. ConvexPolygonShape shape = new ConvexPolygonShape();
  13. shape.SetPoints(new Vector3[] {
  14. camera.ProjectRayOrigin(tl), // THIS IS NOT REQUIRED FOR PERSPECTIVE CAMERA. You may want to use camera.Translation
  15. camera.ProjectRayOrigin(tr),
  16. camera.ProjectRayOrigin(bl),
  17. camera.ProjectRayOrigin(br),
  18. camera.ProjectRayOrigin(tl) + camera.ProjectRayNormal(tl) * 1000,
  19. camera.ProjectRayOrigin(tr) + camera.ProjectRayNormal(tr) * 1000,
  20. camera.ProjectRayOrigin(bl) + camera.ProjectRayNormal(bl) * 1000,
  21. camera.ProjectRayOrigin(br) + camera.ProjectRayNormal(br) * 1000
  22. });
  23. param.SetShape(shape);
  24. param.CollisionMask = 2; // Very Important to prevent lag
  25. Godot.Array result = state.IntersectShape(param, 1000);
  26.  
  27. List<Node> nodes = new List<Node>();
  28.  
  29. foreach(Godot.Dictionary cast in result)
  30. {
  31. nodes.Add((Node)cast["collider"]);
  32.  
  33. }
  34.  
  35. return nodes;
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement