Log In  

Cart #pijifekubu-0 | 2022-12-16 | Code ▽ | Embed ▽ | No License
3

I asked ChatGPT to generate a raycasting engine for Pico-8 and this is what it gave me. Though I added movement and fixed the math functions as it was using the lua standard math library.

P#122566 2022-12-16 18:32

1

This looks like a decent boilerplate! With such a specific prompt, I wonder if we could see sign of overfitting (i.e. straight copy/pasted lines of code).

Do you still have the unmodified output code? I'm being curious about how much you had to tweak it.

P#122575 2022-12-17 00:15
1

Here is the original unedited code. It is worth noting that it does indeed comment the code.

-- constants
WIDTH = 128
HEIGHT = 128
FOV = math.pi / 2 -- field of view
DEPTH = 16 -- number of depth levels

-- map data
map = {
  {1,1,1,1,1,1,1,1,1,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,0,0,0,0,0,0,0,0,1},
  {1,1,1,1,1,1,1,1,1,1}
}

-- player position and direction
pos = {x=5, y=5}
dir = {x=1, y=0}

-- function to calculate the distance to the wall
function calc_dist(x, y, dir_x, dir_y)
  -- calculate the step and initial side distance
  local step_x, step_y
  local side_dist_x, side_dist_y
  if dir_x < 0 then
    step_x = -1
    side_dist_x = (pos.x - x) * 2
  else
    step_x = 1
    side_dist_x = (x + 1 - pos.x) * 2
  end
  if dir_y < 0 then
    step_y = -1
    side_dist_y = (pos.y - y) * 2
  else
    step_y = 1
    side_dist_y = (y + 1 - pos.y) * 2
  end

  -- perform DDA
  local hit = 0
  local side = 0
  while hit == 0 do
    if side_dist_x < side_dist_y then
      side_dist_x = side_dist_x + 2
      x = x + step_x
      side = 0
    else
      side_dist_y = side_dist_y + 2
      y = y + step_y
      side = 1
    end
    if map[y][x] > 0 then
      hit = 1
    end
  end

  -- calculate the distance to the wall
  local dist
  if side == 0 then
    dist = math.abs((x - pos.x + (1 - step_x) / 2) / dir_x)
  else
    dist = math.abs((y - pos.y + (1 - step_y) / 2) / dir_y)
  end
  return dist
end

function _draw()
  -- clear the screen
  cls()

-- loop through each column of the screen
for x = 0, WIDTH - 1 do
  -- calculate the ray direction
  local camera_x = 2 * x / WIDTH - 1 -- x-coordinate in camera space
  local ray_dir_x = dir.x + dir.y * camera_x
  local ray_dir_y = dir.y - dir.x * camera_x

  -- calculate the distance to the wall
  local dist = calc_dist(math.floor(pos.x), math.floor(pos.y), ray_dir_x, ray_dir_y)

  -- calculate the size of the wall slice
  local wall_slice_height = math.floor(HEIGHT / dist)

  -- calculate the top and bottom of the wall slice
  local wall_slice_top = math.floor((HEIGHT - wall_slice_height) / 2)
  local wall_slice_bottom = wall_slice_top + wall_slice_height - 1

  -- draw the wall slice
  for y = 0, HEIGHT - 1 do
    if y < wall_slice_top then
      pset(x, y, 7) -- ceiling
    elseif y > wall_slice_bottom then
      pset(x, y, 8) -- floor
    else
      pset(x, y, 6) -- wall
    end
  end
end
P#122592 2022-12-17 07:29 ( Edited 2022-12-17 07:30)
1

Thank you! That's an impressive wall of code.

I did some googling and it looks like some bits are a literal Lua translation of code from this article like the

//calculate step and initial sideDist

and

//perform DDA

parts. This implementation already has at least this JS translation with similar comments, so I guess it's able to translate into Lua from there?

It's funny because there are Lua implementations out there.

I see that the vanishing point jumps sometimes and I'm curious about what's wrong (and why is it wrong) but I'm not familiar enough with the algorithm to spot the mistake.

P#122661 2022-12-18 21:48 ( Edited 2022-12-18 22:04)

Yeah so people have noticed this about a number of these AI's. They are trained using information from the internet so they end up grabbing other peoples code, and for image generating ones, it is grabbing others art. It does seem bad that the people making the AI can profit off of someone else's work.

P#122688 2022-12-19 06:13

[Please log in to post a comment]