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

#1 2012-04-29 14:56:11

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

Guide to Raycasters:

Ever seen a 3-d project in which the author called it a "ray caster"? Have you been amazed by that project showing a 3-d interactive environment, yet didn't know how to script it? Here is a guide to making a sprite based raycaster:

What you will need
• 1 sprite based map
• 1 movement sprite
• 1 wall sensing sprite
• 1 (recommended 4) sensors
• 1 (recommended 2) drawers

Step 1: Creating your map
Your map could be anything you want it to be, but I recommend for your first time having a map look sort of like this:

_________________
|                       |
|     _________    |
|    |            |    |
|    |________|    |
|                       |
|_______________|

add this script to it:

when gf clicked
go to x: [0] y: [0]
set [ghost v] effect to [100]
Step 2: Movement
Get your movement sprite and make it a 1 pixel large costume. Then put it inside the outer walls of you map sprite but outside the inner walls. Like this:
_________________
|               •       |
|     _________    |
|    |            |    |
|    |________|    |
|                       |
|_______________|

Where the dot represents the sprite.

Then add this script to it
when gf clicked
go to x: [-20] y: [0] //assuming that that is where you put your sprite
set [drawing v] to [0] 
set [touching wall v] to [0]
set [ghost v] effect to [100]
forever
if <not<(drawing)=[1]>>
if <key [right arrow v] pressed?>
turn cw [2] degrees
broadcast [sense v]
end
if <key [left arrow v] pressed?>
turn cw [-2] degrees
broadcast [sense v]
end
if <not<(touching wall)=[1]>>
if <key [up arrow v] pressed?>
move <speed> steps //where speed is the desired amount of steps you want it to move
broadcast [sense v]
end
end
if <not<(touching wall)=[-1]>>
if <key [down arrow v] pressed?>
move <<speed>*[-1]> steps //where speed is the desired amount of steps you want it to move
broadcast [sense v]
end
end
end

Step 3: Wall sensing
You now have your map and something that moves around in that map, but the only think that would stop it moving is variable values. So, we're going to make those values go to use.
Add this script to your wall sensing sprite.
when gf clicked
set [ghost v] effect to [100]
forever
if <not<(drawing)=[1]>>
go to [movement v] //where movement is the movement sprite
point in direction <[direction v] of [movement v]>
move <speed> steps
if <touching [walls v]?> //the map sprite
set [touching wall v] to [1]
else
set [touching wall v] to [0]
move <<speed>*[-2]> steps
if <touching [walls v]?>
set [touching walls v] to [-1]
else
set [touching wals v] to [0]
end
end
end
Now you can move around your map without going through walls.

Step 4: Distance sensing
This is the sensing script if you only have one sensor. You should divide up the numbers and for each sprite make a new list.
when gf clicked
set [ghost v] effect to [0]
delete (all v) of [distances v]
go to [movement v]
When I receive [sense v] //the broadcast from before
set [drawing v] to [1]
delete (all v) of [distances v]
set [distance v] to [0]
set [angle offset v] to [48]
repeat [96]
set [distance v] to [60]
go to [movement v]
point in direction <<[direction v] of [movement v]> + (angle offset)>
repeat until <<touching [walls v]?> or <(distance) = [0]>>
move (1) steps
change [distance v] by [-1]
end
add (distance) to [distances v]
change [angle offset v] by [-1]
end
wait until <(length of all lists) = [96]> //if you have other sensors
broadcast [draw v]
Step 5: Drawing
Divide up all the lists between all of the drawing sprites you have. Here is a script if you have only 1 drawer and 1 distance sensor

when I receive [draw v] 
go to x: [-237.5] y: [180]
set pen size to [5] //because 96*5=480 which is the width of the screen
set pen color to (color you want)
clear
pen up
set [help counting v] to <length of [distances v]> 
repeat <length of [distances v]>
set pen shade to <<item (help counting) of [distances v]>*<[1]+<[2]/[3]>>> 
set y to <<item (help counting) of [distances v]>*[4]>
pen down
set y to <<<item (help counting) of [distances v]>*[4]>*[-1]>
pen up
change [help counting v] by [-1]
set pen shade to [0]
change x by [5]
end
set [drawing v] to [0]
Next Update: Guide to Array Based Raycasters

Last edited by TorbyFork234 (2012-04-29 15:00:42)

Offline

 

#2 2012-04-30 02:08:36

jji7skyline
Scratcher
Registered: 2010-03-08
Posts: 1000+

Re: Guide to Raycasters:

Wow! Cool tutorial! I've always wanted to try raycasting! I'll try this one day :3


I don't know why you say goodbye, I say hello!  big_smile

Offline

 

#3 2012-04-30 18:44:20

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

Re: Guide to Raycasters:

jji7skyline wrote:

