• Create Account

# Ellipsoid Collision Detection and Response - Incorrect Intersection Point

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

No replies to this topic

### #1BlueSpud  Members   -  Reputation: 543

Like
0Likes
Like

Posted 05 May 2014 - 05:56 PM

I've implemented the collision detection and response outlined in this paper:http://www.peroxide.dk/papers/collision/collision.pdf The collision detection of the whole thing works fine, but some quirky things happen when I try to do the collision response. Here is my current response code that (for the most part) works. - A side note: This code is just included in case the problem does not lie in the spots I have speculated it does. This code does not have to be read unless there is no obvious mistake in the code I have put further down below and have determined to be the culprit.

if (pysObjs.size() > 0)
{
//here we're going to test all the collision objects and apply the collision response to the nearest collision
glm::dvec3 velocity = playerESpace.vecInElipsoidSpace(glm::dvec3(-camx,-camy,-camz)-glm::dvec3(-lcamx,-lcamy,-lcamz));
glm::dvec3 basePos = playerESpace.vecInElipsoidSpace(glm::dvec3(-lcamx,-lcamy,-lcamz));
glm::dvec3 destination = playerESpace.vecInElipsoidSpace(glm::dvec3(-camx,-camy,-camz));
int object = 0;
double distance = 0.0;
bool initial = false;
std::vector<collisionResultsPacket>results;
for (int i = 0; i < pysObjs.size(); i++)
{
//we need to test each mesh and see which is the closest triangle from all the meshes
pysObjs[i].testCollisionMeshWithElipsoid(0);

//we just got the closest result for that mesh
//now we test if that is closer than the one we already have
if (initial == false || pysObjs[i].closestResults.distance < distance )
{
if (pysObjs[i].closestResults.packet.collided == true)
{
initial = true;
distance = pysObjs[i].closestResults.distance;
object = i;
}
}
}

//now we have the colsest collision point and we can calculate the collision response properly
if (pysObjs[object].closestResults.packet.collided == true )
{
basePos = basePos + velocity*(pysObjs[object].closestResults.packet.time0*0.3f);

glm::dvec3 newIntersectionPoint = pysObjs[object].closestResults.packet.intersectionPoint;

if (distance >= veryCloseDistance)
{
//velocity = glm::normalize(velocity)*(distance-veryCloseDistance);
//basePos = basePos+velocity;

velocity = glm::normalize(velocity);
newIntersectionPoint = pysObjs[object].closestResults.packet.intersectionPoint - (veryCloseDistance*velocity);

}

glm::dvec3 slidingNormal = glm::normalize(pysObjs[object].closestResults.packet.normal);

double distancetoCollision = glm::dot(destination, slidingNormal) - ((slidingNormal.x*basePos.x)+(slidingNormal.y*basePos.y)+(slidingNormal.z*basePos.z));
destination = destination - distancetoCollision*slidingNormal;

destination = -destination*glm::inverse(playerESpace.CBM);
basePos = -basePos*glm::inverse(playerESpace.CBM);

camx = destination.x;
camy = destination.y;
camz = destination.z;

lcamx = basePos.x;
lcamy = basePos.y;
lcamz = basePos.z;

}
} 

You'll notice that the sliding plane's origin is not the intersection point.This is because the point is being calculated incorrectly as a bit of visual debugging has show. When the final position is set to the intersection point, the camera moves rather far away from the triangle. The code above works rather well, I just need to figure out why. I'v triple checked the code and I can't find any discrepancies between mine and the original, other than the pieces I've changed. Because none of the data in the response function is actually generated here, here is the code for the physicsObjects that actually check the collision.

//
// physicObject.cpp
// Injection
//
// Created by Logan on 8/7/13.
//

