I have been attempting to make a raycast in ICE and I’m stuck on some math. I need to be able to have a for statement that loops through the rays (I’m only doing 7 rays for right now) and in that a line that moves the point forward on the ray. I have a black and white sprite that gets printed on the screen at (0,0) Here is the code I have so far:

Code:
For(Theta,~30,30,10   # 0° is forward and it loops through by 10° so the view is 70° wide
PX->TESTX       # reset the start to the player
PY->TESTY       # same just the Y cord
Repeat N=10    # repeats until the ray is 10 pixels long
setColor(224)     # sets the color of the ray
If getPixel(TESTX, TESTY)!=0  # if the pixel that is tested is not black
setPixel(TESTX, TESTY)            # sets the non-black pixels to red to show ray
                    # moves the point forward on the ray by changing the x and y (what I need help with)
Else                                            # if the pixel is black
Save the point and N to a list     # save the point and N to the list for when I draw (going to wait for this)
End
End
End


I don’t mind if it doesn’t directly work with ICE because I can change it a bit. Also if there are any question or something is hard to understand please ask (:
It looks like you're trying to implement ray casting by stepping pixel-by-pixel. It's important to note that the ratio of your step size for the y value to x value step size should be tan(theta), so step y pixels by sin(theta) and x pixels by cos(theta). This comes with another problem though; you still have to find a nice integer ratio so you aren't stepping '0' x-value pixels if your angle is close to 90 degrees.

One alternative method to this method ray casting is just to solve a systems of equations.
You can compute your slope using tan(theta). Note: the slope of your ray cast line is the slope of some line so you can use algebra to solve for any intersection points, assuming the object you're checking for ray casting collisions is something like a polygon, in which case your second line would just be the side of the polygon.

If you want to compute intersections of lines in higher dimensions: https://stackoverflow.com/questions/563198/how-do-you-detect-where-two-line-segments-intersect but you're probably dealing with something like line to plane intersections in something like 3D.
Thanks so much! you explained it so well that all I need to do I debug and do the drawing. I did not think I would get this far this fast Very Happy
  
Register to Join the Conversation
Have your own thoughts to add to this or any other topic? Want to ask a question, offer a suggestion, share your own programs and projects, upload a file to the file archives, get help with calculator and computer programming, or simply chat with like-minded coders and tech and calculator enthusiasts via the site-wide AJAX SAX widget? Registration for a free Cemetech account only takes a minute.

» Go to Registration page
Page 1 of 1
» All times are UTC - 5 Hours
 
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum

 

Advertisement