Can resolve collision when AABB corners cut through triangle.
At least two more cases left to resolve.
This commit is contained in:
@@ -2,7 +2,9 @@
|
|||||||
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
|
<Entity xmlns:c="components" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../Types/Entity.xsd">
|
||||||
|
|
||||||
<Components>
|
<Components>
|
||||||
<c:Transform/>
|
<c:Transform>
|
||||||
|
<Position X="0" Y="0" Z="-0.313012958"/>
|
||||||
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
|
|
||||||
<Children>
|
<Children>
|
||||||
@@ -11,8 +13,8 @@
|
|||||||
<c:Camera/>
|
<c:Camera/>
|
||||||
<c:Listener/>
|
<c:Listener/>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Position X="5.01875639" Y="2.58534265" Z="3.01552486"/>
|
<Position X="2.56531811" Y="0.637967348" Z="0.202344239"/>
|
||||||
<Orientation X="-0.331608921" Y="1.01212442" Z="1.23717996e-06"/>
|
<Orientation X="2.68780756" Y="1.36143553" Z="3.14159179"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
<Children/>
|
<Children/>
|
||||||
@@ -21,25 +23,40 @@
|
|||||||
<Components>
|
<Components>
|
||||||
<c:Collidable/>
|
<c:Collidable/>
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>../assets/Models/Core/UnitCube.obj</Resource>
|
<Resource>../assets/Models/Core/UnitQuad.obj</Resource>
|
||||||
<Color A="1" B="0" G="0" R="1"/>
|
<Color A="1" B="1" G="0.392156869" R="0"/>
|
||||||
</c:Model>
|
</c:Model>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Scale X="0.200000003" Y="0.200000003" Z="0.200000003"/>
|
<Position X="-0.941270411" Y="-0.594426811" Z="2.90833044"/>
|
||||||
|
<Scale X="1.10000002" Y="1" Z="0.200000003"/>
|
||||||
|
<Orientation X="5.78700018" Y="2.59000015" Z="0"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
<Children/>
|
<Children>
|
||||||
|
<Entity>
|
||||||
|
<Components>
|
||||||
|
<c:Model>
|
||||||
|
<Resource>../assets/Models/Core/UnitQuad.obj</Resource>
|
||||||
|
<Color A="1" B="0" G="0" R="1"/>
|
||||||
|
</c:Model>
|
||||||
|
<c:Transform>
|
||||||
|
<Orientation X="0" Y="3.1415927" Z="4.71238898"/>
|
||||||
|
</c:Transform>
|
||||||
|
</Components>
|
||||||
|
<Children/>
|
||||||
|
</Entity>
|
||||||
|
</Children>
|
||||||
</Entity>
|
</Entity>
|
||||||
<Entity>
|
<Entity>
|
||||||
<Components>
|
<Components>
|
||||||
<c:AABB/>
|
<c:AABB/>
|
||||||
<c:Collidable/>
|
<c:Collidable/>
|
||||||
<c:Physics/>
|
|
||||||
<c:Model>
|
<c:Model>
|
||||||
<Resource>../assets/Models/Core/UnitCube.obj</Resource>
|
<Resource>../assets/Models/Core/UnitCube.obj</Resource>
|
||||||
</c:Model>
|
</c:Model>
|
||||||
|
<c:Physics/>
|
||||||
<c:Transform>
|
<c:Transform>
|
||||||
<Position X="1.96935618" Y="1.31790924" Z="-1.02448177"/>
|
<Position X="-0.30590561" Y="-0.251752943" Z="2.13862514"/>
|
||||||
</c:Transform>
|
</c:Transform>
|
||||||
</Components>
|
</Components>
|
||||||
<Children/>
|
<Children/>
|
||||||
|
|||||||
@@ -299,7 +299,7 @@ bool AABBvsTriangle(const AABB& box, const glm::vec3& v0, const glm::vec3& v1, c
|
|||||||
//closest to being perpendicular to the plane of the polygon.
|
//closest to being perpendicular to the plane of the polygon.
|
||||||
|
|
||||||
triNormal = glm::normalize(triNormal);
|
triNormal = glm::normalize(triNormal);
|
||||||
glm::vec3 diagonal = signNonZero(triNormal) * half;
|
glm::vec3 diagonal = -signNonZero(triNormal) * half;
|
||||||
#define EARLY_OUT_OR_MAYBE_JUST_EXTRA_WORK
|
#define EARLY_OUT_OR_MAYBE_JUST_EXTRA_WORK
|
||||||
#ifdef EARLY_OUT_OR_MAYBE_JUST_EXTRA_WORK
|
#ifdef EARLY_OUT_OR_MAYBE_JUST_EXTRA_WORK
|
||||||
//The triangle plane contains all points P in dot(triNormal, P) == dot(triNormal, v0)
|
//The triangle plane contains all points P in dot(triNormal, P) == dot(triNormal, v0)
|
||||||
@@ -323,9 +323,9 @@ bool AABBvsTriangle(const AABB& box, const glm::vec3& v0, const glm::vec3& v1, c
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
float dist = glm::dot(triNormal, origin + diagonal - v0);
|
//Distance between triangle plane, and the diagonal corner, multiplied by the normal.
|
||||||
outResolutionVector = dist * triNormal;
|
//Signed distance, positive if on the same side as the normal.
|
||||||
LOG_DEBUG("Triangle collision corner");
|
outResolutionVector = -glm::dot(triNormal, origin + diagonal - v0) * triNormal;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -51,7 +51,13 @@ void CollisionSystem::UpdateComponent(EntityWrapper& entity, ComponentWrapper& c
|
|||||||
auto absScale = Transform::AbsoluteScale(m_World, cModel.EntityID);
|
auto absScale = Transform::AbsoluteScale(m_World, cModel.EntityID);
|
||||||
glm::mat4 modelMatrix = glm::translate(absPosition) * glm::toMat4(absOrientation) * glm::scale(absScale);
|
glm::mat4 modelMatrix = glm::translate(absPosition) * glm::toMat4(absOrientation) * glm::scale(absScale);
|
||||||
|
|
||||||
RawModel* model = ResourceManager::Load<RawModel>(cModel["Resource"]);
|
RawModel* model;
|
||||||
|
try {
|
||||||
|
model = ResourceManager::Load<RawModel, true>(cModel["Resource"]);
|
||||||
|
} catch (const std::exception&) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
glm::vec3 resolutionVector;
|
glm::vec3 resolutionVector;
|
||||||
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
|
if (Collision::AABBvsTriangles(boxA, model->m_Vertices, model->m_Indices, modelMatrix, resolutionVector)) {
|
||||||
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
(glm::vec3&)cTransform["Position"] += resolutionVector;
|
||||||
|
|||||||
Reference in New Issue
Block a user