🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

mouse coordinates for entities move

Started by
1 comment, last by Shaarigan 3 years, 11 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.

Advertisement

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