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.

DarkBASIC Professional Discussion / Finding pixel perfect 3d location using PICK SCREEN?

Author
Message
Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 12th Sep 2011 22:34
I'm trying to use the PICK SCREEN command to find what tile the user is currently clicking on. The tiles are displayed as a matrix, being 32 by 32 pixels in size each. Using this tutorial (http://forum.thegamecreators.com/?m=forum_view&t=154753&b=22) I was able to get it all working fairly well, with one problem. For my game I need to know exactly where the player is clicking; a difference of one or two pixels will result in them interacting with the wrong block.

Right now the location seems to be offset by a range of 3 to around 6 pixels; blocks near the center of the screen are offset differently than those on the edge of the screen. Here's the code I use to find the X and Z position of the mouse (Y is used for depth):

Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 12th Sep 2011 23:04
Surely mouse X/Y works, because the mouse is moved on the screen, so uses different coordinates?

Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 13th Sep 2011 00:56 Edited at: 13th Sep 2011 01:04
The values for MOUSE X and MOUSE Y are correct. I thought it might be an issue with the distance I set (though 600 - 4 is the exact distance), so I increased it and got the same results. After playing around with it a bit more I did find a discrepancy which could very well be what's causing these issues.

Using the GET PICK VECTOR X/Z commands I found that the values they return are different depending on what side of the screen the mouse is on. For example, when the mouse is moved to the left side the value of the X vector is -373.639, but on the right side it's 368.118 (I rounded these values). Likewise the bottom of the X vector gave me -299.237 and the top gave me 296.573. From my understanding these values should be exact opposites of each other since the camera is in the center of the screen.

This is an issue with PICK SCREEN, but I'm not sure if it's the values I'm entering (though that's not likely, as described above) or how DarkBasic Pro handles the command/calculates the result. Is it even possible to find the exact 3d location the mouse is pointing to?


EDIT: I forgot to mention that I attached a screenshot which shows what the game is currently considering to be the left hand side of the blocks. As you can see it's more accurate at the edge of the screen than the middle. A simple offset by a certain pixel amount isn't enough to fix the problem; somewhere a calculation isn't correct.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 13th Sep 2011 01:55 Edited at: 13th Sep 2011 01:59
If this is exact 2D with no 3D effect, then use the mouse coordinates with the scroll offset instead. So as you scroll the map, add/subtract that value to the mouse position. When the scroll value > 32 set it back to 0 again. So you will move the mouse around, and get a map position from the screen / 32 - scroll offset.

Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 13th Sep 2011 02:54
If I understand correctly, what you're suggesting is that I use my current method to get the mouse's world position, divide that by the size of the tile (32, which I do already in a later portion of the code), and then offset that position by however much the blocks are cut off on the end (meaning if half a block is visible at the very edge of the screen, it would be offset by 16 pixels). Is that correct or did I completely misunderstand what you were suggesting? If it is correct, wouldn't the position still be incorrect since the problem is likely caused by how DarkBasic pro is handling the PICK SCREEN command?
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 13th Sep 2011 03:16 Edited at: 13th Sep 2011 03:19
Then you wouldn't need the pick screen command, you would use the screen size, and Data map. Being as your screen is always a fixed size, and the tiles are always a fixed size, and the mouse pointer is always in the screen coordinates, you have all the maths to get the exact pixels of a 32*32 block. But you have to make sure that your scroll speed divides into 32 properly, or keep track of it.

Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 13th Sep 2011 07:07
I don't think I quite understand how the formula "MOUSEX() / 32 - Scroll_Offset" would give me the world location of where the mouse is pointing. If my world is 100,000 pixels wide, and the screen is currently positioned over pixels 50,000 to 51,000, how would I know where the screen is using the formula above?

When I first started looking into all this I thought it would be easy enough to put together something of my own using the player's position to determine where I was in the world. I added "MOUSEX() - SCREEN WIDTH() / 2" to the player's position which gave me almost the exact same results as using the PICK SCREEN code above, with the exact same problem. The edges of the blocks were offset depending on how far away they were from the center of the screen. Part of the problem may be that the player's Y location (5) is a single pixel higher than the matrix used to display the terrain (4). Maybe I need to account for this somehow?

What I'd be really interested in knowing is what math is going on behind the scenes with PICK SCREEN. Really I'm open to any fix at this point; I've spent waaaaaay too much time on something that should be simple
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 13th Sep 2011 15:31 Edited at: 13th Sep 2011 15:38
Is that posted picture scaled in any way? According to Photoshop your tiles aren't 32*32. They have the same problem on a 32*32 grid as you are describing.



Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 13th Sep 2011 18:20
No, I didn't do any scaling to the picture itself. That said, the matrix I'm using is 1536 x 1536 pixels in size, which represents a total of 48 up and down tiles (tiles being 32 pixels). In order to increase how much the player is able to see I've pulled the camera back by 600. That might be what's causing the effect.

From your screenshot it looks like the right hand side is more or less correct, getting more and more distorted as you move to the left. This is exactly the issue I'm having as well. One thought I had was that it could be an issue with the angle of the camera (would at least explain why it's more distorted as you move from one side to another) but the game returns the correct value of 90 degrees for the X angle.
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 13th Sep 2011 18:40 Edited at: 13th Sep 2011 18:42
Well you could lock all of you objects to the screen. This will give you screen coordinates for objects. Or you could try changing the camera POV, and see if you can get it flat. Or ignore the screen completely, and just use an array of map information for collision. So when you move the mouse 32 pixels the array is updated by 1 map position.

Alaror
15
Years of Service
User Offline
Joined: 9th May 2011
Location:
Posted: 13th Sep 2011 22:00 Edited at: 13th Sep 2011 22:06
I've had a breakthrough, it's working perfectly now! Sven B posted the correct code in this topic: http://forum.thegamecreators.com/?m=forum_view&t=185556&b=1 The issue was that the data wasn't being scaled correctly (the formula for var_Multiplier# was incorrect, and I needed to use 1 for the PICK SCREEN distance).

Here's the final code:


The "+ 5" for the X value and "+ con_Tile_Size + 3" for the Z value were added to fix an offset issue between where everything is displayed in the world and where the mouse coordinates were.

I really, really appreciate the time you took to help me Pincho. After working on this for two days I was about ready to croak haha!

Login to post a reply

Server time is: 2026-07-11 02:55:00
Your offset time is: 2026-07-11 02:55:00