Advertisement

mouse coordinates for entities move

Started by July 07, 2020 12:25 AM
1 comment, last by Shaarigan 4 years, 4 months ago

I am making a 2D map editor, I am using OpenGL with GLFW, GLEW and GLSL. I want to select the entities and drag them with the mouse. I compare the position of the mouse with the 4 edges of the entity, but my method is not precise, especially when I move the camera. Which formula o method i can use for this?

PD: Sorry for bad english.

It should be precise enougth to detect what element is udner the mouse. How is your matrix constructed, is it a projection or orthogonal matrix?

If you initialized a projection matrix then you need to transform the coords of your 2D stuff into orthogonal window coords before you can use them. Then use a rectangle intersection test with your mouse position and camera offset to find objects in the editor.

        ///  
        /// 
        ///     Determines if this rectangle intersets with rect. 
        /// 
        public bool IntersectsWith(Rectangle rect) {
            return(rect.X < this.X + this.Width) &&
            (this.X < (rect.X + rect.Width)) && 
            (rect.Y < this.Y + this.Height) &&
            (this.Y < rect.Y + rect.Height); 
        } 

(From C# Rectangle.cs)

This topic is closed to new replies.

Advertisement