Detecting the Direction of a Collision

前端 未结 5 864
执笔经年
执笔经年 2021-02-07 13:52

A square tile collides with another square tile. The bartender says...

I have:

  • The height, width, x, and y of both tiles.
  • The 2D vector of the mo
5条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-02-07 14:41

      if(player.y <= wall.y + wall.height && player.y > wall.y || player.y + player.height  <= wall.y+wall.height && player.y + player.height > wall.y){
                if(player.x -2< wall.x + wall.width && player.x > wall.x){
                    leftspeed = 0
                }else if(player.x + player.width +2 > wall.x && player.x + player.width < wall.x + wall.width){
                    rightspeed = 0
                }
            }
    
            if(player.x <= wall.x + wall.width && player.x > wall.x || player.x + player.width  <= wall.x+wall.width && player.x + player.width > wall.x){
                if(player.y -2 < wall.y + wall.height && player.y > wall.y){
                    upspeed = 0
                }else  if(player.y + player.height +2> wall.y && player.y + player.height < wall.y + wall.height){
                    downspeed = 0
                }
            }
    

    this should work, there is a 2 pixel margin around everything that you may want to remove

提交回复
热议问题