Wow! Cool tutorial! I've always wanted to try raycasting! I'll try this one day :3

Thanks!

Offline

 

#4 2012-05-03 17:48:37

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

Re: Guide to Raycasters:

bump

Offline

 

#5 2012-05-05 20:02:58

pi3
Scratcher
Registered: 2011-12-31
Posts: 500+

Re: Guide to Raycasters:

Epic guide! This really should be more appreciated. I'd love-it if I could.  smile


http://i44.tinypic.com/ofdhc4.jpgThanks FreshStudios!

Offline

 

#6 2012-05-06 01:50:45

chanmanpartyman
Scratcher
Registered: 2011-05-30
Posts: 500+

Re: Guide to Raycasters:

This is more of a weird obsession, but you should fix the operators (numbers instead of strings)

Offline

 

#7 2012-05-06 01:55:19

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

Re: Guide to Raycasters:

chanmanpartyman wrote:

This is more of a weird obsession, but you should fix the operators (numbers instead of strings)

I don't understand

Offline

 

#8 2012-05-06 14:25:18

chanmanpartyman
Scratcher
Registered: 2011-05-30
Posts: 500+

Re: Guide to Raycasters:

A string insert (any text) is:

[hi]
While a number is expressed as:
(10)

Offline

 

#9 2012-05-06 18:00:23

coppearlix
Scratcher
Registered: 2010-01-31
Posts: 500+

Re: Guide to Raycasters:

Awesome! I'm probably not going to use it but it's very interesting.


The Gobo has transformed--into the Gobony!
It's ALIIIIVE!

Offline

 

#10 2012-05-07 10:40:18

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

Re: Guide to Raycasters:

chanmanpartyman wrote:

A string insert (any text) is:

[hi]
While a number is expressed as:
(10)

Oh, I don't think that it's really necessary.

Offline

 

#11 2012-05-07 23:13:10

berberberber
Scratcher
Registered: 2012-03-08
Posts: 1000+

Re: Guide to Raycasters:

I have a problem. My raycaster: raycaster has a fisheye effect, even though the map is square! Could you tell me what i did wrong? Thanks!
bump

Last edited by berberberber (2012-05-07 23:13:32)


http://i47.tinypic.com/2iaa73k.png

Offline

 

#12 2012-05-08 10:23:49

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

Re: Guide to Raycasters:

berberberber wrote:

I have a problem. My raycaster: raycaster has a fisheye effect, even though the map is square! Could you tell me what i did wrong? Thanks!
bump

This one, since it's so simple, will have the fisheye effect. It means you did it completely to how I wrote how to do it. Mine (my first one found here) also has the fisheye effect. You would have to do your own math to take that out. It has something to do with cosine.

Offline

 

#13 2012-05-17 21:20:14

epicawesomeness
Scratcher
Registered: 2011-11-21
Posts: 5

Re: Guide to Raycasters:

Amazing!!!!!!!!!!!!!!!!! THX

Offline

 

#14 2012-07-14 10:26:01

TheRageFalcon
Scratcher
Registered: 2012-03-20
Posts: 1

Re: Guide to Raycasters:

epic

Offline

 

#15 2012-07-17 00:53:22

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

Re: Guide to Raycasters:

bump, I completely forgot about this.

Offline

 

#16 2013-02-14 23:36:21

uglysquidward
Scratcher
Registered: 2012-09-30
Posts: 100+

Re: Guide to Raycasters:

Cool!


Moved back to qwerts9000

Offline

 

#17 2013-02-15 10:44:58

Sqervay
New Scratcher
Registered: 2012-10-28
Posts: 52

Re: Guide to Raycasters:

Thanks for the tutorial  smile

Offline

 

#18 2013-04-19 02:11:25

Kamboy3210
New Scratcher
Registered: 2013-04-18
Posts: 1

Re: Guide to Raycasters:

My one takes forever for distance sensing and it gets so boring!
How can I make it faster?
Other than that, AWESOME!

when gf clicked
repeat until <(timer) > [10]>
  go to [mouse-pointer v]
  think [Awesome!]
end

Offline

 

#19 2013-04-19 13:05:12

CAA14
Scratcher
Registered: 2013-01-14
Posts: 1000+

Re: Guide to Raycasters:

What is a raycaster?
I will be honest, i do not understand why you use some of those math equations(Like 1 +2/3), but it looks and sounds cool, i just do not know what a raycaster is...

Regards,

CAA14

Offline

 

#20 2013-04-20 07:14:32

cobraguy
Scratcher
Registered: 2009-11-18
Posts: 100+

Re: Guide to Raycasters:

Thanks for the tutoria!


Fruit Fighter, when your food eats you.
http://i.imgur.com/8CR6Lnj.png?1

Offline

 

Board footer