Computer Science Canada Using Raycasting in Turing |
Author: | kane8290 [ 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. |
Author: | Raknarg [ 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 |
Author: | kane8290 [ 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 |
Author: | Raknarg [ 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: Math.Distance (x1, y1, x2, y2 : real) : real This takes in two points and results the distance Math.DistancePointLine (xp, yp, x1, y1, x2, y2 : real) : real 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. |
Author: | kane8290 [ 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... |
Author: | evildaddy911 [ 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 |
Author: | kane8290 [ 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? |
Author: | Raknarg [ 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:
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? |
Author: | smool [ 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. |
Author: | kane8290 [ 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... |
Author: | kane8290 [ 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. |
Author: | smool [ Thu May 03, 2012 9:56 pm ] |
Post subject: | RE:Using Raycasting in Turing |
Well I can't follow this at all, sorry ![]() |
Author: | Raknarg [ 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. |
Author: | kane8290 [ 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... |
Author: | smool [ 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. |
Author: | kane8290 [ Sat May 05, 2012 11:29 am ] |
Post subject: | Re: Using Raycasting in Turing |
Thanks, but my main issue at the moment is getting the detection system to work. Here is what I have soo far, it uses the same map as last time. Having the walls be detected and drawn is proving difficult (or buggy). Also, I need a way to check a pixel on a picture (like whatdotcolour), but without having to draw the image out... |
Author: | kane8290 [ Tue May 08, 2012 6:33 pm ] |
Post subject: | RE:Using Raycasting in Turing |
In an attempt to redo the who system, I came across a new system to use. But I have a tiny problem...how do do have it so that you have an array of points that start 30 points to the left of you and end 30 points right of you, then they all move up the Y axis by one each time unitl they hit something. I hate arrays... |
Author: | Raknarg [ Tue May 08, 2012 6:42 pm ] |
Post subject: | RE:Using Raycasting in Turing |
Maybe you should try rephrasing a bit, that didnt make much sense. And dont worry, one day you'll realize they're a beautiful paracosm to a world of amazing programming magic. Trust me. |
Author: | Amarylis [ Wed May 09, 2012 9:12 pm ] |
Post subject: | RE:Using Raycasting in Turing |
If you have all the values of the colour codes for the pixels, you can use a 2D array to check with the coordinates |
Author: | kane8290 [ Thu May 17, 2012 1:05 pm ] |
Post subject: | Re: Using Raycasting in Turing |
I tried that, thanks. Here is the new program. Everything runs okay, but still no walls being drawn. |