Detecting the Direction of a Collision

前端 未结 5 860
执笔经年
执笔经年 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:32

    you have to model your square to capture the orientation too , just with (x,y,height) of the square , orientation i mean whether its in first ,second ,third of fourth quadrant , may be by modeling all four corner vertices of square .

    then you need to determine , all the four vectors that make this square

    find cosine between the 2D Vector you have with each side of square for example if cosine of given side and 2D Vector is 1 , then both are orthogonal or 0 they are perpendicular , any other value would fall in between

    or any other vector algebra tricks on how you want to determine/define your collision !

    0 讨论(0)
  • 2021-02-07 14:34

    Given r1 and r2 (r2 being stationary), first find the closest corner of r2 to r1. This point is (c1.x,c1.y) and imagine now you extend this out into two planes, one parallel to the x axis and one to the y.

    Now get the closest corner of r1 to r2 (call it c2) and use it in the following formula y = mx + b where b is c2.x and m is your vector. and x is c1.x

    So if y is greater than c1.y then it means at the point of x contact (width) you've already hit the top. If it's less, then you haven't hit it yet. Invert for bottom/top.

    0 讨论(0)
  • 2021-02-07 14:34

    Assuming you have a way to detect collisions, understanding which sides collided is straight forward. You simply need to examine the x and y positions of each square.

    Square1 : (x1, y1)
    Square2 : (x2, y2)

    I'll work from the assumption that the top left corner of your work area is (0,0) and that x values increase as you move right, and y values increase as you move down.

    With this in mind:

    If (x1 < x2), the right side of square 1 collided with the left side of square 2
    If (x1 > x2), the left side of square 1 collided with the right side of square 2
    if (y1 < y2), the bottom side of square 1 collided with the top side of square 2
    if (y1 > y2), the top side of square 1 collided with the bottom side of square 2

    I suggest you draw yourself a few pictures, and it should become clear to you.

    0 讨论(0)
  • 2021-02-07 14:35
    float player_bottom = player.get_y() + player.get_height();
    float tiles_bottom = tiles.get_y() + tiles.get_height();
    float player_right = player.get_x() + player.get_width();
    float tiles_right = tiles.get_x() + tiles.get_width();
    
    float b_collision = tiles_bottom - player.get_y();
    float t_collision = player_bottom - tiles.get_y();
    float l_collision = player_right - tiles.get_x();
    float r_collision = tiles_right - player.get_x();
    
    if (t_collision < b_collision && t_collision < l_collision && t_collision < r_collision )
    {                           
    //Top collision
    }
    if (b_collision < t_collision && b_collision < l_collision && b_collision < r_collision)                        
    {
    //bottom collision
    }
    if (l_collision < r_collision && l_collision < t_collision && l_collision < b_collision)
    {
    //Left collision
    }
    if (r_collision < l_collision && r_collision < t_collision && r_collision < b_collision )
    {
    //Right collision
    }
    

    This doesn't solve when object is inside one of the other. But it does work with overlapping

    0 讨论(0)
  • 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

    0 讨论(0)
提交回复
热议问题