Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3401706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:02:54+00:00 2026-05-18T05:02:54+00:00

I have a ray, going outwards from a camera, I need to find the

  • 0

I have a ray, going outwards from a camera, I need to find the length of the ray inside the cube. Note that the camera could be inside the cube, in which case it should return the distance to the edge of the cube.

It’s for a shader, so it should be relatively small and involve a minimum of branching.

Any ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:02:55+00:00Added an answer on May 18, 2026 at 5:02 am

    To intersect a ray with a convex polyhedron, intersect the ray with the planes containing each face.

    Suppose the ray starts at q and has direction r. Then any point on the ray can be represented as q + t r for a scalar parameter t. Now suppose we want to intersect this ray with the plane given by the equation p · n = k (where n is an outward-pointing unit normal to the face). The ray intersects the plane when

    (q + t r) · n = k

    That is, when

    q · n + t r · n = k

    and so

    t = (k − q · n) / r · n

    The ray enters the face when r · n is negative and exits when r · n is positive. When r · n is zero, the ray is parallel to the face. You need to check for this case to avoid division by zero; and in this case, if q · n > k, the ray misses the polyhedron completely.

    So find t for all the faces of your polyhedron and let tin be the maximum t for the the points where the ray enters a face, and tout be the minimum t for the points where the ray exits a face. Then the length of the ray within the polyhedron is

    max(max(tout, 0) − max(tin, 0), 0) / |r|

    If r is a unit vector you can avoid the division here. The two inner max(t, 0) are necessary to handle the case where the camera is inside the cube or the cube is behind the camera. The outer max(…, 0) is there to handle the case where the ray misses the cube entirely (in which case tin will be greater than tout).

    I’m afraid I have no idea how to represent this computation in your shader language, let alone how to do so branchlessly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ray, I need to find the closest line segment that it
I have an intent which i need to set it, two flags: FLAG_ACTIVITY_SINGLE_TOP ->
I have an abstract class called camera which PointCamera uses as its super class.
I have read ray's awsome tutorials on apns from there http://www.raywenderlich.com/3443/apple-push-notification-services-tutorial-part-12 http://www.raywenderlich.com/3525/apple-push-notification-services-tutorial-part-2 and i
I have read Ray Wenderlich's awesome tutorials on apple push-notifications from here and here
I have a whole bunch of POV-RAY files from a molecular dynamics simulation with
I have downloaded the demo from the Ray Wenderlich tutorial. Now I am making
Have you ever seen any of there error messages? -- SQL Server 2000 Could
Have just started using Visual Studio Professional's built-in unit testing features, which as I
Have you refactored from an ActiveRecord to a DataMapper pattern? What conditions prompted the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.