This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2012-04-13 01:10:37

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

help with raycaster

My first raycaster found here. When you play it, it looks like you're in a curved room, but the map is a perfectly straight square. Can someone explain to me why? I tried to make the scripts as simple as possible (I don't know how else to do it since I didn't look at a tutorial), so it shouldn't be all that hard to read. Just know that I made 4 scanners and 2 drawers.

Offline

 

#2 2012-04-13 12:43:38

TorbyFork234_Test
Scratcher
Registered: 2012-04-04
Posts: 15

Re: help with raycaster

bump

Offline

 

#3 2012-04-13 16:48:56

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

so that you don't need to download, here is the script for sensing the walls

when I receive
sense
delete
all
of
distances
set
drawing
to
1
set
distance
to
0
set
scanning remembering thingamajig
to
48
repeat
24
because I have 4 sensors
set
distance
to
60
go to
movement
the movement sprite
point in direction
direction
of
movement
+
scanning remembering thingamajig
repeat until
touching
wall
?
or
distance
=
0
move
1
steps
change
distance
by
-1
add
distance
to
distances
change
scanning remembering thingamajig
by
-1

and here is the script for drawing the walls

when I receive
draw
go to x:
1
y:
180
since each drawer takes half the screen
set pen size to
5
set pen color to
clear
pen up
set
help counting
to
24
since each distances list is 24 items long
repeat
24
set pen shade to
item
help counting
of
distances
*
1
+
2
/
3
set y to
item
help counting
of
distances
*
4
pen down
set y to
item
help counting
of
distances
*
4
*
-1
pen up
change
help counting
by
-1
set pen shade to
0
change x by
5
because the pen size is 5

Each drawer does that script twice to match up with the 4 lists of distances. I'm trying to figure out how to fix that when it senses, it draws it in a curved room when it's supposed to be straight. Can someone please help me?

Last edited by TorbyFork234 (2012-04-13 16:49:17)

Offline

 

#4 2012-04-13 16:53:38

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: help with raycaster

The reason is because you're using purely distance.  If you use purely distance, even if points are on the same line that's perpendicular to your view, they're different distances.  This creates the Fisheye effect.  In order to correct this, take the distance you get when scanning and multiply by the cosine of the offset of the angle (i.e. if you're drawing the part of the wall right in the middle, the offset is 0 degrees and so the distance will simply be multiplied by 1).


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#5 2012-04-13 16:55:58

TorbyFork234_Test
Scratcher
Registered: 2012-04-04
Posts: 15

Re: help with raycaster

AtomicBawm3 wrote:

The reason is because you're using purely distance.  If you use purely distance, even if points are on the same line that's perpendicular to your view, they're different distances.  This creates the Fisheye effect.  In order to correct this, take the distance you get when scanning and multiply by the cosine of the offset of the angle (i.e. if you're drawing the part of the wall right in the middle, the offset is 0 degrees and so the distance will simply be multiplied by 1).

How do I find the offset of the angle? I tried turning 1 degree, moving 1 step, and seeing if it's still touching the wall, and if not, take that direction and continue going. but that didn't work.

Offline

 

#6 2012-04-13 17:18:24

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: help with raycaster

Your offset would by your variable "scanning remembering thingamajig."  Also, you might be able to reduce your FOV.


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#7 2012-04-13 17:20:37

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

so the new script should be

add
distance
*
cos
of
scanning remembering thingamajig
to
distances

?

Also, what's FOV?

Last edited by TorbyFork234 (2012-04-13 17:21:08)

Offline

 

#8 2012-04-13 18:11:21

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: help with raycaster

Yep, looks like it from the way you've got your scripts set up.  smile  I hope this helps...what you could do is change you FOV from 96 to something more like 60 as well...that will reduce the effect as well.  That would mean at the beginning:
Sensor 1 variable: 30
Sensor 2 variable: 15
Sensor 3 variable: 0
Sensor 4 variable: -15


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#9 2012-04-13 18:32:31

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

AtomicBawm3 wrote:

Yep, looks like it from the way you've got your scripts set up.  smile  I hope this helps...what you could do is change you FOV from 96 to something more like 60 as well...that will reduce the effect as well.  That would mean at the beginning:
Sensor 1 variable: 30
Sensor 2 variable: 15
Sensor 3 variable: 0
Sensor 4 variable: -15

But that would also reduce the resolution so I won't do it.

Offline

 

#10 2012-04-13 18:34:42

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

It didn't work, it only made it worse.

Offline

 

#11 2012-04-14 11:43:47

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

bump

Offline

 

#12 2012-04-14 12:05:33

AtomicBawm3
Scratcher
Registered: 2009-06-27
Posts: 1000+

Re: help with raycaster

Oh, I know why it's not working...You have your distance counting down as it gets farther away...so your formula for distance would be:

add
60
-
cos
of
variable
*
60
-
distance
to
distances

Tell me how that works out.

Last edited by AtomicBawm3 (2012-04-14 12:05:46)


http://i50.tinypic.com/j0yw0p.jpg

Offline

 

#13 2012-04-14 12:13:55

TorbyFork234_Test
Scratcher
Registered: 2012-04-04
Posts: 15

Re: help with raycaster

no, I tried that and everything up close was fine, but everything far away was all wacky. and then I tried

60
-
cos
of
variable
*
60
-
distance

and that didn't work either.

Offline

 

#14 2012-04-14 13:22:25

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

Next thing I'll try

60
-
cos
of
direction
of
movement
-
variable
*
60
-
distance

Would it work?

Offline

 

#15 2012-04-14 13:27:03

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

Didn't work. Help anyone?

Offline

 

#16 2012-04-15 13:12:43

TorbyFork234
Scratcher
Registered: 2012-03-01
Posts: 1000+

Re: help with raycaster

bump

Offline

 

Board footer