I've created a ray distribution method that generates rays over a fixed angle with an even distribution along the xz plane.

``````/// <summary>
/// Generates rays that are evenly distrbuted in a circular formation around a point but are restricted to an angle
/// </summary>
/// <param name="point">The point around which the rays are generated</param>
/// <param name="forward">The forward vector from which the angle is taken</param>
/// <param name="angle">The angle over which to distribute the rays (radians)</param>
/// <param name="numRays">The number of rays to generate</param>
/// <returns>Generated rays</returns>
public static CollisionDetection.Ray[] RayDistributionCircular(Vector3 point, Vector3 forward, float angle, int numRays)
{
CollisionDetection.Ray[] rays = new CollisionDetection.Ray[numRays];

// Current heading is based on xz plane
float increment = angle / (float)numRays;

Vector3 f = new Vector3();
f.Y = 0f;

if ((numRays & 2) == 0)
{
// Even number of rays

for (int i = 0; i < numRays; ++i)
{

// Create the ray
rays[i] = new CollisionDetection.Ray(point, f);

}
}
else
{
// Odd number of rays

// Calculate the direction based on the current heading
// Use xz plane only

// Create the for the forward direction
rays[0] = new CollisionDetection.Ray(point, f);

int j = 1;
float offset = 0;

for (int i = 1; i < numRays; i += 2)
{
offset = increment * j;

// Create the ray
rays[i] = new CollisionDetection.Ray(point, f);

// Create the ray
rays[i + 1] = new CollisionDetection.Ray(point, f);

j++;
}
}

return rays;
}``````

I'm currently splitting the algorithm into two separate algorithms to deal with either an even or odd number of rays.

- Even rays are distributed across the angle evenly.

- Odd rays are also distributed evenly but require another ray facing the same direction as the forward direction.

Is there any way to combine the two algorithms and always have a ray that points along the original supplied forward vector? The rays must be evenly spaced and constrained to the angle supplied.

Answered by Momerath 1,327 in a post from

You can't achieve your last question. It isn't possible to have an even number of rays that are evenly spaced, with one right in the center. You have to have an odd number to have one in the center.

Answered by ddanbe 2,724 in a post from

Unless you already own a copy this book might be of interest.

## All 5 Replies

You can't achieve your last question. It isn't possible to have an even number of rays that are evenly spaced, with one right in the center. You have to have an odd number to have one in the center.

I've created a ray distribution method that generates rays over a fixed angle with an even distribution along the xz plane.

``````/// <summary>
/// Generates rays that are evenly distrbuted in a circular formation around a point but are restricted to an angle
/// </summary>
/// <param name="point">The point around which the rays are generated</param>
/// <param name="forward">The forward vector from which the angle is taken</param>
/// <param name="angle">The angle over which to distribute the rays (radians)</param>
/// <param name="numRays">The number of rays to generate</param>
/// <returns>Generated rays</returns>
public static CollisionDetection.Ray[] RayDistributionCircular(Vector3 point, Vector3 forward, float angle, int numRays)
{
CollisionDetection.Ray[] rays = new CollisionDetection.Ray[numRays];

// Current heading is based on xz plane
float increment = angle / (float)numRays;

Vector3 f = new Vector3();
f.Y = 0f;

if ((numRays & 2) == 0)
{
// Even number of rays

for (int i = 0; i < numRays; ++i)
{

// Create the ray
rays[i] = new CollisionDetection.Ray(point, f);

}
}
else
{
// Odd number of rays

// Calculate the direction based on the current heading
// Use xz plane only

// Create the for the forward direction
rays[0] = new CollisionDetection.Ray(point, f);

int j = 1;
float offset = 0;

for (int i = 1; i < numRays; i += 2)
{
offset = increment * j;

// Create the ray
rays[i] = new CollisionDetection.Ray(point, f);

// Create the ray
rays[i + 1] = new CollisionDetection.Ray(point, f);

j++;
}
}

return rays;
}``````

I'm currently splitting the algorithm into two separate algorithms to deal with either an even or odd number of rays.

- Even rays are distributed across the angle evenly.

- Odd rays are also distributed evenly but require another ray facing the same direction as the forward direction.

Is there any way to combine the two algorithms and always have a ray that points along the original supplied forward vector? The rays must be evenly spaced and constrained to the angle supplied.

I think need more R&D for ray distribution method for single algoritham

Unless you already own a copy this book might be of interest.

Unless you already own a copy this book might be of interest.

Thanks for the suggestion. I can look in my local library. Which chapter (or page) are you referring too?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.