Jump to content
  • Advertisement
Sign in to follow this  
FFA702

lodev raycaster C# simple implementation issue

This topic is 1484 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am doing a very simple implementation of the lodev raycaster in C#, however, i have some technical issues. First, i can't get the collision right, each time i am supposed to collide i either 1- get a division by 0 at:

                    //Calculate height of line to draw on screen
                    int h = ydisplay;
                    int lineHeight = Math.Abs(h / (int)(perpWallDist));

or i get an out of range exception on the array i am not supposed to escape because of the same collision code:

if (e.KeyChar == 'w')
            {
                if (worldmap[Convert.ToInt32((posX + dirX * moveSpeed)), Convert.ToInt32((posY))] == 0) posX += dirX * moveSpeed;
                if (worldmap[Convert.ToInt32((posX)), Convert.ToInt32((posY + dirY * moveSpeed))] == 0) posY += dirY * moveSpeed;
            }
            if (e.KeyChar == 's')
            {
                if (worldmap[Convert.ToInt32((posX - dirX * moveSpeed)), Convert.ToInt32((posY))] == 0) posX -= dirX * moveSpeed;
                if (worldmap[Convert.ToInt32((posX)), Convert.ToInt32((posY - dirY * moveSpeed))] == 0) posY -= dirY * moveSpeed;
            }

Secondly, my world appears rough and wrong. Its weird because my code is almost an exact copy of the lodev one. Can anyone point the issues ? Pictures attached.

 

Also the code is from this tutorial: odev.org/cgtutor/raycasting.html

 

Thanks in advance !

[attachment=23922:2.png][attachment=23923:3.png][attachment=23921:1.png]

Share this post


Link to post
Share on other sites
Advertisement
what max value does 'moveSpeed' have ? (I assume DirX and DirY are normalized)?


lineHeight = Math.Abs(h / (int)(perpWallDist));[/quote]
1. why the abs? can you have a negative height or distance?
2. what is your expected behaviour if you are <1unit in distance to the wall? wouldn't it be better to divide first and then case the final result to int? that should also lead to smoother visuals (otherwise your walls that are somewhere between 1 und 2 units in distances are in general divided by 1 and next ones are divided by 2 which is half the size, that's not really a smooth transition.

Share this post


Link to post
Share on other sites

Fixed the graphic issue, i was rounding the wrong item of Math.Abs(h / (int)(perpWallDist)), i ended up rounding the whole result instead of the distance. For the collision issue it seems to be working half the time. Of course empty grid are marked by 0, so the code checks if the mapgrid is empty, if it is it execute the movement. 

Share this post


Link to post
Share on other sites

Fixed the collision problem too gosh i can't believe C# is so weird, apparently Convert.ToInt32() is massively different than (int) because it gives drastically different result.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!