• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BlueSpud

Ellipsoid Collision Detection and Response - Incorrect Intersection Point

0 posts in this topic

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.
// Copyright (c) 2013 Logan. All rights reserved.
//

#include "physicObject.h"
void physicsObject::loadCollisionMesh(std::string file)
{
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.

0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0