cleaned up collision code some

This commit is contained in:
Jeffrey Ventrella 2013-04-27 09:47:46 -07:00
parent 5c3bd5fe87
commit 57cbd9c6d9
2 changed files with 34 additions and 7 deletions

View file

@ -574,27 +574,33 @@ void Avatar::updateCollisionWithSphere( glm::vec3 position, float radius, float
}
//detect collisions with other avatars and respond
void Avatar::updateCollisionWithOtherAvatar( Avatar * otherAvatar, float deltaTime ) {
// check if the bounding spheres of the two avatars are colliding
glm::vec3 vectorBetweenBoundingSpheres(_position - otherAvatar->_position);
if ( glm::length(vectorBetweenBoundingSpheres) < _height * ONE_HALF + otherAvatar->_height * ONE_HALF ) {
// loop through the bones of each avatar to check for every possible collision
for (int b=1; b<NUM_AVATAR_BONES; b++) {
for (int o=b+1; o<NUM_AVATAR_BONES; o++) {
glm::vec3 vectorBetweenJoints(_bone[b].springyPosition - otherAvatar->_bone[o].springyPosition);
float distanceBetweenJoints = glm::length(vectorBetweenJoints);
// to avoid divide by zero
if ( distanceBetweenJoints > 0.0 ) {
float combinedRadius = _bone[b].radius + otherAvatar->_bone[o].radius;
if ( distanceBetweenJoints < combinedRadius * 1.8) {
// check for collision
if ( distanceBetweenJoints < combinedRadius * COLLISION_RADIUS_SCALAR) {
glm::vec3 directionVector = vectorBetweenJoints / distanceBetweenJoints;
_bone[b].springyVelocity += directionVector * 0.1f * deltaTime;
_velocity += directionVector * 3.0f * deltaTime;
// push ball away from colliding other ball and puch avatar body (_velocity) as well
_bone[b].springyVelocity += directionVector * COLLISION_BALL_FORCE * deltaTime;
_velocity += directionVector * COLLISION_BODY_FORCE * deltaTime;
// apply fruction to _velocity
float momentum = 1.0 - COLLISION_FRICTION * deltaTime;
if ( momentum < 0.0 ) { momentum = 0.0;}
_velocity *= momentum;
@ -605,6 +611,7 @@ void Avatar::updateCollisionWithOtherAvatar( Avatar * otherAvatar, float deltaTi
}
}
void Avatar::setDisplayingHead( bool displayingHead ) {
_displayingHead = displayingHead;
}

View file

@ -32,10 +32,29 @@ const float YAW_MAG = 500.0; //JJV - changed from 300.0;
const float TEST_YAW_DECAY = 5.0;
const float LIN_VEL_DECAY = 5.0;
const float COLLISION_FRICTION = 0.5;
const float COLLISION_FRICTION = 0.5;
const float COLLISION_RADIUS_SCALAR = 1.8;
const float COLLISION_BALL_FORCE = 0.1;
const float COLLISION_BODY_FORCE = 3.0;
enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH};
enum DriveKeys
{
FWD = 0,
BACK,
LEFT,
RIGHT,
UP,
DOWN,
ROT_LEFT,
ROT_RIGHT,
MAX_DRIVE_KEYS
};
/*
#define FWD 0
#define BACK 1
#define LEFT 2
@ -45,8 +64,9 @@ enum eyeContactTargets {LEFT_EYE, RIGHT_EYE, MOUTH};
#define ROT_LEFT 6
#define ROT_RIGHT 7
#define MAX_DRIVE_KEYS 8
*/
#define MAX_OTHER_AVATARS 10 // temporary - for testing purposes!
//#define MAX_OTHER_AVATARS 10 // temporary - for testing purposes!