OpenGL 3.0 Window Collision Detection












1















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?










share|improve this question

























  • 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.

    – GoldSpark
    Nov 22 '18 at 3:51
















1















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?










share|improve this question

























  • 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.

    – GoldSpark
    Nov 22 '18 at 3:51














1












1








1


1






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?










share|improve this question
















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?







c++ opengl graphics glsl






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 6:11









Rabbid76

38.2k123249




38.2k123249










asked Nov 22 '18 at 3:40









GoldSparkGoldSpark

65




65













  • 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.

    – GoldSpark
    Nov 22 '18 at 3:51



















  • 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.

    – GoldSpark
    Nov 22 '18 at 3:51

















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.

– GoldSpark
Nov 22 '18 at 3:51





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.

– GoldSpark
Nov 22 '18 at 3:51












1 Answer
1






active

oldest

votes


















0














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-






share|improve this answer


























  • Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

    – GoldSpark
    Nov 22 '18 at 6:06













  • @GoldSpark You're welcome.

    – Rabbid76
    Nov 22 '18 at 6:08











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423548%2fopengl-3-0-window-collision-detection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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-






share|improve this answer


























  • Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

    – GoldSpark
    Nov 22 '18 at 6:06













  • @GoldSpark You're welcome.

    – Rabbid76
    Nov 22 '18 at 6:08
















0














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-






share|improve this answer


























  • Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

    – GoldSpark
    Nov 22 '18 at 6:06













  • @GoldSpark You're welcome.

    – Rabbid76
    Nov 22 '18 at 6:08














0












0








0







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-






share|improve this answer















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-







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 5:55

























answered Nov 22 '18 at 5:49









Rabbid76Rabbid76

38.2k123249




38.2k123249













  • Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

    – GoldSpark
    Nov 22 '18 at 6:06













  • @GoldSpark You're welcome.

    – Rabbid76
    Nov 22 '18 at 6:08



















  • Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

    – GoldSpark
    Nov 22 '18 at 6:06













  • @GoldSpark You're welcome.

    – Rabbid76
    Nov 22 '18 at 6:08

















Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

– GoldSpark
Nov 22 '18 at 6:06







Hey man thank you very much! I totally forgot about the w component of the matrix -.- .. I even compared to the aspect ratio because I didnt know what is going on haha.I was about to do line equations /////

– GoldSpark
Nov 22 '18 at 6:06















@GoldSpark You're welcome.

– Rabbid76
Nov 22 '18 at 6:08





@GoldSpark You're welcome.

– Rabbid76
Nov 22 '18 at 6:08




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53423548%2fopengl-3-0-window-collision-detection%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

"Incorrect syntax near the keyword 'ON'. (on update cascade, on delete cascade,)

Alcedinidae

Origin of the phrase “under your belt”?