Hey guys here is the code for the ray cast function my mate sent me, i've followed the steps exactly but i get no output
void view::RayCast( float fRayStartX, float fRayStartY, unsigned int uViewingAngle )
{
float fDX, fDY; // General purpose change in X and Y variables.
int nGridX = 0; // The map array position of the next square intersecting the ray
int nGridY = 0; // The map array position of the next square intersecting the ray
int nTextIndex = 0; // The index into the relevant column of the texture map
// Set the column angle to the angle of first ray with respect to the view.
unsigned int nColumnAngle = m_nViewColAngStart;
// Loop through all columns of pixels in viewport:
for (int nColumn = 0; nColumn < m_nViewWidth; nColumn++)
{
// ***************************************************************************************
// STEP 1: Use the starting position of the ray (fRayStartX, fRayStartY), the viewing
// angle (uViewingAngle) and the column angle for this pixel column (nColumnAngle) to
// calculate the end position (fRayEndX, fRayEndY) of a ray 1024 units long.
// ***************************************************************************************
float fRayEndX;
float fRayEndY;
int RayLength = 1024;
// Avoid the situation where the column angle is 0
if (nColumnAngle == 0) { nColumnAngle++; }
// Calculate the angle of ray in with respect to the world
// Ensure the angle is in the range of 0-4096
int rayAngle = nColumnAngle + uViewingAngle;
if(rayAngle < 0)
{
rayAngle = 0;
}
else
if (rayAngle > 4096)
{
rayAngle = 4096;
}
// Look up the sin and cos of the ray angle in the relevant arrays
fSinArray[rayAngle] = sin(rayAngle*DEG_TO_RAD);
fCosArray[rayAngle] = cos(rayAngle*DEG_TO_RAD);
// Rotate endpoint of a ray 1024 units long by the viewing angle:
fRayEndX = (fSinArray[uViewingAngle] * RayLength) + fRayStartX;
fRayEndY = (fCosArray[uViewingAngle] * RayLength) + fRayStartY;
// Create variables to hold the ray's current position (fRayX, fRayY) and initialise them
// to the start position.
float fRayX = fRayStartX;
float fRayY = fRayStartY;
// ***************************************************************************************
// STEP 2: Using the endpoint of the ray (fRayEndX, fRayEndY) calculate the change in
// x and y (fDRayX and fDRayY) along the length of the ray. Then use these values to calculate
// the slope (fSlope) of the ray (which is also m in the line equation y = mx + c).
// ***************************************************************************************
// Find difference in x,y coordinates along ray:
float fDRayX = fRayEndX - fRayX;
float fDRayY = fRayEndY - fRayY;
/*float fSlope;*/
// Make sure fDRayX is never exactly 0 otherwise you could get divide by zero errors
// Calculate fSlope and make sure that is never exactly 0 either
if(fDRayX == 0)
{
fDRayX++;
}
float fSlope = (fDRayY / fDRayX) - RayLength;
if (fSlope == 0)
{
fSlope++;
}
// Cast ray from grid line to grid line until the loop breaks out
while( true )
{
// ***********************************************************************************
// STEP 3: Calculate the co-ordinates of the intersection points between the ray and
// the next map grid line in both the x and y axis: (fXCrossX, fXCrossY) for the x axis
// and (fYCrossX, fYCrossY) for the y axis.
// ***********************************************************************************
float fXCrossX, fXCrossY; // Intersection point between the ray and the next grid line in the x axis
// The sign of fDRayX tells you the direction of the ray in the x axis which will help you
// to calculate fXCrossX. Once you have fXCrossX you can calculate fXCrossY based on the
// fact that the change in x position (fXCrossX - fRayX) and the slope (fSlope) allows
// you to calculate the change in the y position (fXCrossY - fRayY) for the movement to
// the next x gridline. i.e. dy = m * dx
// Check sign of fDRayX and calculate fXCrossX
if ( fDRayX < 0)
{
fXCrossX = ((fDRayX / 128) *128 -1) /** fCosArray[rayAngle]*/;
}
else
fXCrossX = ((fDRayX / 128) *128 + 128) /** fCosArray[rayAngle]*/;
// Calculate fXCrossY
fXCrossY = fRayY + fSlope * ( fXCrossX - fRayX);
float fYCrossX, fYCrossY; // Intersection point between the ray and the next grid line in the y axis
// The sign of fDRayY tells you the direction of the ray in the y axis which will help you
// to calculate fYCrossX. Once you have fYCrossX you can calculate fYCrossY based on the
// fact that the change in y position (fXCrossY - fRayY) and the slope (fSlope) allows
// you to calculate the change in the x position (fYCrossY - fRayX) for the movement to
// the next y gridline. i.e. dx = dy / m
// Check sign of fDRayY and calculate fYCrossY
if ( fDRayY < 0)
{
fYCrossY = ((fDRayY / 128) * 128 -1)/** fCosArray[rayAngle]*/;
}
else
fYCrossY =((fDRayY / 128) * 128 + 128)/** fCosArray[rayAngle]*/;
// Calculate fYCrossX
fYCrossX = fRayX + ( fYCrossY - fRayY) / fSlope;
// ***********************************************************************************
// STEP 4: Use Pythagoras to calculate the relative distance to each of the grid line
// intersection points(dXDistSq and dYDistSq) No need to use square roots here!
// Double precision is necessary though.
// ***********************************************************************************
// Calculate dXDistSq
float hypX = fXCrossX - fRayX;
float hypY = fYCrossY - fRayY;
float dx = fSinArray[rayAngle] * hypX;
float dy = fCosArray[rayAngle] * hypY;
double dXDistSq = dx * dx;
// Calculate dYDistSq
double dYDistSq = dy * dy;
// ***********************************************************************************
// STEP 5: Depending on which grid line is closer, update the ray position and
// calcuate the map grid position (nGridX, nGridY). Check this map position to for an
// obstruction and break out of the while loop if there is.
// ***********************************************************************************
// If x grid line is closer...
if (dXDistSq < dYDistSq)
{
// Calculate maze grid coordinates of square nGridX and nGridY
nGridX = fXCrossX / 128;
nGridY = fXCrossY / 128;
// Move current ray position to ray intersection point
fRayX = fXCrossX;
fRayY = fXCrossY;
nTextIndex = (int)fXCrossY % 128;
// Is there a maze cube here? If so, stop looping:
if ( m_pViewWorld->m_pWorldTextures[nGridX][nGridY] != NULL)
break;
} else { // If y grid line is closer:
// Calculate maze grid coordinates of square nGridX and nGridY
nGridX = fXCrossX / 128;
nGridY = fXCrossY / 128;
// Move current ray position to ray intersection point
fRayX = fXCrossX;
fRayY = fYCrossY;
nTextIndex = (int) fXCrossX % 128;
// Is there a maze cube here? If so, stop looping:
if ( m_pViewWorld->m_pWorldTextures[nGridX][nGridY] != NULL)
break;
}// End of If Else
}// End of infinate for
Help with Ray Casting function
Re: Help with Ray Casting function
however this is the output i get
- Attachments
-
- raycasting.png
- (87.87 KiB) Downloaded 57 times
Re: Help with Ray Casting function
haha your determined to get this working
What you call a bug...I call a new feature!