Log in or sign up to comment
Posted by RAStemen

Cool stuff, and your writeup of the difficulty of the issue is was nice too. It reminded me of how much of a pain in the ass it was to try to occlude objects properly when I had to write a program to turn a 2D blueprint into an isometric view. Also, I hadn't ever read about the pixel perfect selection technique you described, so it's nice to learn something new.

Posted by Fobwashed

A short small update. Pretty much what I've been up to today -_-;;

You can select things with a mouse!

Something that should be super damn basic right? Well, you'd think so until you try to implement it yourself. Up until now, I was using the mouse's position on the grid to determine which object should be selected, but this wasn't exactly a great way to go about things since in any given grid, you can have a floor, an object on the floor, 4 possible walls in each direction and an opening (door/window/hole) or wall object on any of those four walls. So with only which grid space the mouse is on, it'd be very difficult to determine which of any number of objects the player wants to interact with. That being the case, being able to just point at an object and have it be selected is critical to ease of use.

Per Pixel Sprite Selection

Initially, I was looking into per pixel collision detection to determine which item would be selected. The method of working this way is to create a fresh render target each frame and drawing all relevant sprites onto this render target as a solid color. Each sprite would be drawn a different color and given an index number. Next, the render target would be casted into a texture file at which point, you can grab the specific pixel at the location of the mouse pointer. Then you can check the pixel's color against the indexed colors of every sprite you've drawn and use that to determine which sprite the mouse is over. It worked. . . but not in a couple important ways that I wanted it to. The most important being that selecting objects behind existing ones became impossible. So say if there was a large object in front of a smaller one, the one in the back is impossible to select. . . So scratch that one.

Rectangles. It always comes back to Rectangles

So, back to the drawing board. Lucky for me, all objects that are selectable already have heights and widths and my previous work with the level editor also stores each objects modified positions. Using these things, I'm able to construct collision rectangles that exist in the game world and using those, I can check against the cursor to determine if an object is being hovered over by the mouse. While not pixel perfect, I got it working to a pretty solid state of usability as shown in the video. Next I had to tackle the same problem I had with the Per Pixel selection. To do this, I ordered the collision checks between the objects and the mouse from closest to the camera to furthest and then based on whether or not the object in front was an object that is in front of something else, tested the object behind as well. If there are no objects behind, then the first object is the one to be selected. So far, it's working reasonably well and I do feel there may be some problems that might arise in the future (especially with huge objects that take multiple spaces), I think I've got a pretty solid foundation to work off of. That's about it for now.