Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculating block/face camera/cursor has focus on in 3d block world

Tags:

java

3d

Been teaching myself 3d programming with a minecraft-clone game. I have an infinite map, loaded in chunks of 16x16x64 blocks.

As the player (the camera) walks around, the center of the camera (the game cursor) points at a block. I'm trying to figure out how to determine which block the user is pointing at.

I have a camera with a 3d coordinate, yaw, pitch, so I know where the user is looking.

I've tried finding coordinates that would be on a "line" drawn from that origin point but that doesn't account for when the camera points at the edges/corners of a block, the system won't know.

I've tried looking for examples online but I'm not finding anything useful, a few examples but they're extremely buggy and poorly documented.

How can I properly convert where the center of the camera is looking into which block/face it's looking at?

like image 720
helion3 Avatar asked Nov 30 '13 19:11

helion3


2 Answers

"I've tried finding coordinates that would be on a "line" drawn from that origin point but that doesn't account for when the camera points at the edges/corners of a block, the system won't know." .. "How can I properly convert where the center of the camera is looking into which block/face it's looking at?"

The two common ways that OpenGL programs do mouse picking is either colour picking or ray casting. It sounds like you are trying to iterate through your block grid to get the position which is a bit unusual, or want to do something like ray casting.

Ray casting is done from the camera -> through the mouse -> onto a plane on the screen -> map screen space to world space -> collide with scene

"I've tried looking for examples online but I'm not finding anything useful, a few examples but they're extremely buggy and poorly documented."

Here are some links to some ray casting tutorials one with maths and diagrams, one with some code examples, and another.

Note that if you find any older OpenGL tutorials, they might mention "selection mode" or the "name stack" which is really old, don't use it - OpenGL wiki.

like image 164
Jacqui Gurto Avatar answered Nov 07 '22 02:11

Jacqui Gurto


There is a Minecraft type clone called CraftMania that is open source at https://github.com/mcourteaux/CraftMania

There might be some documented information about 3D picking.

like image 1
JustBrenkman Avatar answered Nov 07 '22 00:11

JustBrenkman