Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Finding the World Position of Mouse (2d)

Author
Message
Alaror
13
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 11th Sep 2011 23:37
In my 2d, side view game my terrain is displayed using a matrix. I'm having difficulty figuring out the position of the mouse in the world. Because my world is not displayed using objects I can't make use of the PICK OBJECT command; I need a mathematically based way to find the exact location of the mouse, which is then used to determine what block the mouse is currently over.

The data I have to work with is the mouse's position on the screen (using MOUSEX) and the player's world position. I thought it would be enough to offset the MOUSEX position by half of the screen width (making 0 the exact center of the player, since they're located in the middle) and then add that to the player's world position, but evidently not. What's most bizarre is that there's no immediately apparent rhyme or reason for what the game is considering to be the end of one block and the start of another. In the screenshot I attached I've drawn lines that show approximately where the game says a block is starting and finishing for three grey blocks, marked by a red line. As you can see it varies quite a bit.

I'm open to suggestions for other, more accurate ways to find the mouse world position, or thoughts on ways to fix it using my current method.

Attachments

Login to view attachments
LastChance
13
Years of Service
User Offline
Joined: 11th Sep 2011
Location:
Posted: 12th Sep 2011 00:55
Algorithm...

let say 1 block is 32 pixels wide and 32 pixels high.
if we assume that display resolution is 1024x768,
then we can draw:
1024/32 = 32 blocks per row
768/32 = 24 blocks per column

so we have to draw 24x32 = 768 blocks every time we redraw the screen.
we can store them in 1D array of type block:



to draw the map we'll use the function draw_map()



to find which block mouse points to we use:



use that in a loop:



with that index you can access the exact block in the Map array and modify it, check for collision etc.

P.S. I think that draw_map will raise out of bonds error. If this happen try to add 1 more element to the array.
Webber
13
Years of Service
User Offline
Joined: 27th May 2011
Location:
Posted: 12th Sep 2011 01:09
Finding the mouse positions would be a lot easier if you were using sprites instead.

Assuming that the height of the camera does not change during game play, the simplest solution would be to adjust the cameras height to make the world fit the size of the screen.

Example code of how to get the distance of the camera and the world to fit the screen resolution.


Example of how to get the positions of the mouse in the world using the cameras x, and z positions.


Again, using sprites would be easier, and more importantly the program will run faster too.

I like games, and stuff.
Alaror
13
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 12th Sep 2011 06:39
Thanks for your responses. I ended up using this tutorial on the PICK SCREEN command: http://forum.thegamecreators.com/?m=forum_view&t=154753&b=22 There's some tweaking that needs to be done but I'm pretty much happy with the results.

Login to post a reply

Server time is: 2024-11-22 12:28:46
Your offset time is: 2024-11-22 12:28:46