Discord
Login
Community
DARK THEME

how do I have wall collisions

so I'm trying to make a horror game because this game does not have any (I think) and not even 20 minutes in I notice the problem I cant get the player to colide with the wall it just goes thru it and I don't want to have to put 50 lines of code into a wall just to make it function here is the code I got currently

init = function()
  player = object end
  player.x = 0
  player.y = 0
  player.speed = 2
end

update = function()
  if keyboard.W then
    player.y += player.speed
  elsif keyboard.S then
    player.y -= player.speed
  elsif keyboard.A then
    player.x -= player.speed
  elsif keyboard.D then
    player.x += player.speed
  end
end

draw = function()
  screen.clear("black")
  screen.drawText(player.x + " X", 0 ,50, 20, "white")
  screen.drawSprite("player", player.x, player.y, 25, 25)
  screen.drawSprite("wall_1", 20, 20, 40, 40)
end

Hi, so to make the player blocked by the wall you need:

1- a collision function. I suggest you use mine; it works. Add it to your code. You can use it by saying checkCollision(obj1,obj2) whith the two object you want to collide with. Here it is.

  return o1.x - o1.w/2 < o2.x + o2.w/2 and
         o1.x + o1.w/2 > o2.x - o2.w/2 and
         o1.y - o1.h/2 < o2.y + o2.h/2 and
         o1.y + o1.h/2 > o2.y - o2.h/2
end

2- Secondly, you need logic that blocks the player when they collide with another player. Hmm... looking at your code, I suggest creating variables that save the player's previous position to block them if there's a collision. Let's see... Ah, I've found the perfect logic for you. Here I'm using an object, but if you want more options, use a class. Here's an example based on your code.

//collide function
checkCollision = function(o1, o2)
  return o1.x - o1.w/2 < o2.x + o2.w/2 and
         o1.x + o1.w/2 > o2.x - o2.w/2 and
         o1.y - o1.h/2 < o2.y + o2.h/2 and
         o1.y + o1.h/2 > o2.y - o2.h/2
end


init = function()
  player = object
    x = 0
    y = 0
    w = 30
    h = 30 /*
    You need to have size variables for my function to work.
    like w (for the width)
    and
    h(for height)
    */
    oldX = 0
    oldY = 0
    color = "rgb(255,170,0)"
    color2 ="rgb(147,98,0)"
    speed = 3 
    collide = false
  end
  
  wall = object
    x = -100
    y = -20
    w = 70
    h = 70
    /* The same goes for your wall; it must also have size variables.
    */
  end
  
end

update = function()
  /*We will connect the oldX to the player.x.
 And the same for y; they will be like variables that save the previous position of your player.
*/
  
  player.oldX = player.x
  player.oldY = player.y
  
  //mouvement logique
  if keyboard.UP then
    player.y += player.speed
  elsif keyboard.DOWN then
    player.y -= player.speed
  elsif keyboard.LEFT then
    player.x -= player.speed
  elsif keyboard.RIGHT then
    player.x += player.speed
  end
  
  //the function of the solid collision with the wall
  if checkCollision(player,wall) then
    player.collide = true
  else
    player.collide = false
  end
  
  if player.collide then
    player.x = player.oldX
    player.y = player.oldY
  end
end

draw = function()
  screen.clear("rgb(0,170,255)")
  
  // Wall
  screen.fillRect(wall.x,wall.y,wall.w,wall.h,"rgb(141,141,141)")
  screen.drawRect(wall.x,wall.y,wall.w,wall.h,"rgb(97,97,97)")
  
  // Player
  screen.fillRect(player.x,player.y,player.w,player.h,player.color)
  screen.drawRect(player.x,player.y,player.w,player.h,player.color2)
end

I think it should work. If you have any other questions, let me know. Good luck with your horror game! ^o^

how do I make it apply for all walls because its not just gonna be 1 wall block its gonna be way more then that

As I said, you create either a class or a map array. If you want more

Post a reply

Progress

Status

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