【问题标题】:How to determine the normal when ball hits rectangle?球击中矩形时如何确定法线?
【发布时间】:2019-09-12 01:24:38
【问题描述】:

我已经定义了这个常量,但我不确定如何找到正常的索引。

public static final int LEFT = 0;
public static final int RIGHT = 1;
public static final int UP = 2;
public static final int DOWN = 3;

public float[] normals = {-1, 0, 1, 0, 0, 1, 0, -1};

public float4 getNormal(int index)
{
    return new float4(normals[index * 2], normals[index * 2 + 1], 0);
}

public static int normal(float x, float y, RectF rect)
{
    // need to find the normal index here
    return normal_index;
}

【问题讨论】:

    标签: android collision


    【解决方案1】:

    我认为这应该适用于内部和外部点(半径中心):

    public static int normal_index(float x, float y, RectF rect)
    {
        float cx = rect.left + rect.width() * 0.5f;
        float cy = rect.top + rect.height() * 0.5f;
    
        boolean a = x - rect.left < rect.bottom - y;
        boolean b = x - rect.left < y - rect.top;
        boolean c = x - rect.right < y - rect.bottom;
        boolean d = x - rect.right < rect.top - y;
    
        if (a && b && x < cx ) return LEFT;     
        else if (!c && !d && x >= cx ) return RIGHT;        
        else if (!a && c && y >= cy ) return DOWN;      
        else if (!b && d && y < cy ) return UP;
    
        return -1;
    }
    

    这个问题似乎与这个问题有关Given a point inside a rectangle, determine the side that's closest to the point

    对于 a、b、c、d 行,我有类似的图片:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-12-28
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多