Camilo
Camilo

Reputation: 309

X,Y position of semi cylinder - ray - triangle- intersection to space [-1,1] [-1,1]

I am rendering a tile map to a fbo and then moving the resulted buffer to a texture and rendering it on a FSQ. Then from the mouse click events, I got the screen coordinates and move them to clip space [-1,1]:

    glm::vec2 posMouseClipSpace((2.0f * myCursorPos.x) / myDeviceWidth - 
                 1.0f, 1.0f - (2.0f *  myCursorPos.y) / myDeviceHeight);

I have logic on my program that based on those coordinates, it selects a specific tile on the texture.

enter image description here

Now, moving to 3D, I am texturing a semi cylinder with the FBO I used in the previous step:

enter image description here

In this case I am using a ray-triangle intersection point that hits the cylinder with radius r and height h. The idea is moving this intersection point to space [-1,1] so I can keep the logic on my program to select tiles

I use the Möller–Trumbore algorithm to check points on the cylinder hit by a ray. Lets say the intersected point is (x,y) (not sure if the point is in triangle, object or world space. Apparently it's worldspace). I want to translate that point to space x:[-1,1], y[-1,1].

I know the height of my cylinder, which is a quarter of the cylinder's arc length:

cylinderHeight = myRadius * (PI/2);

so the point in the Y axis can be set in [-1,1]space:

vec2.y = (2.f * (intersectedPoint.y - myCylinder->position().y) ) / 
         (myCylinder->height()) - 1.f

and That works perfectly.

However, How to compute the horizontal axis which depends on 2 variables x and z?

Currently, my cylinder's radius is 1, so by coincidence a semi cylinder set in the origin would go from (-1 ,1) on the X axis, which made me think it was [-1,1] space, but it turns out is not. My next approach was using the arc length of a semi circle s =r * PI and then plug that value into the equation:

vec2.x = (2.f * (intersectedPoint.x - myCylinder->position().x) ) / 
             (myCylinder->arcLength()) - 1.f

but clearly it goes off by 1 unit on the negative direction.

I appreciate the help.

Upvotes: 1

Views: 206

Answers (1)

meowgoesthedog
meowgoesthedog

Reputation: 15035

From your description, it seems that you want to convert the world space intersection coordinate to its corresponding normalized texture coordinate.

For this you need the Z coordinate as well, as there must be two "horizontal" coordinates. However you don't need the arc length.

Using the relative X and Z coordinates of intersectedPoint, calculate the polar angle using atan2, and divide by PI (the angular range of the semi-circle arc):

vec2.x = atan2(intersectedPoint.z - myCylinder->position().z,
               myCylinder->position().x - intersectedPoint.x) / PI;

Upvotes: 1

Related Questions