コード例 #1
0
    //Are two AABB intersecting?
    private void AABB_AABB()
    {
        MyVector2 t1_p1 = t1_p1_trans.position.ToMyVector2();
        MyVector2 t1_p2 = t1_p2_trans.position.ToMyVector2();
        MyVector2 t1_p3 = t1_p3_trans.position.ToMyVector2();

        MyVector2 t2_p1 = t2_p1_trans.position.ToMyVector2();
        MyVector2 t2_p2 = t2_p2_trans.position.ToMyVector2();
        MyVector2 t2_p3 = t2_p3_trans.position.ToMyVector2();

        Triangle2 t1 = new Triangle2(t1_p1, t1_p2, t1_p3);
        Triangle2 t2 = new Triangle2(t2_p1, t2_p2, t2_p3);

        bool isIntersecting = Intersections.AABB_AABB_2D(
            t1.MinX(), t1.MaxX(), t1.MinY(), t1.MaxY(),
            t2.MinX(), t2.MaxX(), t2.MinY(), t2.MaxY());

        Debug.Log("AABB intersecting: " + isIntersecting);

        //Display the rectangles and the vertices we use to make the rectangles
        Vector3 r1_size = new Vector3(t1.MaxX() - t1.MinX(), 0.01f, t1.MaxY() - t1.MinY());
        Vector3 r2_size = new Vector3(t2.MaxX() - t2.MinX(), 0.01f, t2.MaxY() - t2.MinY());

        Vector3 r1_center = new Vector3(t1.MinX() + (r1_size.x * 0.5f), 0f, t1.MinY() + (r1_size.z * 0.5f));
        Vector3 r2_center = new Vector3(t2.MinX() + (r2_size.x * 0.5f), 0f, t2.MinY() + (r2_size.z * 0.5f));

        Gizmos.color = Color.white;

        Gizmos.DrawCube(r1_center, r1_size);

        float r = 0.1f;

        //Gizmos.DrawWireSphere(t1_p1.ToVector3(), r);
        //Gizmos.DrawWireSphere(t1_p2.ToVector3(), r);
        //Gizmos.DrawWireSphere(t1_p3.ToVector3(), r);

        Gizmos.color = isIntersecting ? Color.red : Color.white;


        Gizmos.DrawCube(r2_center, r2_size);

        //Gizmos.DrawWireSphere(t2_p1.ToVector3(), r);
        //Gizmos.DrawWireSphere(t2_p2.ToVector3(), r);
        //Gizmos.DrawWireSphere(t2_p3.ToVector3(), r);
    }