Fixed AABB hit normal.

This commit is contained in:
Branimir Karadžić 2017-09-28 21:50:11 -07:00
parent 5e40a84769
commit 783db66079
3 changed files with 49 additions and 31 deletions

View File

@ -175,6 +175,7 @@ public:
{ 5.0f, 1.0f, 1.0f },
{ 10.0f, 5.0f, 5.0f },
};
ddSetWireframe(true);
ddSetColor(intersect(ray, aabb) ? selected : 0xff00ff00);
ddDraw(aabb);
ddPop();
@ -189,9 +190,12 @@ public:
bx::mtxSRT(obb.m_mtx, 1.0f, 1.0f, 1.0f, time*0.23f, time, 0.0f, 3.0f, 0.0f, 0.0f);
toAabb(aabb, obb);
ddSetColor(0xff0000ff);
ddDraw(aabb);
ddPush();
toAabb(aabb, obb);
ddSetWireframe(true);
ddSetColor(0xff0000ff);
ddDraw(aabb);
ddPop();
ddSetWireframe(false);
ddSetColor(intersect(ray, obb) ? selected : 0xffffffff);
@ -316,9 +320,12 @@ public:
ddSetColor(intersect(ray, cylinder) ? selected : 0xffffffff);
ddDraw(cylinder);
toAabb(aabb, cylinder);
ddSetColor(0xff0000ff);
ddDraw(aabb);
ddPush();
toAabb(aabb, cylinder);
ddSetWireframe(true);
ddSetColor(0xff0000ff);
ddDraw(aabb);
ddPop();
ddPop();

View File

@ -523,9 +523,9 @@ bool intersect(const Ray& _ray, const Aabb& _aabb, Hit* _hit)
if (NULL != _hit)
{
_hit->m_normal[0] = float( (min[0] == tmin) - (max[0] == tmin) );
_hit->m_normal[1] = float( (min[1] == tmin) - (max[1] == tmin) );
_hit->m_normal[2] = float( (min[2] == tmin) - (max[2] == tmin) );
_hit->m_normal[0] = float( (t1[0] == tmin) - (t0[0] == tmin) );
_hit->m_normal[1] = float( (t1[1] == tmin) - (t0[1] == tmin) );
_hit->m_normal[2] = float( (t1[2] == tmin) - (t0[2] == tmin) );
_hit->m_dist = tmin;
getPointAt(_hit->m_pos, _ray, tmin);
@ -779,11 +779,11 @@ bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
return hit;
}
float tmp[3];
getPointAt(tmp, _ray, tt);
float hitPos[3];
getPointAt(hitPos, _ray, tt);
float point[3];
bx::vec3Sub(point, tmp, _cone.m_pos);
bx::vec3Sub(point, hitPos, _cone.m_pos);
const float hh = bx::vec3Dot(normal, point);
@ -800,12 +800,13 @@ bool intersect(const Ray& _ray, const Cone& _cone, Hit* _hit)
{
_hit->m_dist = tt;
bx::vec3Move(_hit->m_pos, point);
bx::vec3Move(_hit->m_pos, hitPos);
const float scale = hh / bx::vec3Dot(point, point);
float pointScaled[3];
bx::vec3Mul(pointScaled, point, scale);
float tmp[3];
bx::vec3Sub(tmp, pointScaled, normal);
bx::vec3Norm(_hit->m_normal, tmp);
}

View File

@ -1103,29 +1103,39 @@ struct DebugDraw
void draw(const Aabb& _aabb)
{
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
close();
const Attrib& attrib = m_attrib[m_stack];
if (attrib.m_wireframe)
{
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
close();
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
close();
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
close();
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
moveTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_min[1], _aabb.m_max[2]);
moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
moveTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_min[1], _aabb.m_max[2]);
moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
moveTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_min[0], _aabb.m_max[1], _aabb.m_max[2]);
moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
moveTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_min[2]);
lineTo(_aabb.m_max[0], _aabb.m_max[1], _aabb.m_max[2]);
}
else
{
Obb obb;
aabbToObb(obb, _aabb);
draw(Mesh::Cube, obb.m_mtx, 1, false);
}
}
void draw(const Cylinder& _cylinder, bool _capsule)