how to do an object picking with openTK

Yairk_kaufmann 6 Reputation points
2021-06-12T12:47:50.59+00:00

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);
        }



    }
}
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,843 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.