Jump to content

How to create a random vector offset by a certain angle

Recommended Posts

Hi folks,

let's get the disclaimer out of the way first: This question is rather technical. I use "the problem" in a rendering circumstance and therefore I thought people here might have the answer - but it could also be used somewhere else. Here we go:


I'm trying - for my own learning - to build a raytracer in VOPs - it has been done before, but I want to build it from scratch to get the most out of it.

Currently I'm stuck on the "diffuse/blurry reflection" of a ray, or adding randomness to the direction of a vector.

Here I couldn't find a good manual on how to do it so I figured it might work with expressing the vector in radial coordinates and then adding a random offset to the angle (and converting it back for further use). Seems cumbersome but so far it was my only solution. Here's what I got:

Picture 1 shows the vector I'd like to randomly transform - let's say by about 10 degrees (+/-5 north-south, +/-5 east-west). Picture 2 shows the result. That's looking rather good. The problem comes when the initial direction of the vector is close to the up-vector (y-Axis in this case) - Picture 3. Picture 4 shows it from the top.

As I use the approach detailed on Scratchapixel (as far as I could follow), I first offset theta, the angle to the up vector and afterwards phi - the angle between x and z. The problem is that if the initial vector is very close to the up vector and the random offset for theta happens to be close to 0, the offset of phi afterwards just turns the vector around itself - not "down" towards the plane of the x/z axis.

Any help is much appreciated - not just on how to solve this, but also for a better approach on how to do this. Thanks a bunch!





Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...