Help needed with mouse movement.

BlitzMax Forums/BlitzMax Programming/Help needed with mouse movement.

Bukky(Posted 2006) [#1]
Hey guys,

I'm in a bit of a pickle. I have a player object that moves in a grid system where each grid slot is about 50x50 pixels. I'm trying to make the player move left and right between grid slots via mouse control, but it's not working very well. The movement doesnt need to be smooth either, it just needs to move over to the next slot.

Here is my code for that particular functionality:

checkMouseX()

If (mouseXpos - lastmouseXpos%) > 50 Then Player.MoveRight() 
	
If (mouseXpos - lastmouseXpos%) < -49 Then Player.MoveLeft() 

updateMouseX()


Here is some mouse related code:

HideMouse()
Global mouseXpos%
Global lastmouseXpos%

lastmouseXpos% = MouseX()

Function checkMouseX()
	mouseXpos = MouseX()
End Function 

Function updateMouseX()
	lastMouseXpos = mouseXpos
	mouseXpos% = MouseX()
End Function 


The MoveRight() and MoveLeft() methods for the player work fine when using the keyboard.

I guess the main problem is that the movement of the player is too imprecise between the grid slots. It tends to move across too many at once, and sometimes it gets "stuck" in a particular column.

Anyone have any ideas?


LarsG(Posted 2006) [#2]
perhaps you should try to get the mouse coordinates, then divide both of them by 50, and the number before the decimal point will be the gridnumber in the x and y "directions"..
with these values, you can move the player by n number of grids..
(you can ofcourse add an offset to these "coordinates" as well)

I hope it made some sense to you..


Bukky(Posted 2006) [#3]
Hmmm...there isnt a simpler solution?

I dont suppose that Blitz has built in functions akin to onMouseMoveLeft() and onMouseMoveRight()? That's really all I'm trying to do at this point.


Abomination(Posted 2006) [#4]
onMouseMoveLeft() And onMouseMoveRight()

Function onMouseMoveLeft()
If (mouseXpos - lastmouseXpos%) < 0 Then Player.MoveLeft() 
End Function	
Function  onMouseMoveRight()
If (mouseXpos - lastmouseXpos%) > 0 Then Player.MoveRight()
End Function 



Bukky(Posted 2006) [#5]
Hey Abomination,

Yaaa, that gets me closer, but it is too sensitive. Is there any way to get the feeling of a "snap" across the grid? The whole reason that I plugged in the values 50 and -49 was due to an attempt to create this "snap-like" behavior.


SculptureOfSoul(Posted 2006) [#6]
I personally use an approach like Lars' and it works just fine.

Pseudo-code:
Player.X = floor(mouseX()/Gridwidth) * Gridwidth
Player.Y = floor(mouseY()/GridHeight) * GridHeight

As Lars mentioned, you can use an offset or a calculation to determine which gridslot you're in.

Given the above pseudo-code approach, you'd do the following:

Player.GridX = (Player.X - Grid.X) / GridWidth
Player.GridY = (Player.Y - Grid.Y) / GridHeight

Assuming Grid.X and Grid.Y represent the upper left corner of the grid. So, if the grid's upper left corner is at (200,200), and Player.X and Player.Y = point (290, 350), and you have a gridheight and gridwidth of 30 you'd get the following

Player.GridX = (290 - 200) / 30
...90/30 = 3

Player.GridY = (350 - 200)/30
...150/30 = 5

So the players grid position would be (3,5). Of course, this approach provides a 0 based grid array, so (3,5) would actually represent the fourth column and the 6th row.

This particular approach also requires this step to be performed after calculating Player.X and Player.Y, although it could be condensed. I just presented it this way to further clarify the approach.


Bukky(Posted 2006) [#7]
Cool, thanks guys. I'll give this method a try.