View more

View more

View more

### Image of the Day Submit

IOTD | Top Screenshots

### The latest, straight to your Inbox.

Subscribe to GameDev.net Direct to receive the latest updates and exclusive content.

# Detecting mouse in box? In Blitz Plus.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

2 replies to this topic

### #1nicksaiz65  Members

Posted 26 July 2012 - 07:21 PM

I am trying to make a tile map editor in Blitz Plus. To do this, I divided the screen up into a grid. However, I am having trouble actually telling where the mouse is. I have tried a couple methods, but none of them worked. Each tile is 40 pixels high and 40 pixels wide.

First we would actually draw the grid using this code:

Function DrawBoxes()
;draw the horizontal rectangles
For horiz = 40 To 600 Step 40 ;-----------
Rect 0,horiz,800,5,1
Next

;draw the vertical rectangles
For verti = 40 To 800 Step 40
Rect verti,0,5,600,1
Next

End Function

Here's my first method for detecting where the mouse is:
For columns = 40 To 800 Step 40 ;we will actually be using the coordinate positions for this
For rows = 40 To 600 Step 40
If MouseX() < (rows + 20) And MouseX() > (rows - 20) ;if it is inside the box horizontally ;we are testing this from the center of the box
If MouseY() < (columns + 20) And MouseY() > (columns - 20) ;if it is inside the box vertically
Color 255,0,0
Rect rows,columns,40,40,1 ;draw a red box so the user knows it is selected
EndIf
EndIf
;EndIf
Next
Next

However, this method lets the user select multiple boxes at once, and if you decrease the size of the hitbox, it barely works at all.

Method 2:
For columns = 40 To 800 Step 40 ;we will actually be using the coordinate positions for this
For rows = 40 To 600 Step 40
If RectsOverlap(rows,columns,25,25,MouseX(),MouseY(),12,21) ;use a smaller hitbox for the rectangle
Color 255,0,0
Rect rows,columns,40,40,1
Endif
Next
Next

However, this method barely works either, and if you increase the size of the hitbox, it lets the user select multiple boxes at once.

Any ideas on how to tell where the mouse is in multiple boxes that are right beside each other? Thanks.
I am the man who will become the pirate king!

### #2prh99  Members

Posted 30 July 2012 - 02:53 AM

The mouse is in box MouseX() / 40 and MouseY() / 40 you'll have to add in the offset if your grid's start is something other than 0,0. You can can get the screen coordinates of the upper left hand corner of square the mouse is by multiplying the result of the previous operation by 40 the adding in any offset for where your grid begins. Hope that helps and I haven't completely misunderstood(4am here lol)

You could also keep track of the box information in an array, storing the x,y of the upper left corner and check the mouse position against the array when needed, say a click.
Patrick

### #3eFoDay  Members

Posted 30 July 2012 - 11:44 AM

Here is a way that I use to find where the mouse is located in the grid:

Vector2f getGridLoc(int tileSize)
{
Vector2f gridLoc;
gridLoc.x = std::floor(mouse.x / tileSize);
gridLoc.y = std::floor(mouse.y / tileSize);
return gridLoc;
}

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.