Why does the size/extents of a bounding box of a BoxCollider in Unity show the Z element being double what it is?

As you can see from the picture below, I fetch the BoxCollider inside CollisionHandler and Debug.Log() it to the screen.

In this case, I’m logging the size, but the same thing happens with extents. There are 4 logs because I have 4 instances of the object in the scene.

The Z value of the Vector3 is double what it should be, whereas the other values are, for the most part, normal. Also, it seems that each object prints a slightly different value for extents.z. Most stay around 1.3, but one gave me 1.07~.

Additionally, it inverts the order for 2 of the objects. I don’t know why.

None of the objects are scaled and all use the exact same values for their collider.

Screenshot of Unity

I tried drawing a cube with the same size as the BoxCollider’s bounding box, and here’s what I got:

Bounding box drawn with Gizmo.DrawBox from the top

Bounding box drawn with Gizmo.DrawBox from the side

The bounding box gizmo and the box collider, side-by-side

The bounding box gizmo and the box collider, side-by-side from the top

I’m not entirely sure if the misalignment with the Collider in the scene is a sign of a problem or just how Gizmos.DrawCube() works. Here’s the code that does this:

private void OnDrawGizmosSelected() {
    Vector3 origin = coll.bounds.center + transform.TransformDirection(new 
    Vector3(0f, 0f, 0.675323f + skinWidth));
    // The hardcoded number above is Z extent of the box.
    Vector3 direction = transform.TransformDirection(Vector3.forward) * 2;

    Gizmos.color = Color.red;
    Gizmos.DrawRay(origin, direction);
    Gizmos.DrawCube(origin + direction, coll.bounds.size);
    // Where coll is a reference to the BoxCollider, cached in Awake().
}

Unity version 2019.3.15f1.

For the X value of 2.286476, it rounded to 2.1 in the 3rd log for some reason. And in the first log, it rounded the doubled value of Z to 2.4 rather than 2.6…

linear algebra – Bounding the smallest eigenvalue of a matrix generated by a positive definite function

Let $g:mathbb{T}tomathbb{R}$ and is given as $$g(x) = sumlimits_{etainmathbb{Z}}frac{1}{1+gamma eta^2}cos{2pieta x}$$

Consider the matrix $$G_{gamma} = (g(x_i-x_j))_{1le i,jle n}$$

where $x_1,x_2,…x_n in (0,1)$ and pairwise distinct.

Due to Bochner’s theorem, $g(x)$ is a positive semi definite function and hence $G_{gamma}$ is a psd matrix.

Let $lambda_{min}(G_{gamma})$ denote the smallest eigenvalue of $G_{gamma}$.

I want to show that $G_{gamma}$ is infact positive definite and as $gammatoinfty$ $$lambda_{min}(G_{gamma}) = Theta(frac{1}{gamma})$$

As a sanity check, I have verified this using numerical computations on some examples.

Motivation : I want to come up with a similar looking formula in a generic dimension, that is for $mathbb{T}^m$. If I am able to prove this for $m=1$ dimension, then I will understand the mechanics of it so that might help me to come up with a $g:mathbb{T}^m to mathbb{R}$ such that $lambda_{min}(G_{gamma}) = Theta(frac{1}{gamma})$

pr.probability – Upper bounding the sum with hypergeometric and binomial probabilities

Could you please help me upper bound this tricky expression:

$$P(A)=sum_{i=0}^n{left( 1 – dfrac{binom kq binom {n-k}{i-q}}{binom {n}{i}} right)}^I binom ni p^i {(1-p)}^{n-i}$$.

So far I only managed to obtain a lower bound by proving $sum_{i=0}^n{dfrac{binom kq binom {n-k}{i-q}}{binom {n}{i}}binom ni p^i {(1-p)}^{n-i}}=binom kq p^q {(1-p)}^{k-q}$ and applying Jensen’s inequality for power function $f(x):=x^I$.

This lower bound is $P(A) geq {left(1-binom kq p^q {(1-p)}^{k-q}right)}^I$ and works okay for my needs. However, I would like to get a similar upper bound too and this is what causes the main difficulty.

Do you have any suggestions?

optimization – Bounding 0-1 matrix with k unique rows

Problem Statement:
Suppose that I have a $0-1$ matrix $A$ (all of the entries are $0$ or $1$). I wish to find the tightest upper bound with $k$ many unique rows. To be more precise, let S denote the set of $0-1$ matrices $B$ such that it only has $k$ unique rows, $A_{ij} leq B_{ij}$ for all $i$ and $j$.
Find
$$min_{B in S} ||A – B||$$

Example:
Suppose $k = 2$ and
$$A = begin{bmatrix}
1 & 0 & 0 & 0 & 0\
1 & 1 & 0 & 0 & 0\
0 & 0 & 1 & 0 & 1\
0 & 0 & 1 & 1 & 0\
end{bmatrix}$$

