Click here to Skip to main content
15,897,371 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
QuestionDetermine straight line Pin
tagopi7-Aug-12 1:04
tagopi7-Aug-12 1:04 
SuggestionRe: Determine straight line Pin
Richard MacCutchan7-Aug-12 1:17
mveRichard MacCutchan7-Aug-12 1:17 
GeneralRe: Determine straight line Pin
tagopi7-Aug-12 1:29
tagopi7-Aug-12 1:29 
GeneralRe: Determine straight line Pin
Amarnath S7-Aug-12 1:18
professionalAmarnath S7-Aug-12 1:18 
GeneralRe: Determine straight line Pin
tagopi7-Aug-12 1:30
tagopi7-Aug-12 1:30 
GeneralRe: Determine straight line Pin
Amarnath S7-Aug-12 1:45
professionalAmarnath S7-Aug-12 1:45 
GeneralRe: Determine straight line Pin
tagopi7-Aug-12 1:49
tagopi7-Aug-12 1:49 
GeneralRe: Determine straight line Pin
Eugen Podsypalnikov7-Aug-12 4:14
Eugen Podsypalnikov7-Aug-12 4:14 
GeneralRe: Determine straight line Pin
Amarnath S7-Aug-12 1:51
professionalAmarnath S7-Aug-12 1:51 
SuggestionRe: Determine straight line Pin
Albert Holguin7-Aug-12 1:40
professionalAlbert Holguin7-Aug-12 1:40 
GeneralRe: Determine straight line Pin
tagopi7-Aug-12 1:50
tagopi7-Aug-12 1:50 
AnswerRe: Determine straight line Pin
pasztorpisti7-Aug-12 5:57
pasztorpisti7-Aug-12 5:57 
AnswerRe: Determine straight line Pin
CPallini7-Aug-12 21:17
mveCPallini7-Aug-12 21:17 
QuestionC++ middleware with GUI in C Pin
Member 83997696-Aug-12 20:02
Member 83997696-Aug-12 20:02 
AnswerRe: C++ middleware with GUI in C Pin
Richard MacCutchan6-Aug-12 20:59
mveRichard MacCutchan6-Aug-12 20:59 
GeneralRe: C++ middleware with GUI in C Pin
Member 83997696-Aug-12 21:58
Member 83997696-Aug-12 21:58 
GeneralRe: C++ middleware with GUI in C Pin
Richard MacCutchan6-Aug-12 22:46
mveRichard MacCutchan6-Aug-12 22:46 
GeneralRe: C++ middleware with GUI in C Pin
JohnAspras6-Aug-12 21:21
JohnAspras6-Aug-12 21:21 
GeneralRe: C++ middleware with GUI in C Pin
Software_Developer6-Aug-12 22:42
Software_Developer6-Aug-12 22:42 
AnswerRe: C++ middleware with GUI in C Pin
pasztorpisti7-Aug-12 11:53
pasztorpisti7-Aug-12 11:53 
GeneralRe: C++ middleware with GUI in C Pin
Member 83997697-Aug-12 19:18
Member 83997697-Aug-12 19:18 
GeneralRe: C++ middleware with GUI in C Pin
pasztorpisti7-Aug-12 21:13
pasztorpisti7-Aug-12 21:13 
GeneralNeed help for a Ray Cast Function Pin
SD1016-Aug-12 8:15
SD1016-Aug-12 8:15 
Hi i have a program to do with ray casting and i am really struggling tryng to code the void view::RayCast function. I have covered the code with comments to help you understand the code, I have looked all oevr the web for help but no success. If there is anyone that is capable of doing this, it would mean the world to me Wink | ;)

C#
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

GeneralRe: Need help for a Ray Cast Function Pin
Wes Aday6-Aug-12 8:20
professionalWes Aday6-Aug-12 8:20 
GeneralRe: Need help for a Ray Cast Function Pin
Software_Developer6-Aug-12 9:02
Software_Developer6-Aug-12 9:02 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.