well since i made this post i have been working on my camera class.
i trying to get the camera collision to work so the camera never goes though a wall.
i have it kind of working right now but when the camera is touching a wall and i move the player it kind of jidders.
can anyone help me out with this. here is the following code i have right now.
#include "stdafx.h"
#include "Camera.h"
C_Camera::C_Camera()
{
// initilizes the camera.
dbSetCameraFOV ( (360/4)-48 );
m_campos.x = 0.0f;
m_campos.y = 0.0f;
m_campos.z = 0.0f;
// will be used for cam collision.
radius = 40; // may change this depending on how it works
checks = 8; // Cam collision needs to be smoother then object collision.
angwidth = 360/checks;
// used colission stuff. not used yet.
DistMin = 50;
DistMax = 100;
HeightMin = 25;
HeightMax = 250;
rad = 0.01745; // speed of rotation
move = 0; // init to zero
Dist = -100.f; // Distance from Object.
Height = 50; //height of camera.
}
C_Camera::~C_Camera(){}
void C_Camera::update(LoadPlayer& thisPlayer, bool collision, BasicObject& Obj)
{
/***********************************************************************************
* This will be my camera control, it will have collision and follow player. *
* you will be able to get the min distance the camera can be to the player and the *
* Max distance. this will go for height aswell. *
************************************************************************************/
float CamRotX = Dist * sin(rad*move);
float CamRotZ = Dist * cos(rad*move);
// put in place if there is collision going on don't reposition based on player.
// keep going though colillision may have an issue still.
if (!iscollide)
{
m_campos.x = CamRotX + thisPlayer.x;
m_campos.y = thisPlayer.y+Height;
m_campos.z = CamRotZ + thisPlayer.z;
}
#ifdef _DEBUG
dbText(20,380,"Camera Ang");
dbText(20,400,str( dbCameraAngleX() ));
dbText(20,420,str( dbCameraAngleY() ));
dbText(20,440,str( dbCameraAngleZ() ));
dbText(200,380,"Camera Pos");
dbText(200,400,str( m_campos.x ));
dbText(200,420,str( m_campos.y ));
dbText(200,440,str( m_campos.z ));
dbText(450,380,"Player Pos");
dbText(450,400,str( thisPlayer.x ));
dbText(450,420,str( thisPlayer.y ));
dbText(450,440,str( thisPlayer.z ));
dbText(20,200,"Cam Collision:");
dbText(20,220,str(iscollide));
#endif
// dont use collision yet get camera to function first.
if (collision)
{
// check for colision for camera.
CamCollision(Obj);
}
dbPositionCamera( m_campos.x, m_campos.y, m_campos.z ); // position cam to new position.
dbPointCamera ( thisPlayer.x, thisPlayer.y, thisPlayer.z ); // Point Camera at Object
}
void C_Camera::CamCollision(BasicObject& Obj)
{
/*************************************************
* This Function is for Camera Collision *
* Camera has it's own collision system so it can *
* be optimized just for camera movement. *
*************************************************/
iscollide = false; // reset collide
for ( int xx=0; xx < checks-1; xx++ )
{
chx = dbNewXValue( m_campos.x,xx*angwidth*1.0f,100 );
chz = dbNewZValue( m_campos.z,xx*angwidth*1.0f,100 );
ang[xx][1] = dbIntersectObject( Obj.O->Ref(),m_campos.x,2,m_campos.z,chx,m_campos.y,chz );
//float dbIntersectObject ( int iObject, float fX, float fY, float fZ, float fToX, float fToY, float fToZ )
if ( ang[xx][1] == 0 )
{
ang[xx][1] = 99999;
}
ang[xx][2] = (float)xx*angwidth;
sort[xx][1] = ang[xx][1];
sort[xx][2] = ang[xx][2];
}
// rem sort the array to find the closest object and it's degrees
for ( int xx=0; xx < checks-2; xx++ )
{
for ( int yy=xx+1; yy < checks-1; yy++ )
{
if ( ang[xx][1] > ang[yy][1] )
{
temp = ang[xx][1];
temptwo = ang[xx][2];
ang[xx][1] = ang[yy][1];
ang[xx][2] = ang[yy][2];
ang[yy][1] = temp;
ang[yy][2] = temptwo;
}
} // end Y
} // End X
//`find +-90 degrees from the closest one for when you walk into a corner so it doesn't shake
prev = (int)dbWrapValue(ang[0][2]-90)/angwidth;
nxt = (int)dbWrapValue(ang[0][2]+90)/angwidth;
newd = radius-ang[0][1];
newa = dbWrapValue(ang[0][2]-180);
newd1 = radius-sort[prev][1];
newa1 = dbWrapValue(sort[prev][2]-180);
newd2 = radius-sort[nxt][1];
newa2 = dbWrapValue(sort[nxt][2]-180);
//`if you are less than radius from a wall, push you out to 20 from it
if ( ang[0][1] < radius && ang[0][1] > 0.0 )
{
m_campos.x = dbNewXValue(m_campos.x,newa,newd);
m_campos.z = dbNewZValue(m_campos.z,newa,newd);
iscollide = true;
}
if ( sort[prev][1] < radius && sort[prev][1] > 0.0 )
{
m_campos.x = dbNewXValue(m_campos.x,newa1,newd1);
m_campos.z = dbNewZValue(m_campos.z,newa1,newd1);
iscollide = true;
}
//`this is if you are coming into a corner, this pushes you sideways
if ( sort[nxt][1] < radius && sort[nxt][1] > 0.0 )
{
m_campos.x = dbNewXValue(m_campos.x,newa2,newd2);
m_campos.z = dbNewZValue(m_campos.z,newa2,newd2);
iscollide = true;
}
}