Discord
Login
Community
DARK THEME

How do I detect what tile a sprite is on?

I'm making a game, and it needs a collision system. I don't want to use a library. I just need to know if there is a way to know which tile a sprite is on.

Yes, mainly because it justifies two methods: either a map array where you create objects for collision detection, like hitboxes.

How do I make a map array?

Map arrays are lists with values; for example, I'll give you an example

init = function()
map = [
[0,0,0,0],
[0,0,0,0],
[0,1,0,0],
[0,0,0,0]
]
end

It's like filling in your card with numbers or any other information that offers possibilities to the player. For example, I can decide that if the player's position is 1, I block them, or that the 0 spaces are empty and the player can move there. You can also create GPS variables to know where the player is located on the map here is another example

clamp = function(value, lower_limit, upper_limit)
  local val = max(value, lower_limit)
  val = min(val, upper_limit)
  return val
end

init = function()
  map = [
    [0,0,0,0],
    [0,0,0,0],
    [0,0,1,0],
    [0,0,0,0]
  ]
  
  map_Pr = object
    x = 0
    y = 0
    case = 10
    color = "rgb(0,197,66)"
  end
  
  player = object
    x = -15
    y = -15
    taille = 10
    color = "rgb(255,170,0)"
  end
end

update = function()
  local oldX = player.x
  local oldY = player.y
  
  if keyboard.press.UP then player.y += map_Pr.case
  elsif keyboard.press.RIGHT then player.x += map_Pr.case
  elsif keyboard.press.DOWN then player.y -= map_Pr.case
  elsif keyboard.press.LEFT then player.x -= map_Pr.case
  end
  
  local j = floor((player.x + 15) / 10)
  local i = floor((player.y + 15) / 10)
  
  if i < 0 or i > 3 or j < 0 or j > 3 then
    player.x = oldX
    player.y = oldY
  elsif map[i][j] == 1 then
    player.x = oldX
    player.y = oldY
  end
  
  player.x = clamp(player.x, -15, 15)
  player.y = clamp(player.y, -15, 15)
end

draw = function()
  screen.clear("rgb(0,170,255)")
  
  for i = 0 to 3
    for j = 0 to 3
      local px = j * 10 - 15
      local py = i * 10 - 15
      local c = map_Pr.color
      if map[i][j] == 1 then c = "black" end
      
      screen.fillRect(px, py, 10, 10, c)
      screen.drawRect(px, py, 10, 10, "rgba(0,105,35,0.2)")
    end
  end
  
  screen.fillRect(player.x, player.y, player.taille, player.taille, player.color)
  screen.drawRect(player.x, player.y, player.taille, player.taille, "rgb(197,131,0)")
end

Here, I move by square, but you don't have to do it the same way; it still works. I suggest this because it's easier, and you just need to enter the number of squares that block the player or cause collisions in the map. But the problem is, it's difficult to create very large maps.

I also don't know how to use the libraries or Quick Engine... Could you explain it to me?

if you import the library into a project then go to the document tab and press libraries the docs for quick engine should pop up

Post a reply

Progress

Status

Preview
Cancel
Post
Validate your e-mail address to participate in the community