1
votes

Can someone tell me how to make triangle vertices collide with edges of the screen? For math library I am using GLM and for window creation and keyboard/mouse input I am using GLFW. I created perspective matrix and simple array of triangle vertices.

Then I multiplied all this in vertex shader like:

gl_Position = projection * view * model * vec4(pos, 1.0);

Projection matrix is defined as:

glm::mat4 projection = glm::perspective(
    45.0f, (GLfloat)screenWidth / (GLfloat)screenHeight, 0.1f, 100.0f);

I have fully working camera and projection. I can move around my "world" and see triangle standing there. The problem I have is I want to make sure that triangle collide with edges of the screen.

What I did was disable camera and only enable keyboard movement. Then I initialized translation matrix as glm::translate(model, glm::vec3(xMove, yMove, -2.5f)); and scale matrix to scale by 0.4.

Now all of that is working fine. When I press RIGHT triangle moves to the right when I press UP triangle moves up etc... The problem is I have no idea how to make it stop moving then it hits edges.

This is what I have tried:

triangleRightVertex.x is glm::vec3 object.

0.4 is scaling value that I used in scaling matrix.

if(((xMove + triangleRightVertex.x) * 0.4f) >= 1.0f)
{
 cout << "Right side collision detected!" << endl; 
}

When I move triangle to the right it does detect collision when x of the third vertex(bottom right corner of triangle) collides with right side but it goes little bit beyond before it detects. But when I tried moving up it detected collision when half of the triangle was up.

I have no idea what to do here can someone explain me this please?

1
My solution is to create lines as window frame and check if point is in the line using line equation. I don't know if that is good. - user5712191

1 Answers

0
votes

Each of the vertex coordinates of the triangle is transformed by the model matrix form model space to world space, by the view matrix from world space to view space and by the projection matrix from view space to clip space. gl_Position is the Homogeneous coordinate in clip space and further transformed by a Perspective divide from clip space to normalized device space. The normalized device space is a cube, with right, bottom, front of (-1, -1, -1) and a left, top, back of (1, 1, 1).
All the geometry which is in this (volume) cube is "visible" on the viewport.

In clip space the clipping of the scene is performed.
A point is in clip space if the x, y and z components are in the range defined by the inverted w component and the w component of the homogeneous coordinates of the point:

-w <=  x, y, z  <= w

What you want to do is to check if a vertex x coordinate of the triangle is clipped. SO you have to check if the x component of the clip space coordinate is in the view volume.

Calculate the clip space position of the vertices on the CPU, as it does the vertex shader. The glm library is very suitable for things like that:

glm::vec3 triangleVertex = ... ; // new model coordinate of the triangle
glm::vec4 h_pos = projection * view * model * vec4(triangleVertex, 1.0);

bool x_is_clipped = h_pos.x < -h_pos.w || h_pos.x > h_pos.w;

If you don't know how the orientation of the triangle is transformed by the model matrix and view matrix, then you have to do this for all the 3 vertex coordinates of the triangle-