Author Topic: Raycasting halp?  (Read 8011 times)

0 Members and 1 Guest are viewing this topic.

Offline noahbaby94

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 585
  • Rating: +29/-24
    • View Profile
    • My blog
Raycasting halp?
« on: May 06, 2009, 07:46:21 pm »
So yea I've read through all of this http://www.permadi.com/tutorial/raycast/ but I still don't get it. Can anyone explain it to me really slowly.
That's what she said!!!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Raycasting halp?
« Reply #1 on: May 06, 2009, 08:26:24 pm »
K, so when we want to create a 3D environment, we need to have some way to store the world as data.  if we wanted, we could store every coordinate of every triangle that makes up the walls and floor and objects.  BUT, since we want to be as memory efficient as possible, we will store the world as a simple matrix.  The world well be made up of a series of blocks that lay on a grid.  A 1 in the matrix might represent a block, and a zero might represent an empty space:

[1,1,1,1,1,1,1,1]
[1,0,0,0,0,0,0,1]
[1,0,0,0,0,0,0,1]
[1,1,1,1,1,1,1,1]
 
Now, in order to render the image on screen, we need to consider how it will look.  There are 96 pixels from left to right, and that is 96 times we need to decide what kind of column to draw (remember, every pixel in a single column is responding to the same distance, so we don't have to go pixel by pixel, only column by column)

If we consider a view of a wall, we can see the farther it gets away, the smaller it gets.  The actual formula is aparentHeight = Arctan(ActualHeight/Distance) , but this is almost exactly equal to aparentHeight = ActualHeight/Distance, and this way is faster.

So in each column of pixels, we will draw a single slice of wall that is a certain distance away.  We have the formula, now all we need is the distance.  This is where the raycasting comes in.

We are at a certain position, and we are facing a certain direction.  In order to find the distance in front of us until we hit a wall, we just need to move us forward until we hit a wall.  You can then do this many more times at many slightly different angles to get all the distances you need (96).

It is then a simple matter to use your information and the formulas to create an amazing, texterless wall!  Textures are MUCH harder, and will be the subject of another lesson if you are still confused.

Hope that helped!  I made a raycasting game in java a while back, so I know a thing or too  ;)


Offline noahbaby94

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 585
  • Rating: +29/-24
    • View Profile
    • My blog
Re: Raycasting halp?
« Reply #2 on: May 06, 2009, 08:35:29 pm »
Thanks sounds really good I'll try to figure out how to implement it thanks again.
That's what she said!!!

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Raycasting halp?
« Reply #3 on: May 06, 2009, 08:40:27 pm »
No prob, Hope you get it working!

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: Raycasting halp?
« Reply #4 on: May 07, 2009, 12:54:01 am »
Textures are MUCH harder
I wouldn't necessarily say that they are MUCH harder. In fact, if you use a simple linear filter, they are quite easy. You have the following: the height of your wall, and the height of your scanline. Assuming you create textures that are the same size as your wall (e.g. 64x64 if your walls a 64 in height) you just calculate a texture step by doing: step = ( height of wall ) / ( height of scanline ). Now you have your step, and the offset into the texture from your raycast already. So just index into the texture from top to bottom with the step you calculated.

At least that's what I did for my raycasting engine, and it working out pretty well.
There are 10 types of people in this world-- those that can read binary, and those that can't.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Raycasting halp?
« Reply #5 on: May 07, 2009, 01:08:34 am »
well, the trickiest part is finding your offset in the picture in the first place, and then scaling the image (Assuming that your writing this for a TI and you are writing your own scale function)

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: Raycasting halp?
« Reply #6 on: May 07, 2009, 01:17:43 am »
well, the trickiest part is finding your offset in the picture in the first place, and then scaling the image (Assuming that your writing this for a TI and you are writing your own scale function)
True, and I did in fact write mine in TI-BASIC. I just threw the 64x64 bitmap data in a compressed list so as to not waste the 9 bytes and allowing me to actually use a list. Then you just treat the list as a 2-dimensional array by using something to the effect of w*y+h.

And effectively, all you need is either the x or y offset. It depends specifically on what your orientation is to decide which to use. And that's simple in the fact that, if I recall correctly, you can divide your space from 0 to 2pi into 4 quadrants (not the four standard quadrants though). Once you know the direction your ray is going in, you find the first GRID COLLISION, where it hits the grid, not necessarily a filled in block. Then, since you know the size of your blocks and the slope of the ray, you just increment by the size of your blocks using that slope. This leads to you gaining your offset if a collision occurs, and then using that offset in an image of your choosing. Then it's as simple as using the linear filter I spoke about above.

Maybe some people would find that hard, but if you just sit down and put it on paper, I don't believe it is really that difficult.
There are 10 types of people in this world-- those that can read binary, and those that can't.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Raycasting halp?
« Reply #7 on: May 07, 2009, 10:25:09 am »
Ah, you did your casting that way.  I was writing mine in java, so i was a bit lazy and just incremented the ray by .02*Unit Vector every time.  Unfortunately that menas it didn't get the exact intersection at the wall, and so both the x offset and the y offset were large enough so that it got confused between the two.

Your way is a lot better, but after programing in TI basic for a while, i was a bit overwhelmed with the speed

"You mean I can cast 300 rays, do 3000 trig functions, increment each ray thousands of times, and still get 60 frames per second!?!?!?"

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55943
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Raycasting halp?
« Reply #8 on: May 07, 2009, 09:33:44 pm »
even if slow I think BASIC raycasting could help, if textures are supported, drawing pre-rendered graphics for pseudo-3D BASIC games such as Phantasy Star 1 clones and such RPGs

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: Raycasting halp?
« Reply #9 on: May 08, 2009, 03:33:52 pm »
Ah, you did your casting that way.  I was writing mine in java, so i was a bit lazy and just incremented the ray by .02*Unit Vector every time.  Unfortunately that menas it didn't get the exact intersection at the wall, and so both the x offset and the y offset were large enough so that it got confused between the two.

Your way is a lot better, but after programing in TI basic for a while, i was a bit overwhelmed with the speed

"You mean I can cast 300 rays, do 3000 trig functions, increment each ray thousands of times, and still get 60 frames per second!?!?!?"
Yeah, I know what you mean, haha. :D

@DJ_Omnimaga: Very true.
There are 10 types of people in this world-- those that can read binary, and those that can't.