I have a voxel game where I want to know which block the players camera is facing. To do this I have a line the distance allowed for the player to interact with the world cast from the center of the camera with its two rotations. How do I divide the line Point : (ROX, ROY, ROZ) to Point : (CPX, CPY, CPZ) by a certain amount of divisions held by a variable named LineDivisions.
Since I don't know how to divide the line i've attempted to cast a ray that changes length in front of the camera. To do this I used the following two formulas.
Rotate in X axis
X' = X
Y' = Ycos(theta) - Zsin (theta)
Z' = Ysin(theta) + Zcos(theta)
Rotate in Y axis
X' = Zsin(theta) + Xcos(theta)
Y' = Y
Z' = Zcos(theta) - Xsin(theta)
I put them in this for loop in this function below.
void DBPB()
{
DB = 0;
DBIZ = 0;
//if(XR < 0){XR = 360;}
//if(XR > 360){XR = 0;}
//if(YR < 0){YR = 360;}
//if(YR > 360){YR = 0;}
//if(ZR < 0){ ZR = 360;}
//if(ZR > 360){ZR = 0;}
//Convert current rotation values into Radian
XRa = (RayXr / 180 * PI);
YRa = (RayYr / 180 * PI);
//ZRa = (ZR / 180 * PI);
//The origin is set to zero and the line that is in the correct axis for the camera is added.
TDX = 0;
TDY = 0;
TDZ = RayLength;
//Raycast
for(RayLength = 0; RayLength > -7; RayLength -= .1)
{
//Rotate in X axis
ROX = TDX;
ROY = (TDY*cos(XRa)) - (TDZ*sin(XRa));
ROZ = (TDY*sin(XRa)) + (TDZ*cos(XRa));
//Rotate in Y axis
RTX = (ROZ*sin(YRa)) + (ROX*cos(YRa));
RTY = ROY;
RTZ = (ROZ*cos(YRa)) - (ROX*sin(YRa));
//Rotate in Z axis(Not used)
//X = X*cos(theta) - y*sin(theta)
//Y = X*Sin(theta) + y*cos(theta)
//Z = Z
//std::cout << "Rotated X :" << RTX << " Y:" << RTY << " Z :" << RTZ << std::endl;
CPX = static_cast<int>(RTX+PX);
CPY = static_cast<int>(RTY+PY);
CPZ = static_cast<int>(RTZ+PZ);
//When not inside a block in creative
//This will hold the position of the block
//before the selected block.
if(DB == 0 && DBIZ == 0)
{
PBX = CPX;
PBY = CPY;
PBZ = CPZ;
}
if(Blocks[CPX][CPY][CPZ] != 0 && Blocks[CPX][CPY][CPZ] != NULL && DB == 0)
{
//Toggle bool DB
std::cout << "Collided X :" << CPX << " Y:" << CPY << " Z :" << CPZ << std::endl;
//Detected Collision at BBX, BBY, BBZ
BBX = CPX;
BBY = CPY;
BBZ = CPZ;
RayLength = -7;
DB = 1;
DBIZ = 1;
}
}
}
The vertex its at jumps in circles but sometimes looks right, Does anyone have resources I can read about doing this?