question

YRK-33345 avatar image
0 Votes"
YRK-33345 asked DanielZhang-MSFT commented

how to do an object picking with openTK

I have a class called Triangle and I want to do a void that checks if the mouse touches the Triangle, how to do that?

the class:

 using System;
 using System.Collections.Generic;
 using System.Linq;
 using System.Text;
 using System.Threading.Tasks;
 using System.Windows.Forms;
 using OpenTK;
 using OpenTK.Graphics.OpenGL;
    
 namespace OPTK
 {
     public class Tringle
     {
         float posx, posy, posz;
         float scx = 0.5f, scy = 0.5f, scz = 0.5f;
         float rotx, roty, rotz, rotQ;
         float colorR = 0.0f, coloG = 1.0f, colorB = 0.0f;
    
         Vector3[] vertex = new Vector3[]
         {
             new Vector3(1f, 1f, 0.0f),
             new Vector3(49f, 1f, 0.0f),
             new Vector3(25f, 49f, 0.0f),
         };
    
         Vector3 vert1 = new Vector3(1f, 1f, 0.0f);
         Vector3 vert2 = new Vector3(49f, 1f, 0.0f);
         Vector3 vert3 = new Vector3(25f, 49f, 0.0f);
    
         public Vector3 position;
    
         public bool MouseHover;
            
    
         public Tringle()
         {
    
         }
    
         public void CollisionDetect()
         {
             if (Cursor.Position.Y < vert2.Y && Cursor.Position.Y > vert2.Y + height() && Cursor.Position.X > vert3.X && Cursor.Position.X < vert3.X + width())
             {
                 MouseHover = true;
             }
             else
             {
                 MouseHover = false;
             }
         }
    
         public void Render()
         {
                
             GL.Translate(posx, posy, posz);
             GL.Rotate(rotx, roty, rotz, rotQ);
             GL.Scale(scx, scy, scz);
    
             GL.Begin(BeginMode.Triangles);
    
             GL.Color3(colorR, coloG, colorB);
             GL.Vertex3(vertex[0]);
             GL.Vertex3(vertex[1]);
             GL.Vertex3(vertex[2]);
    
             GL.End();
         }
    
         public void ChaingePosition(int x, int y, int z)
         {
             posx = x;
             posy = y;
             posz = z;
    
             position = new Vector3(vert1);
         }
    
         public void ChaingeRotation(int x, int y, int z, int q)
         {
             rotx = x;
             roty = y;
             rotz = z;
             rotQ = q;
         }
    
         public void ChaingeScale(int x, int y, int z)
         {
             scx = x;
             scy = y;
             scz = z;
         }
    
         public void ChaingeColor(int r, int g, int b)
         {
             colorR = r;
             coloG = g;
             colorB = b;
         }
    
         public float width()
         {
             return vertex[1].X -= vertex[2].X;
         }
    
         public float height()
         {
             return vertex[1].Y - vertex[0].Y;
         }
    
         public void changeVert1(int x, int y, int z)
         {
             vertex[0] = new Vector3(x,y,z);
         }
    
         public void changeVert2(int x, int y, int z)
         {
             vertex[1] = new Vector3(x, y, z);
         }
    
         public void changeVert3(int x, int y, int z)
         {
             vertex[2] = new Vector3(x, y, z);
         }
    
            
    
     }
 }


dotnet-csharp
· 3
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi @YRK-33345,
>>I want to do a void that checks if the mouse touches the Triangle
What is "a void"? And I tested your code and found a problem with the following code:

 GL.Vertex3(vertex[0]);
 GL.Vertex3(vertex[1]);
 GL.Vertex3(vertex[2]);
    
 position = new Vector3(vert1);

Modified:

 GL.Vertex3(vertex[0].X, vertex[0].Y, vertex[0].Z);
 GL.Vertex3(vertex[1].X, vertex[1].Y, vertex[1].Z);
 GL.Vertex3(vertex[2].X, vertex[2].Y, vertex[2].Z);
    
 position = new Vector3(vert1.X,vert1.Y,vert1.Z);

Please explain in detail what is the specific problem with your code.
Best Regards,
Daniel Zhang


0 Votes 0 ·

Thank you, and I also want to create a function that checks if the mouse thouches the triangle. Can you please help me? @DanielZhang-MSFT

0 Votes 0 ·

Hi @YRK-33345,
First of all, I cannot draw a triangle based on the code you provided. Please provide more relevant code to draw a triangle. Then you can judge whether it is within the triangle according to the coordinates of the mouse. Take your data as an example, when the X axis of the mouse is between 1f and 49f and the Y axis is between 1f and 49f, the Z axis is 0.0f.
Best Regards,
Daniel Zhang

0 Votes 0 ·

0 Answers