Posted: Wed May 02, 2012 7:47 pm Post subject: Using Raycasting in Turing
Hey, I'm new to this forum, but have been using turing for 3 full years now in my computer programming class. It's my grade 12 year, and I want to really make a good, high quality program for the class. As of right now, I am planning on making an action/ adventure/ RGP type game (similar to the first two elder scrolls). Now, I have read the "tutorial" availiable online that gives you a good idea of how raycasting works and what-not, but I havn't been able to really get started.
Can anyone here give me a general idea of how to go about raycasting in turing? I'm not asking you to do it for me, just some pointers on how to, well, do it.
I have the map already done, was was thinking of using Math.Distance to find the distance to the walls. But that is really all I got. I learn best by following instructions and then exploring on my own, not reading concepts and trying to apply them.
Look forward to hearing from you guys.
Sponsor Sponsor
Raknarg
Posted: Wed May 02, 2012 8:47 pm Post subject: RE:Using Raycasting in Turing
What are you trying to accomplish with this? I'm pretty sure I know what you're talking about, but I need some context
kane8290
Posted: Wed May 02, 2012 9:03 pm Post subject: RE:Using Raycasting in Turing
im doing the stage where you find the distance of the walls from the player so that they can later be drawn
Raknarg
Posted: Thu May 03, 2012 8:48 am Post subject: RE:Using Raycasting in Turing
ok. So you're saying you have 2 points in 2 dimensional space and you want the distance between them. That's easy enough. In fact, they have 2 built in functions that could be used for this:
This takes in one point and the ends of a line, and determines the closest distance between them. if you're looking at a wall, I would suggest using the second function there.
kane8290
Posted: Thu May 03, 2012 10:23 am Post subject: RE:Using Raycasting in Turing
The nproblem im having is finding a way to find the point in which a ray hits a wall, recording it, and then doing that for the rest of the rays in varius directions.
I guess in general, I'm looking for a walkthrough on how to raycast in turing...
evildaddy911
Posted: Thu May 03, 2012 10:34 am Post subject: RE:Using Raycasting in Turing
so what your doing is finding the point at which the ray hits the wall?
all you need to do is use y=mx+b, then solve the equation, you should have done that in math class by grade 11
in case youve forgotten,
m1(x)+b1=m2(x)+b2
or, x=(b1-b2)/(m1-m2)
(the 1s and 2s are subscripts)
then find the y value using the first equation. After that, its a pretty simple matter to see if the point the ray hit was on the wall
kane8290
Posted: Thu May 03, 2012 12:04 pm Post subject: RE:Using Raycasting in Turing
I know of that equation. I just really suck at explaining stuff. Last try;
I have a playerX and a playerY variable that represent your location in the world. On a 640 by 400 color map i made in paint using turings definition of black, green, red, etc, I made walls of various colors that are all the same size. I have decided to use 60 as me field of view. From the direction the player is facing, i need to have 60 rays (30 on each side of the player) go out in a straight line.
Once I have that done, I need to calculate the distance of any ray until it hits it's first color. Once I have that distance, I can use it to draw a rectangle on screen that has been scaled to match the distance I got.
I know that that may not be true raycasting, but it is still a way that I find to make sense. So, my question is, how would I actually do any of that? I got the rectangle drawing and scalling part (that part is easy), bu the rest i dont quite get.
How would one send out 60 rays (one for each degree from 30 on either side of the player look direction) and then record the distance till they hit a colored space on the 2d map?
Raknarg
Posted: Thu May 03, 2012 1:18 pm Post subject: RE:Using Raycasting in Turing
Oh, you're talking about vectors, excuse me.
In your case, you dont actually need to use true raycasting. What I would do is just use some trigonometry and whatdotcolor.
Have a loop and just keep increasing each line by a tiny amount until one of them hits a certain colour. Something like this:
Turing:
type coordinate : record
dx, dy :int
x, y, vx, vy :real endrecord
var point :array1.. 60of coordinate
for i :1.. 60
point (i).x :=maxx / 2
point (i).y :=maxy / 2
point (i).vx :=cos(i *5)
point (i).vy :=sin(i *5) endfor
loop for i :1.. 60
point (i).x += point (i).vx
point (i).y += point (i).vy
point (i).dx :=round(point (i).x)
point (i).dy :=round(point (i).y) ifwhatdotcolour(point (i).dx, point (i).dy)=7then return else Draw.Dot(point (i).dx, point (i).dy, 12) endif endfor endloop
Sorry, if I overexplained, but I felt it was necessary, and I think you deserve it.
So in your case, you don't have to actually draw the lines, but everything else would be the same.
Any of this I should explain?
Sponsor Sponsor
smool
Posted: Thu May 03, 2012 4:05 pm Post subject: RE:Using Raycasting in Turing
Okay so what you want to do is line-intersection. y=mx+b sucks because you have to have exceptions and such, just not that great. whatdotcolor works like Raknarg was saying, but it's not perfect. What you want to do is define the walla by the coordinates of the tips (x, y) and then define the start and end position of your ray (x, y). Then you do some crazy math: http://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
That is the algorithm i use for all my collision detection. It's big, it's ugly, but it works everyime.
kane8290
Posted: Thu May 03, 2012 4:42 pm Post subject: Re: Using Raycasting in Turing
Thanks. Um, this is what I have so far. It's from earlier (before seeking help). Can you find why it wont work? If it is hopeless then I am def using your sugestion.
Hopefully you can follow it...
Posted: Thu May 03, 2012 7:53 pm Post subject: RE:Using Raycasting in Turing
In the above, Ihave mostly had issues with getting the program to read the map file without litteraly having to draw it. Also, Allowing for smooth rotation, not rapid shifts to the cardinal directions, is causing some problems.
smool
Posted: Thu May 03, 2012 9:56 pm Post subject: RE:Using Raycasting in Turing
Well I can't follow this at all, sorry What are the ViewX and ViewY variables? what does Draw_Wall actually do? I would take what you have learned and rewrite it. Often when I write something and it doesn't work, I just rewrite it from scratch, improving on the things i did wrong the first time. Plus, you only have about 60 lines here, so won't take to long to rewrite.
Raknarg
Posted: Fri May 04, 2012 10:18 am Post subject: RE:Using Raycasting in Turing
@smool I wanted him to start on something that wasnt vectors, not everyone understands them
When I do games like these, I dont use pictures as the background exactly. I make tiles instead. That way you can actually compare areas on the board as a grid, and so you can actually tell the distance of objects.
kane8290
Posted: Fri May 04, 2012 10:28 am Post subject: Re: Using Raycasting in Turing
Lol, thats okay, my programming is usually pretty messy. What it is supposed to do is this;
Starting 30 pixels to the left of the players Y coordinate, the program checks the color of the pixel of that coordinate for a specific color. If none is found, then it increases the check location by a factor of Y + 1. This continues until either a color is detected, or the Y reaches a maximum value of PlayerY + 60.
Once either of those conditions are met, the check coordinate resets is Y to that of the players and adds one to its X. Effectivly, this has the program scan a column, and move to the next when something (or nothing) is found.
Once a color has been detected, the distance between the players Y and the points Y is calculated and used to adjust the values of a rectangle that is too be drawn. Hopefully that clears some of this up. The problem that I'm having is that I don't know how to check a coordinate on a picture without actually having it draw onscreen (which I dont want to do). The other issue is that I cant think of an easy ay to have smooth player rotation...
example 2.bmp
Description:
The drawn part
Filesize:
750.05 KB
Viewed:
83 Time(s)
example 1.bmp
Description:
Explaining the check part
Filesize:
750.05 KB
Viewed:
100 Time(s)
smool
Posted: Fri May 04, 2012 10:01 pm Post subject: RE:Using Raycasting in Turing
you want rotation? then you want this: http://en.wikipedia.org/wiki/Rotation_matrix
The key thing to get from that article is the following:
- X' = Xcos(t) - Ysin(t)
- Y' = Xsin(t) + Ycos(t)
That rotates the point (X, Y) about the origin (0, 0). So if you want to rotate around a different coordinate, simply translate your point so that its around the origin, rotate it, than translate back.