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
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 endStep 3: Wall sensing
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 endNow you can move around your map without going through walls.
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
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
Wow! Cool tutorial! I've always wanted to try raycasting! I'll try this one day :3
Offline
jji7skyline wrote:
Wow! Cool tutorial! I've always wanted to try raycasting! I'll try this one day :3
Thanks!
Offline
This is more of a weird obsession, but you should fix the operators (numbers instead of strings)
Offline
chanmanpartyman wrote:
This is more of a weird obsession, but you should fix the operators (numbers instead of strings)
I don't understand
Offline
A string insert (any text) is:
[hi]While a number is expressed as:
(10)
Offline
Awesome! I'm probably not going to use it but it's very interesting.
Offline
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
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)
Offline
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
Amazing!!!!!!!!!!!!!!!!! THX
Offline
bump, I completely forgot about this.
Offline
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
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