Discord
Login
Community
DARK THEME

map colisions

I've figured out how to use AABB to detect collisions with sprites but what if my sprite walks into a wall and how do I make it so it stops instead of going through it?

code for the test where I figured out how to do sprite collisions

init = function()
  text = "no collision"
  speed = 3
  playerX = 0 
  playerY = 100
end

movement = function()
  if keyboard.DOWN then 
    playerY -= speed
  elsif keyboard.UP then
    playerY += speed
  elsif keyboard.LEFT then
    playerX -= speed
  elsif keyboard.RIGHT then
    playerX += speed
 
  end
end

update = function()
   a_min_x = -25
  a_min_y = -25
  a_max_x = 25
  a_max_y = 25
  
  b_min_x = playerX - 25
  b_min_y = playerY - 25
  b_max_x = playerX + 25
  b_max_y = playerY + 25
  movement()
  if a_min_x <= b_max_x and b_min_x <= a_max_x and a_min_y <= b_max_y and b_min_y <= a_max_y then 
    text = "collision!"
  else 
    text = "no collision"
  end
end

draw = function()
  screen.clear()
  screen.drawSprite("icon",0,0,50)
  screen.drawSprite("sprite",playerX,playerY,50)
  screen.drawText(text,150,75,20)
end

https://microstudio.dev/i/gilles/quickengine/ >> function spriteMapCollision(player, levelMap)

https://microstudio.dev/i/TwiceUponATime/tilemapcollision/

"Separation / Push-back" or "Minimum Translation Vector"

see what happens when you increase the number of spawn boxes in the QuickEngine engine to 100.

init = function()
  .......
  for i=1 to 100
    local box = Quick.addSprite("box",0,0,20)
    box.x = random.next()*20-10
    box.y = random.next()*10
  end
  ......
end

im trying to do this from the ground up without quick engine or anything like that

The simplest possible collision implementation with a map and basic physics.

https://microstudio.dev/i/gilles/platformerbasics/

Post a reply

Progress

Status

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