So I have been working on collision for my camera and Objects.
i have it working pretty good but i have some issue with one wall not detecting any collision and going right though it.
i have attach the source and an exe of the demo if any one want to see it in action.
but if anyone can could give me a hand to get the collision working i would appreate it. the collision is based off Kensupen's collision.
here is a the collision code
#include "..\\stdafx.h"
#include <algorithm>
// used for the stl sort function.
bool operator<(const s_Dist& a, const s_Dist& b) {
return a.Dist < b.Dist;
}
Collision::Collision(){
// will be used for cam collision.
radius = 15; // may change this depending on how it works
checks = 8; // Cam collision needs to be smoother then object collision.
angwidth = 360/checks;
}
Collision::~Collision(){}
void Collision::Collide( BasicObject& Player, BasicObject& Level )
{
/*************************************************
* This Function is for Object Collision *
* Camera has it's own collision system so it can *
* be optimized just for camera movement. *
*************************************************/
// Make sure object cords are correct.
// this needs to be called or else a bug in the camera shows up.
Player.x = Player.PositionX();
Player.y = Player.PositionY();
Player.z = Player.PositionZ();
for ( int xx=0; xx < checks-1; xx++ )
{
chx = Player.x + ( 100 * sin(xx*angwidth*1.f) );
chz = Player.z + ( 100 * cos(xx*angwidth*1.f) );
stlsDist[xx].Dist = dbIntersectObject( Level.O->Ref(),Player.x,2,Player.z,chx,Player.y,chz );
stlsDist[xx].Ang = (float)xx*angwidth;
stlsort[xx] = stlsDist[xx];
}
// testing stl sort routine
std::sort(stlsDist, stlsDist+checks-1);
dbText(400,60,"Angle to closest wall:");
dbText(400,80,str(stlsDist[xx].Ang) );
dbText(400,100,"Distance to that wall:");
dbText(400,120,str(stlsDist[xx].Dist) );
//`find +-90 degrees from the closest one for when you walk into a corner so it doesn't shake
prev = (int)dbWrapValue(stlsDist[0].Ang-90)/angwidth;
nxt = (int)dbWrapValue(stlsDist[0].Ang+90)/angwidth;
newd = radius-stlsDist[0].Dist;
newa = dbWrapValue(stlsDist[0].Ang-180);
newd1 = radius-stlsort[prev].Dist;
newa1 = dbWrapValue(stlsort[prev].Ang-180);
newd2 = radius-stlsort[nxt].Dist;
newa2 = dbWrapValue(stlsort[nxt].Ang-180);
//`if you are less than radius from a wall, push you out to 20 from it
if ( stlsDist[0].Dist < radius && stlsDist[0].Dist > 0.0 )
{
Player.x = dbNewXValue(Player.x,newa,newd);
Player.z = dbNewZValue(Player.z,newa,newd);
UpdatePOS(Player,Player.x,Player.y,Player.z);
}
if ( stlsort[prev].Dist < radius && stlsort[prev].Dist > 0.0 )
{
//Player.x = dbNewXValue(Player.x,newa1,newd1);
//Player.z = dbNewZValue(Player.z,newa1,newd1);
//UpdatePOS(Player,Player.x,Player.y,Player.z);
// seams to mess the collsion up. working on this.
}
//`this is if you are coming into a corner, this pushes you sideways
if ( stlsort[nxt].Dist < radius && stlsort[nxt].Dist > 0.0 )
{
Player.x = dbNewXValue(Player.x,newa2,newd2);
Player.z = dbNewZValue(Player.z,newa2,newd2);
UpdatePOS(Player,Player.x,Player.y,Player.z);
}
}
void Collision::UpdatePOS( BasicObject& obj, float fx, float fy, float fz)
{
// update the postions.
dbPositionObject ( obj.O->Ref(), fx, fy, fz );
obj.x = fx;
obj.y = fy;
obj.z = fz;
}