Need Help from a good C++ Programmer ASAP

Hi guys I have a program to do with Ray Casting and i am having difficultly implementing the void view::RayCast() Function, I have looked all over the internet for help but havent succeded, i've looked at the mathematics behind the function. I have covered the function in comments so its easy to read the function and see what it does, I got it down into 5 keys Steps. If there is anyione out there who is capable of doing this it would mean a lot to me :)

Thanks Guys
Here is the function code

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.
// ***************************************************************************************

// 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


// Look up the sin and cos of the ray angle in the relevant arrays


// Rotate endpoint of a ray 1024 units long by the viewing angle:


// 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:


// 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


// 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


// Calculate fXCrossY


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 fXCrossX


// Calculate fXCrossX


// ***********************************************************************************
// 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
double dXDistSq = 0.0;

// Calculate dYDistSq
double dYDistSq = 0.0;

// ***********************************************************************************
// 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


// Move current ray position to ray intersection point


// 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


// Move current ray position to ray intersection point


// 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



closed account (o1vk4iN6)
Well first, why not implement a vector/point class of some sort, so instead of passing, RayStartX and RayStart, you just pass one variable.

Make it as simple as:

1
2
3
4
5
6
7
8
9
10

class Vec2
{

public:
     float x, y;


};


Adding on functions as needed.

Also you have almost no code. If you have comments why not implement it yourself ? I'd also avoid using infinite loops as you have there, you can create a much more understandable loop by including the condition in the while statement.

Also use [ code ] [/ code ] blocks to add syntax highlighting.
Last edited on
Topic archived. No new replies allowed.