Then the optimal matrix $B$ is
$$A = begin{bmatrix}
1 & 1 & 0 & 0 & 0\
1 & 1 & 0 & 0 & 0\
0 & 0 & 1 & 1 & 1\
0 & 0 & 1 & 1 & 1\
end{bmatrix}$$

Since $B$ only has $2$ distinct rows, $A leq B$, and $||A – B|| = 3$ is minimized.

Question 1:
This problem reminds of the minimum $k$-union, set-union, and other NP-complete problems. Is this problem an NP-complete optimization problem?

Question 2:
Is there an efficient way to obtain approximately optimal matrix $B in S$? Instead of minimizing $||A – B||$, can we get close to the minimum possible value?

So far, I have tried to cluster each row of matrix $A$ using k-means. Then within each cluster $i$, I tried to construct a vector $v_i$. Where $j^{th}$ entry of $v_i$ is $1$ if at least p-percent of the vectors in cluster $i$ has $j^{th}$ entry to be $1$. The vectors $v_i$ served as initial potential guess for possible rows of the matrix $B$. Then I used greedy algorithm. This has decent performance, but it’s not great.

opengl – How do I calculate the bounding box for an ortho matrix for Cascaded shadow mapping?

I’ve been trying to get a cascaded shadow mapping system implemented on my engine, though it appears to be that the bounding boxes for the cascades aren’t correct.

The part I’m interested in can be found here, under the function name “CalcOrthoProjs”.

I’ve been trying to understand the matrix multiplications with this answer, and the ogldev variable and function names are kind of confusing me.

This is how I modified ogldev’s function to work with my variables:

void Scene::calcOrthoProjections(Camera &camera, glm::mat4 LightView, std::vector<glm::mat4> &orthoContainer, std::vector<GLfloat> &cascadeEnd) {
GLfloat FOV, nearPlane, farPlane, ratio;
camera.getPerspectiveInfo(FOV, nearPlane, farPlane);
ratio = static_cast<GLfloat>(RE::config.height) / static_cast<GLfloat>(RE::config.width);

GLfloat tanHalfHFov = glm::tan(glm::radians(FOV / 2.0f));
GLfloat tanHalfVFov = glm::tan(glm::radians((FOV*ratio) / 2.0));


for (GLuint i = 0; i < RE::config.r_shadow_cascade_factor; i++) {
    GLfloat xn = cascadeEnd(i)     * tanHalfHFov;
    GLfloat xf = cascadeEnd(i + 1) * tanHalfHFov;
    GLfloat yn = cascadeEnd(i)     * tanHalfVFov;
    GLfloat yf = cascadeEnd(i + 1) * tanHalfVFov;

    //The frustum Corners on View(Camera) space
    glm::vec4 frustumCorners(8){
        //Near Face
        glm::vec4( xn,  yn, cascadeEnd(i), 1.0),
        glm::vec4(-xn,  yn, cascadeEnd(i), 1.0),
        glm::vec4( xn, -yn, cascadeEnd(i), 1.0),
        glm::vec4(-xn, -yn, cascadeEnd(i), 1.0),
        //Far face
        glm::vec4( xf,  yf, cascadeEnd(i + 1), 1.0),
        glm::vec4(-xf,  yf, cascadeEnd(i + 1), 1.0),
        glm::vec4( xf, -yf, cascadeEnd(i + 1), 1.0),
        glm::vec4(-xf, -yf, cascadeEnd(i + 1), 1.0),


    };

    //The frustum Corners in LightSpace
    glm::vec4 frustumCornersL(8);

    GLfloat minX, maxX, minY, maxY, minZ, maxZ;
    minX = minY = minZ = std::numeric_limits<GLfloat>::max();
    maxX = maxY = maxZ = std::numeric_limits<GLfloat>::min();

    glm::mat4 cam = (camera.getProjection() * camera.getView());
    glm::mat4 camInverse = glm::inverse(cam);

    for (GLuint j = 0; j < 8; j++) {
        //View(Camera) space to world space
        glm::vec4 vW = camInverse * frustumCorners(j);

        //world space to light space
        frustumCornersL(j) = LightView * vW;

        minX = min(minX, frustumCornersL(j).x);
        maxX = max(maxX, frustumCornersL(j).x);
        minY = min(minY, frustumCornersL(j).y);
        maxY = max(maxY, frustumCornersL(j).y);
        minZ = min(minZ, frustumCornersL(j).z);
        maxZ = max(maxZ, frustumCornersL(j).z);
    }
    orthoContainer(i) = glm::ortho(minX, maxX, minY, maxY, minZ, maxZ) * LightView;
 }
}

LightView represents a matrix created with:

glm::LookAt(-glm::normalize(light.direction), glm::vec3(0.0), glm::vec3(0.0, 1.0, 0.0))

camera.getProjection() returns the perspective matrix of the main camera

camera.getView() returns a LookAt at the objective the camera is looking at

The orthoContainer values are then fed into the depth rendering unaltered afterwards (by anything other than the model matrix of each model)

I wrote some comments on how I think the math is done, trying to understand what’s wrong.

The result is a frustum too wide, resulting on low res shadows(even for the closest shadow map):
Low res shadows

and this is the depth map of the closest shadow map:
Depth buffer of the closest shadow map

any insight as to why this isn’t working, or any other best practice advice, is welcome. Thanks in advance!

co.combinatorics – Upper bounding VC dimension of an indicator function class

I would like to upper bound the VC dimension of the function class $ F$ defined as follows:

$$ F := left{ (x,t) mapsto mathbb{1} left( c_Qmin_{q in Q} {|x-q |}_1 – t > 0 right) ; | ; Q subset mathbb{R}^{d}, |Q| = k right}, $$

where $k$ is a fixed positive integer, $x,q in mathbb{R}^d, t in mathbb{R}, mathbb{1}(A) $ denotes the indicator function (=1 if A is true and 0 otherwise), $| cdot | _ 1 $ is the L1 norm, and $c_Q$ is a constant that depends on $Q$.

Context: I am studying a grouping procedure that minimizes $L_1$ norm. In particular, I would like understand how the complexity of the class of functions $left{ c_Q min_{q in Q} {|x-q |}_1 ; | ; Q subset mathbb{R}^{d}, |Q| = k right}$ scales with $k$ and $d$ (i.e., $O(d k log k)$) . The above is a generalization of VC-dimension called Pseudodimension.

I would appreciate any suggestions you might have.
Thanks!

cv.complex variables – Bounding the absolute value of a complex integral

I’m working on some problems involving Fourier transforms and convolution problems and there is one problem I cannot solve. In my situation we have a complex valued function $f(ix)$, with $xinmathbb{R}$ and two functions from which we know the following: $|g_2(ix)|le|g_1(ix)|=1$.

The convolution problem is the following, I have two functions (spectra in my case) that are defined as
$$ U_1(iomega) = int_{-infty}^infty f(ix)g_1(i(omega-x))mathrm{d}x $$
$$ U_2(iomega) = int_{-infty}^infty f(ix)g_2(i(omega-x))mathrm{d}x $$
assuming these exists. I want to show that $|U_2(iomega)|le|U_1(iomega)|$ for all $omega$. Intuitively, this is true, since the magnitude of $g_1$ is equal to $1$ and $g_2$ is less or equal to $1$. Also, when I perform some simulation experiments with random functions $f$, I obtain that this relationship holds, however I cannot prove it. sadface

What I have been able to show is this:
$$|U_1(iomega)| = int_{-infty}^infty f(ix)g_1(i(omega-x))mathrm{d}x le int_{-infty}^infty |f(ix)g_1(i(omega-x))|mathrm{d}x =int_{-infty}^infty |f(i(omega-x))|mathrm{d}x$$
$$|U_2(iomega)| = int_{-infty}^infty f(ix)g_2(i(omega-x))mathrm{d}x le int_{-infty}^infty |f(ix)g_2(i(omega-x))|mathrm{d}x leint_{-infty}^infty |f(i(omega-x))|mathrm{d}x$$
Hence, the upperbound on $|U_2|$ is less or equal than the upperbound on $|U_1|$, however, this does not guarantee that $|U_2(iomega)|le|U_1(iomega)|$ for all $omega$.

Do you maybe have any idea how to approach this further?

unity – How do I stretch a mesh with a cube bounding box by moving the cube’s corners

Basic Problem in 2D

I have a square space, which contains an irregular pattern. I want to move the corners of this square arbitrarily and have the pattern within stretch evenly to fill the space.

Illustration of square being stretched into an arbitrary quad

The simplest solution I can conceive would be:

  • For every point in the square, express it’s position in relation to the square (ie. an x,y value between 0 and 1 where 0,0 is the bottom left corner, and 1,1 is the top right corner)
  • Use those values to interpolate between the bottom left and top right corners of the deformed quad to find its correct position in that quad.

I think that may work fine in 2D, but it seems like a very naive approach that may give odd looking results in 3D.

Real Problem in 3D

The above is a simple formulation of the problem, but what I’m actual to aiming for is to recreate the irregular grid cityscape illustrated here and here.

grid illustration

I have the grid, but now I need to deform the tiles (which are cube shaped by default) to fit into the grid’s irregular spaces. Every side of a space is straight, the only thing that’s moved from a regular square is the corners.

I suspect that if I follow the simple method I explained above (but in 3 dimensions) that the skewed tiles won’t look very good, and I think there must be some other more well accepted method of accomplishing this (such as the coordinate systems described in this paper).

This seems like quite a fundamental challenge which would come up all the time, but I can’t find anything on the internet that explains the best way to handle it in simple terms. Mainly I get Unity questions complaining about unintended distortions, or I get dense mathematics papers which seem only indirectly related. I suspect I just don’t know the right words to search for.

The main thing that makes me suspect that my solution above wouldn’t work well, is this paper: “Mean value coordinates for quad cages in 3D” which directly addresses my goal, and shows how different methods can cause unintended distortions. Unfortunately though, the explanation is too advanced for me to easily understand.