Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Raycasting Fisheye Error  (Read 811 times)
0 Members and 1 Guest are viewing this topic.
Offline Borkpaladin
« Posted 2013-12-19 09:10:30 »

For a bit of fun I decided to create a 2.5D raycasting engine, it works well, however when I attempt to remove the fisheye effect with Math.cos in this line of code:
1  
dist * Math.cos(Math.toRadians((angleOfRay * angleBetweenRays) - (fov / 2.0)));

It causes a weird distortion in the height of the walls, it appears to slightly over compensate for the fisheye effect.

The entire class is accessible here: https://www.dropbox.com/s/nuhol52mm95lm7p/Source.txt

Screenshots:


If this error is totally unsolvable from the information provided, would anyone be able to show me any good raycasting tutorials besides this one http://www.permadi.com/tutorial/raycast/ ?
Offline jmguillemette
« Reply #1 - Posted 2013-12-20 18:47:16 »

there is a great write up on why this happends in raycasting..

the good news is you are doing it correctly if your getting this.
the bad news is you need to implement a correction prior to drawing if you want a nice square image.

If i find the write up ill post it here later tonight.

If i recall properly you need to adjust the height of everything to a constant to correct the fisheye effect.

-=Like a post.. give the author a medal!=-
Offline Borkpaladin
« Reply #2 - Posted 2013-12-22 07:45:17 »

Thanks for your help, it would be great if you were able to find that article.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Borkpaladin
« Reply #3 - Posted 2013-12-23 10:10:51 »

I found the error, it was actually due to the inaccurate method in which I detected the collision of a ray with a wall
Just in case any one else encounters the same error, here is the corrected code snippet, (it'll probably be incomprehensible, but it's better than nothing).

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
boolean rayHitHorizontalSide= hitHorizontalSide(x2, y2, sourceX, sourceY);
            if (rayHitHorizontalSide) {
               double rise =(Math.floor(y2/wallSize)*wallSize - sourceY);
               double run = rise/vy * vx;
               dist = Math.sqrt(run*run + rise*rise);
               
               rise =(Math.floor(y2/wallSize)*wallSize+wallSize - sourceY);
               run = rise/vy * vx;
               if(Math.sqrt(run*run + rise*rise) < dist)
                  dist = Math.sqrt(run*run + rise*rise);
            } else {
               double run = (Math.floor(x2/wallSize)*wallSize - sourceX);
               double rise = run/vx * vy;
               dist = Math.sqrt(run*run + rise*rise);
               
               run = (Math.floor(x2/wallSize)*wallSize+wallSize - sourceX);
               rise = run/vx * vy;
               if(Math.sqrt(run*run + rise*rise) < dist)
                  dist = Math.sqrt(run*run + rise*rise);
            }
Offline Riven
« League of Dukes »

JGO Overlord


Medals: 816
Projects: 4
Exp: 16 years


Hand over your head.


« Reply #4 - Posted 2013-12-23 18:07:15 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
             boolean rayHitHorizontalSide= hitHorizontalSide(x2, y2, sourceX, sourceY);
             if (rayHitHorizontalSide) {
                double rise =(Math.floor(y2/wallSize)*wallSize - sourceY);
                double run = rise/vy * vx;
-               dist = Math.sqrt(run*run + rise*rise);
+               dist2 = (run*run + rise*rise);
               
-               rise =(Math.floor(y2/wallSize)*wallSize+wallSize - sourceY);
+               rise += wallSize;
                run = rise/vy * vx;
-               if(Math.sqrt(run*run + rise*rise) < dist)
+               if((run*run + rise*rise) < dist2)
                   dist2 = (run*run + rise*rise);
             } else {
                double run = (Math.floor(x2/wallSize)*wallSize - sourceX);
                double rise = run/vx * vy;
-               dist = Math.sqrt(run*run + rise*rise);
+               dist2 = (run*run + rise*rise);
               
-               run = (Math.floor(x2/wallSize)*wallSize+wallSize - sourceX);
+               run += wallSize;
                rise = run/vx * vy;
-               if(Math.sqrt(run*run + rise*rise) < dist)
+               if((run*run + rise*rise) < dist2)
                   dist2 = (run*run + rise*rise);
             }
+            dist = Math.sqrt(dist2);

You might very well get rid of most, if not all square root calculations.

Hi, appreciate more people! Σ ♥ = ¾
Learn how to award medals... and work your way up the social rankings
Offline Borkpaladin
« Reply #5 - Posted 2013-12-28 05:33:28 »

Thanks, I'll remember to do that in future projects too.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (42 views)
2014-10-16 15:22:06

Norakomi (32 views)
2014-10-16 15:20:20

lcass (37 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (66 views)
2014-10-14 00:35:47

TehJavaDev (59 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45

BurntPizza (85 views)
2014-10-11 22:30:10
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!