#include "physicObject.h"
{
model = ModelRegistry.accessModel(file);
collisionEnabled = true;
//we have to get the bounding box

}
bool physicsObject::onSameSide(glm::dvec3 point, glm::dvec3 p1, glm::dvec3 p2, glm::dvec3 p3)
{
glm::dvec3 cross1 = glm::cross(p3-p2, point-p2);
glm::dvec3 cross2 = glm::cross(p3-p2, p1-p2);
if (glm::dot(cross1, cross2) >= 0.0)
return true;
else return false;

}
collisionResponsePacket physicsObject::checkTriangle(unsigned int i)
{
collisionResponsePacket packetToReturn;
glm::dvec3 velocity = playerESpace.vecInElipsoidSpace(glm::dvec3(-camx,-camy,-camz)-glm::dvec3(-lcamx,-lcamy,-lcamz));
glm::dvec3 basePos = playerESpace.vecInElipsoidSpace(glm::dvec3(-lcamx,-lcamy,-lcamz));

//for now, we're just going to check the one triangle.
//.obj stores vertexes in counter-clockwise order, so we're going to swap the positions.
glm::dvec3 p1 = playerESpace.vecInElipsoidSpace((glm::dvec3(ModelRegistry.models[model].m.obj[i].x3,ModelRegistry.models[model].m.obj[i].y3,ModelRegistry.models[model].m.obj[i].z3)));
glm::dvec3 p2 = playerESpace.vecInElipsoidSpace((glm::dvec3(ModelRegistry.models[model].m.obj[i].x2,ModelRegistry.models[model].m.obj[i].y2,ModelRegistry.models[model].m.obj[i].z2)));
glm::dvec3 p3 = playerESpace.vecInElipsoidSpace((glm::dvec3(ModelRegistry.models[model].m.obj[i].x1,ModelRegistry.models[model].m.obj[i].y1,ModelRegistry.models[model].m.obj[i].z1)));

//get the normal
glm::dvec3 normal = glm::normalize(glm::cross(p2-p1, p3-p1));

//get signed distance
double distance = glm::dot(normal, basePos) - ((normal.x*p1.x)+(normal.y*p1.y)+(normal.z*p1.z));
double nvd = glm::dot(normal, velocity);

//if its equal to zero, the elipse is traveling parallel to the triangle
if (nvd != 0.0)
{
//figure out what time the sphere would intersect with the collision plane, it doesn't nessesarily have to
double time0,time1;
time0 = (-1.0f-distance)/nvd;
time1 = (1.0f-distance)/nvd;

if (time0 > time1)
{
double temp = time1;
time1 = time0;
time0 = temp;
}
//check if there can actually be a collision
if (time0 > 1.0f || time1 < 0.0f)
return packetToReturn;
else
{
//fix thew times if there was a possible collision
if (time0 < 0.0) time0 = 0.0;
if (time1 < 0.0) time1 = 0.0;
if (time0 > 1.0) time0 = 1.0;
if (time1 > 1.0) time1 = 1.0;

//continue to look for a collision
glm::dvec3 planeIntersectionPoint = (basePos-normal) + time0*velocity;
//now we have where the elipsoide will collide with the trangle, now we check if it is an actual collision
if (onSameSide(planeIntersectionPoint, p1, p2, p3) && onSameSide(planeIntersectionPoint, p2, p1, p3) && onSameSide(planeIntersectionPoint, p3, p1, p2))
{
//there was a collision here so we give the packet the proper information
packetToReturn.intersectionPoint = planeIntersectionPoint;
packetToReturn.time0 = time0;
packetToReturn.time1 = time1;
packetToReturn.collided = true;
packetToReturn.normal = normal;
}
else return packetToReturn;
}

} else return packetToReturn;

return packetToReturn;
}

void physicsObject::testCollisionMeshWithElipsoid(double time)
{
//some of the vectors we require for the response
glm::dvec3 velocity = playerESpace.vecInElipsoidSpace(glm::dvec3(-camx,-camy,-camz)-glm::dvec3(-lcamx,-lcamy,-lcamz));
std::vector<collisionResponsePacket>results;

for (int i = 0; i < ModelRegistry.models[model].m.obj.size(); i++)
//we chech each triangle
results.push_back(checkTriangle(i));

double closest = 0.0;
int triangle = 0;
bool initial = false;
//now we find which one we are going to respond against, then do it
for (int i = 0; i < results.size(); i++)
{
if (results[i].collided == true)
{
//get the distance to the collision
double distanceToCollision = results[i].time0*glm::length(velocity);
if (initial == false || distanceToCollision < closest )
{
//this one was less than the previos one
initial = true;
closest = distanceToCollision;
triangle = i;
}

}
}
//we store the information for the closest collision here
closestResults.i = triangle;
closestResults.distance = closest;
closestResults.packet = results[triangle];
results.clear();



}

The culprit I suspect is either in one of the two following places. This is the code I think needs to be looked at by a second pair of eyes, not necessarily the entire code above.

glm::dvec3 planeIntersectionPoint = (basePos-normal) + time0*velocity;

And the other piece I suspect could cause problems:

double distanceToCollision = results[i].time0*glm::length(velocity);

These are really the only things that are really used in the final collision detection. I'm satisfied with the outcome of the method that I implemented. Its just one of those things that keeps me awake at night. "Why didn't the original code work? Why didn't what the paper said work correctly?" The problem could easily lie in my implementation of the algorithm and if I had the choice, I'd re-write the whole program with this routine in mind. Unfortunately I can't do that. Any thought or input into why the original solution to the collision and response problem did not work for me would be much appreciated and it may help people having perhaps the same problem as me in the future.

Thank you.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS