[英]Intersection of mouse cursor and projected 2D rectangle
There is a bunch of 2x2 "floor/tiles" in a 3D-world with JOGL, they are projected into the camera and I want to know the cursor is hovering the tile or not. 在使用JOGL的3D世界中,有一堆2x2的“地板/瓷砖”,它们被投影到相机中,我想知道光标是否在瓷砖上徘徊。
I have a camera settings like this: 我有这样的相机设置:
glu.gluPerspective(90, 4.0/3.0, 1, 100);
glu.gluLookAt(-2f, 8f, -2f, 0f, 0f, 0f, 0f, 1f, 0f);
and there are some tiles or blocks on the y=0 pane like this 并且在y = 0窗格上有一些像这样的图块或图块
gl.glPushMatrix();
{
// Camera settings (same as above)
glu.gluPerspective(90, 4.0/3.0, 1, 100);
glu.gluLookAt(-2f, 8f, -2f, 0f, 0f, 0f, 0f, 1f, 0f);
// Draw the tiles
gl.glPushMatrix();
{
gl.glBegin(GL2.GL_POLYGON);
{
// a bunch of translated and textured
// (1,0,1) (1,0,-1) (-1,0,-1) (-1,0,1)
// rectangle here,
}
gl.glEnd();
}
gl.glPopMatrix();
}
gl.glPopMatrix();
I am new in 3D and I am only familiar with Java Graphics2D. 我是3D的新手,并且只熟悉Java Graphics2D。 Intersection of 2D rectangle and cursor is just a few easy comparison, but it seems to be a lot more complicated in 3D. 2D矩形和光标的交点只是几个简单的比较,但是在3D中似乎要复杂得多。 I am looking for some Maths or library to do this. 我正在寻找一些数学或图书馆来做到这一点。
Or if there is a method to get the 4 point of the final pixels on the screen, Maybe I would like to do java.awt.Shape contain() and check it intersects or not. 或者,如果有一种方法可以获取屏幕上最终像素的4点,也许我想做java.awt.Shape contains()并检查它是否相交。
The result will be like this: 结果将是这样的:
Maybe the simplest solution is using the gluProject(); 也许最简单的解决方案是使用gluProject();。 to get the screen coordinate, and using java.awt.geom.Path2D to check the mouse coordinate is in the area or not. 获取屏幕坐标,并使用java.awt.geom.Path2D检查鼠标坐标是否在该区域中。
Here is a simple sample code: 这是一个简单的示例代码:
// do inside the matrix stack? of rendering rectangles to get the right matrix we want
float[][] FinalCoordinate = new float[4][4];
float[] ModelView = new float[16];
float[] Projection = new float[16];
gl.glGetFloatv(GL2.GL_MODELVIEW_MATRIX,ModelView,0);
gl.glGetFloatv(GL2.GL_PROJECTION_MATRIX,Projection,0);
for(int x = 0; x < 4; x++)
{
glu.gluProject(Vertex[x][0],0f,Vertex[x][2],ModelView,0,Projection,0,new int[]{0,0,800,600},0,FinalCoordinate[x],0);;
}
after getting four points of the final coorindates, use Path2D to check the intersection: 得到最终坐标的四个点后,使用Path2D检查相交:
Path2D p = new Path2D.Float();
p.moveTo(FinalCoordinate[0][0],FinalCoordinate[0][1]);
for(int x = 1;x<4;k++)
{
p.lineTo(fc[x][0],fc[x][1]);
}
p.closePath();
boolean Result = p.contains(MouseX, MouseY);
thats it! 而已! Thanks those suggestions and links :) 谢谢那些建议和链接:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.