Table of Contents

Camera Extensions

Extension methods for CameraComponent. They fall into two halves: converting between screen and world space, which needs no physics engine, and raycasting into the scene, which does - so those live in the Bepu and Bullet packages.

Screen and world space

In the Stride.CommunityToolkit.Engine namespace. Screen positions here are normalized: (0,0) is the top-left of the viewport and (1,1) the bottom-right.

  • ScreenToWorldRay() - A Ray from the camera through a screen point. The usual starting point for object picking.
  • ScreenToWorldPointOnXYPlane() - Where that ray crosses the Z=0 plane, as a Vector2, or null when the ray is parallel to the plane or crosses it outside the near and far planes. This is how you turn a mouse position into a 2D world position without any physics at all.
  • ScreenToWorldRayEnds() - The near and far points of the ray as Vector4 world positions, already divided by w.
  • GetOrthographicVisibleSize() - The width and height an orthographic camera shows, in world units, read from its projection.
  • GetOrthographicVisibleBounds() - That rectangle of the XY plane centred on the camera, as its lower-left and upper-right corners: the edges of a 2D scene's view.
  • ScreenToWorldRaySegment() - The ray as a RaySegment from the near plane to the far plane, which is the form the Bepu and Bullet Simulation raycast helpers take.
  • ScreenToWorldPoint() - Converts a screen position with a depth to a world position.
  • WorldToScreenPoint() - The inverse: where a world position lands on screen. Use this to hang a label on an object.
  • IsInScreenRectangle() - Whether a world position lands inside a rectangle of the screen, given as two normalized corners in either order: the test a marquee selection makes for every candidate. A position behind the camera is never inside. E05_3D_LayerSelection drags a rectangle and selects with it.
  • TryWorldToScreenPoint() - The same in pixels, and false for a position behind the camera or off the screen, so a label is only drawn for what is in view. An overload gives the normalized position.
  • WorldToClip() - Converts a world position to clip space, one step earlier than WorldToScreenPoint().
  • LogicDirectionToWorldDirection() - Turns a 2D input direction into a world direction relative to where the camera is facing, so "forward" on a gamepad stick means forward on screen. An overload takes an explicit up vector.
Note

Most of these have ref/out overloads that avoid copying vectors, and WorldToScreenPoint() has one taking a GraphicsDevice when you want pixels rather than normalized coordinates. See the API reference for the full set.

Raycasting - Bepu

In the Stride.CommunityToolkit.Bepu namespace.

  • Raycast() - Casts from the camera through a screen position and reports the first hit as a HitInfo.
  • RaycastMouse() - The same, reading the mouse position for you.

Both cast in the Bepu simulation of the camera's entity, so the camera must already be in the scene.

Raycasting - Bullet

In the Stride.CommunityToolkit.Bullet namespace. Both take either a ScriptComponent or a Simulation and return a HitResult.

  • Raycast() - Casts from the camera through a screen position, filtered by collision group.
  • RaycastMouse() - The same, reading the mouse position for you.

See also