#Strategy to determine ray length

13 messages · Page 1 of 1 (latest)

worn kindle
#

I'm working on a ray project, so far I was able to draw rays, but now I have to somehow determine their individual lengths and I can't come up with a strategy on my own.

My Ray has an origin (vector x, y) and a direction (unit vector dx, dy). It's a graphics application, so I'm confined by screen space (1024, 768).

How do I go from Point x, y and direction dx, dy to a length, with with I can multiply dx and dy to obtain the end coordinate for my line (ray)?

signal pineBOT
worn kindle
#

I attached a screenshot, right now I just multiply the unit vector by 200 pixels, that's why all rays are 200 pixels long

#

If code helps, it's very bare bones because I'm completely stuck

void RenderDrawRays(SDL_Renderer *ren, Ray rays[], int num_rays) {
    for (int i = 0; i < num_rays; i++) {
        Ray ray = rays[i];

        int ex = ray.x + ray.dx * 200;
        int ey = ray.y + ray.dy * 200;

        SDL_RenderDrawLine(ren, ray.x, ray.y, ex, ey);
    }
}
#

This calculation happens prior, here I create the rays x, y, dx, dy

void circle_rays(Circle circle, Ray rays[], int num_rays) {
    for (int i = 0; i < num_rays; i++) {
        double theta = ((double)i / num_rays) * 2 * M_PI;
        double dx = cos(theta);
        double dy = sin(theta);

        int x = circle.x + circle.r * dx;
        int y = circle.y + circle.r * dy;

        Ray ray = {x, y, dx, dy};
        rays[i] = ray;
    }
}
#

But I think my problem is entirely math related, that's why I posted it here

#

Thinking about this, I might have to approach this differently. Because a ray grows, right? So maybe I should start increasing it's length and then check if it collides with something

sturdy sky
#

You should look into ray sphere intersection

sturdy sky
#

Ray sphere intersections basically gives a value which tells you how long your ray is until it intersects a sphere

worn kindle
sturdy sky
#

I can't tell whether you are using ray marching, do you have a ray sphere/circle intersection function or a sign distance function?

#

I suggest you to use ray circle intersection function