Well, I am creating a Minecraft like engine in XNA, and since I first implemented the block selector (the cube that allows you to select blocks in 3D space) it has always been very inaccurate. Here is what I mean:
And here is the code that does the selecting (note that I have tryed to ceil then floor the vectors but none correct the abnormal positioning of it):
private Block GetSelectedBlock()
{
Block block = new Block();
block.BlockType = BlockType.none;
for (float x = Variables.SELECTION_MIN; x < Variables.SELECTION_MAX; x += 0.1f)
{
Vector3 targetPoint = camera.Position + (camera.LookVector * x);
//targetPoint = Util.FloorVector3(targetPoint);
//targetPoint = Util.CeilVector3(targetPoint);
blockSelector.UpdatePosition(new Vector3((int)targetPoint.X, (int)targetPoint.Y, (int)targetPoint.Z));
block = world.GetBlock((int)targetPoint.X, (int)targetPoint.Y, (int)targetPoint.Z);
if (block.BlockType != BlockType.none)
{
blockSelector.Visible = true;
break;
}
blockSelector.Visible = false;
}
return block;
}
Also the two following screen shots show where the block selection cube is placed when I do call the blockSelector.UpdatePosition()
function but do not cast the values to integers:
Any ideas what I am doing incorrectly?
Also regarding Blecki's answer here is what I implemented based on his idea:
private Block GetSelectedBlock()
{
Block block = new Block();
block.BlockType = BlockType.none;
for (float x = Variables.SELECTION_MIN; x < Variables.SELECTION_MAX; x += 0.1f)
{
Vector3 targetPoint = camera.Position + (camera.LookVector * x) + new Vector3(0.5f, 0.5f, 0.5f);
targetPoint = Util.FloorVector3(targetPoint);
blockSelector.UpdatePosition(new Vector3((int)targetPoint.X, (int)targetPoint.Y, (int)targetPoint.Z));
block = world.GetBlock((int)targetPoint.X, (int)targetPoint.Y, (int)targetPoint.Z);
if (block.BlockType != BlockType.none)
{
blockSelector.Visible = true;
break;
}
blockSelector.Visible = false;
}
return block;
}
It seems to allow you to place blocks alot more accurately but it is still off centre: