-2

I've been following this thread: https://www.omnimaga.org/ti-basic-language/raycasting-halp/

I've tried anything. How do I do Raycasting in TI-Basic, is it possible to just draw a simple wall and move around? I have a TI-83 Plus.

5
  • 4
    Can you give an example of what you have tried and what problems you have come across. Nov 20, 2017 at 14:28
  • omnimaga.org/ti-basic-language/raycasting-halp i did What she said there, but i coldent understand it Nov 20, 2017 at 14:46
  • Couldnt is What i meant, haha Nov 20, 2017 at 14:47
  • 3
    You can edit your post (and comments too, to fix typo's). Please update your question with what you've tried already. Nov 20, 2017 at 20:50
  • You will probably want to know about any 3D engine or programs to use. There's a couple of projects out there that you could join for discussion.
    – yacc
    Nov 21, 2017 at 9:37

1 Answer 1

10

Can ray casting be done in TI-83 Basic? Yes; let's try it.

First, what exactly is ray casting?

Ray casting is a term used to describe a collection of algorithms for rendering 3D graphics. What ray casting algorithms generally have in common is that they:

  1. Send out rays from a single point in different directions
  2. Compute the closest object that intersects that ray
  3. Render the part of that object that intersects the ray at a position dependent on the ray's direction

For TI-83, we'll implement a simple ray caster that only sends out horizontal rays. From a top down view, it might looks like this:

Ray Caster Top Down View

Of course, we'll need to use more than 5 rays if we want a decent image. Now that we've defined what we're implementing, let's move on to figuring out how.

What values do we need to keep track of?

This is easy. The values we need to keep track of are right in the ray casting algorithm.

  • We need to keep track of the rays. Specifically, we need to know their directions.
  • We need to keep track of the origin of the rays, or the camera's position.
  • We need to keep track of all of "objects"' locations. For simplicity, we'll just keep track of walls.

Now, the next part is easy too. How do we keep track of these values? Well, since there are a bunch of rays, we'll store their directions in a list, L₁. On the other hand, since there is only one camera, we'll store it's position in two variables, S and T (because they are right above X and Y an the TI-83 keyboard. We'd like to use X and Y, but these get modified by the Line operation we use later). The walls give us more options, but for simplicity, we'll store a matrix that represents a floor plan. If there is non-zero number at a position in the matrix, that means there is a wall at the corresponding location in the world. We'll say that each value in the matrix cooresponds to a 1 × 1 location in the world.

What do we do with the values?

Now that we know what values we need to store (these values are called our model), we need to figure out what we actually have to do with them. In other words, we need to elaborate on the ray casting algorithm.

Before we do anything, we need to set initial states for all of our values. We'll use the contents of [A] for the map information. The starting location of the camera is arbitrary, so we'll just set it at (1.5, 1.5), right in the middle of the first tile.

Before we talk about the direction of the rays, we need to decide on how many to use. Ideally, we'd use at least one ray for each pixel, but the TI-83 has a horizontal resolution of 96 pixels, and using 96 rays may cause our ray caster to be too slow. Instead, we'll use 24 rays, or one ray for every 4 pixels. This should give us a reasonable image quality, while hopefully not being too much for the TI-83 to handle. We also need to choose a Field of View. We'll pick 96°, because that means one pixel represents 1°. Finally, we'll say the middle ray starts pointing 45° below horizontal.

Now, back to the algorithm. By considering how a general raycasting algorithm applies to our model, we get the steps:

  1. Set up the graph screen
  2. Set S and T to 1.5
  3. Get the width and height of the map
  4. Compute the initial directions for each ray
  5. Loop these while running:
    1. For each ray:
      1. Compute if it intersects a wall
      2. If it does, draw a vertical line on the screen representing the wall
    2. If pressing move forward; Else, if pressing , move backward
    3. If pressing , turn left; Else, if pressing , turn right
    4. If pressing CLEAR, stop running

Let's tackle these one by one.

Implementation

1. Set up the graph screen

We need to turn off the graph axes and set the limits. We'll see why these limits are what they are in part 5.2.2.

AxesOff
4 → Xmin
24 * 4 + 3 → Xmax
-1 → Ymin
1 → Ymax

2. Set S and T to 1.5

Easy:

1.5 → S
1.5 → T

3. Get the width and height of the map

dim([A]) → L₁
L₁(1) → H
L₁(2) → W

4. Compute the initial directions for each ray

Not bad:

seq((I - 12.5) * 4° + 45°, I, 1, 24) → L₁

5. Loop these while running:

1 → R
While R
    ...
End

5.1. For each ray:

For(I, 1, 24)
    ...
End

5.1.1. Compute if it intersects a wall

This is the first really tricky part. Basically, we are going to start at the camera, and then keep moving out along the ray, tile by tile, until we either hit a wall or go out of the map. If we hit a wall we will set C (for collision) to 1.

Within the while loop:

  • U and V represent the current position of the ray trace
  • The first If Else block checks in which quadrant the ray is pointing. Then, based on the quadrant it stores prospective next values for U and V into P and Q. Basically, it follows the ray horizontally to the next tile, and stores that into P, and follows the ray vertically to the next tile and stores it to Q.
  • The next If Else block determines whether P or Q was the right prospective value. Whichever would lead to a shorter distance traveled is the right guess. If we picked the longer one, we would skip a tile. Due to symmetry in trig functions, the new U and V values can be calculated from P and Q regardless of the quadrant. The distance of the step is also updated in this block. We will use the distance later to determine the height of the drawn line.
  • Finally, the last if statement tests If the current block is a wall. If it is, C is set so that the ray trace stops.

If this part is unclear, let me know and I can add a diagram.

0 → C
S → U
T → V
0 → D
While C = 0 and 1 ≤ int(U) and int(U) ≤ W and 1 ≤ int(V) and int(V) ≤ H
    L₁(I) → A
    If A > 0° :Then
        If A > 90° :Then
            -int(-U - 1) → P
            int(V + 1) → Q
        Else
            int(U + 1) → P
            int(V + 1) → Q
        End
    Else
        If A > -90° :Then
            int(U + 1) → P
            -int(-V - 1) → Q
        Else
            -int(-U - 1) → P
            -int(-V - 1) → Q
        End
    End
    If abs((P - U) / cos(A)) < abs((Q - V) / sin(A)) :Then
        D + abs((P - U) / cos(A)) → D
        V + (P - U) * tan(A) → V
        P → U
    Else
        D + abs((Q - V) / sin(A)) → D
        U + (Q - V) / tan(A) → U
        Q → V
    End
    If int(U) ≥ 1 and int(U) ≤ W and int(V) ≥ 1 and int(V) ≤ H :Then
        If [A](int(U), int(V))
        1 → C
    End
    If -int(-U + 1) ≥ 1 and -int(-U + 1) ≤ W and -int(-V + 1) ≥ 1 and -int(-V + 1) ≤ H :Then
        If [A](-int(-U + 1), -int(-V + 1))
        1 → C
    End
End

5.1.2. If it does, draw a vertical line on the screen representing the wall

This part just uses a little trig to determine how tall the line on screen should be. Since the horizontal FOV is 96° and the resolution of a TI-83 is 96 pixels by 64 pixels, the vertical FOV should be 96° * 64 / 96, or 64°. To find the height of the displayed line, we want to find the inclantion to the top of the wall (the angle required to look up to the top of the wall) divided by half the vertical field of view. We use the value in [A] for the height of the wall. Remember, we set up the graph screen earlier, and since each ray represents four horizontal pixels, we need to draw 4 lines.

Additionally, before drawing the lines, we clear the part of the screen we will be drawing on by drawing 4 full height white lines.

Again, if this part is not clear, I can add a diagram.

I * 4 → N
Line(N, 1, N, -1, 0)
Line(N + 1, 1, N + 1, -1, 0)
Line(N + 2, 1, N + 2, -1, 0)
Line(N + 3, 1, N + 3, -1, 0)
If C :Then
    tan⁻¹(([A](int(U), int(V)) + [A](-int(-U + 1), -int(-V + 1))) / D) / 32° → Z
    Line(N, Z, N, -Z)
    Line(N + 1, Z, N + 1, -Z)
    Line(N + 2, Z, N + 2, -Z)
    Line(N + 3, Z, N + 3, -Z)
End

5.2. If pressing move forward; Else, if pressing , move backward

This and the next part ar straightforward. F is the angle the camer is facing, and is determined by averaging the middle two rays.

getKey → K
(L₁(12) + L₁(13)) / 2 → F
If K = 25 :Then
    S + 0.1 * cos(F) → S
    T + 0.1 * sin(F) → T
End
If K = 34 :Then
    S - 0.1 * cos(F) → S
    T - 0.1 * sin(F) → T
End

5.3. If pressing , turn left; Else, if pressing , turn right

If K = 24 :Then
    L₁ + 2° → L₁
End
If K = 26 :Then
    L₁ - 2° → L₁
End

5.4. If pressing CLEAR, stop running

If K = 45
0 → R

Everything together

By putting all of the code together, and removing the whitespace which was added for readability, we get the complete (and ugly) program, shown below. It runs, but it is pretty slow, certainly too slow for a game. Most of the execution time is spent in step 5.1.1, the actual ray casting algorithm. The speed of the ray casting algorithm could potentially be improved by making clever use of builtin procedures, but that's outside the scope of the question.

AxesOff
4→Xmin
24*4+3→Xmax
-1→Ymin
1→Ymax
1.5→S
1.5→T
dim([A])→L₁
L₁(1)→H
L₁(2)→W
seq((I-12.5)*4°+45°,I,1,24)→L₁
1→R
While R
For(I,1,24)
0→C
S→U
T→V
0→D
While C=0 and 1≤int(U) and int(U)≤W and 1≤int(V) and int(V)≤H
L₁(I)→A
If A>0°:Then
If A>90°:Then
-int(-U-1)→P
int(V+1)→Q
Else
int(U+1)→P
int(V+1)→Q
End
Else
If A>-90°:Then
int(U+1)→P
-int(-V-1)→Q
Else
-int(-U-1)→P
-int(-V-1)→Q
End
End
If abs((P-U)/cos(A))<abs((Q-V)/sin(A)):Then
D+abs((P-U)/cos(A))→D
V+(P-U)*tan(A)→V
P→U
Else
D+abs((Q-V)/sin(A))→D
U+(Q-V)/tan(A)→U
Q→V
End
If int(U)≥1 and int(U)≤W and int(V)≥1 and int(V)≤H:Then
If [A](int(U),int(V))
1→C
End
If -int(-U+1)≥1 and -int(-U+1)≤W and -int(-V+1)≥1 and -int(-V+1)≤H:Then
If [A](-int(-U+1),-int(-V+1))
1→C
End
End
I*4→N
Line(N,1,N,-1,0)
Line(N+1,1,N+1,-1,0)
Line(N+2,1,N+2,-1,0)
Line(N+3,1,N+3,-1,0)
If C:Then
tan⁻¹(([A](int(U),int(V))+[A](-int(-U+1),-int(-V+1)))/D)/32°→Z
Line(N,Z,N,-Z)
Line(N+1,Z,N+1,-Z)
Line(N+2,Z,N+2,-Z)
Line(N+3,Z,N+3,-Z)
End
End
getKey→K
(L₁(12)+L₁(13))/2→F
If K=25:Then
S+0.1*cos(F)→S
T+0.1*sin(F)→T
End
If K=34:Then
S-0.1*cos(F)→S
T-0.1*sin(F)→T
End
If K=24:Then
L₁+2°→L₁
End
If K=26:Then
L₁-2°→L₁
End
If K=45
0→R
End
1
  • 1
    Very very good answer. +1. I made a raycaster on my CE once, and this was quite close to how I did it. There are several trivial optimizations, mostly 0-> to DelVar, but well done!
    – iPhoenix
    Dec 12, 2017 at 22:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.