/*************************************************/
#include "stdafx.h"
#include "Main.h"
/**************************************************/
Flt CamTppDist ,
CamTppCustom ,
CamTppCustomYaw ,
CamTppCustomPitch,
camPitchInterp;
Vec head;
Flt CameraTimerReset;
Bool CamIsRotated;
/********************************************************/
Camera cam;
/*************************************************/
void UpdateCamera()
{
Bool zoom=(Ms.wheel() && (!Gui.wheel() || Gui.wheel()==Gui.desktop()));
if(Players.elms())
{
Player &plr =Players[0];
UInt actor_groups=~(IndexToFlag(AG_CONTROLLER)|IndexToFlag(AG_PLAYER)|IndexToFlag(AG_ITEM)|IndexToFlag(AG_RAGDOLL)|IndexToFlag(AG_KINEMATIC_RAGDOLL));
Bool custom=(InvGui.visible()); // custom rotation
AdjustValTime(CamTppCustom,custom,0.001f);
if(Ms.b(1)) // rotate
{
//Players[0].matrix().angles().y+=Mid(dy,-max,max);
CamTppCustomYaw -=Ms.dir_d.x;
CamTppCustomPitch+=Ms.dir_d.y;
Ms.freeze();
}else{
CamTppCustomYaw =LerpAngle(Cam.yaw,-Players[0].matrix().angles().y,1);
CamTppCustomPitch= plr.angle.y;
}
Clamp(CamTppCustomPitch, -PI_4,PI_4);
Cam.yaw =CamTppCustomYaw;
Cam.pitch=CamTppCustomPitch;
{
if(zoom)CamTppDist*=ScaleFactor(Ms.wheel()*-0.2f);
Clamp(CamTppDist,0.0f,9.5f);
Cam.dist=Max(CamTppDist,CamTppCustom);
if(zoom)CamTppDist=Cam.dist;
}
// camera position
{
camPitchInterp = LerpAngle(camPitchInterp, Cam.pitch/2.0f,Time.d()*3);
head =plr.cskel.skeleton()->getPoint("Head").pos+Vec(1,0.5f,-1) // calculate behind and above head position relative to body bone
-plr.cskel.skeleton()->getBone ("Body").pos;
head*=plr.cskel.scale ();
Matrix3 look_matrix; look_matrix.setRotateXY(-plr.angle.y,-plr.angle.x); head*=look_matrix;
Vec pos= plr.stored_pos+head;
//pos=Lerp(pos, plr.stored_pos , 0.5f);
//pos=Lerp(pos, plr.stored_pos+Vec(0,0.2f,0), CamTppCustom);
Cam.at=pos;
}
// physics collisions
Ball ball(0.1f,Players[0].stored_pos); // start camera position at stored position
Vec move=Cam.at-ball.pos;
PhysHit phys_hit;
if(Physics.sweep(ball,move,&phys_hit,actor_groups)) // move toward desired camera center
{
// encountered an obstacle
Cam.setSpherical(ball.pos + move*phys_hit.frac, Cam.yaw, Cam.pitch, 0, ball.r); // we're forced to stop the camera at contact
}else
{
// no obstacle detected
Cam.setSpherical();
ball.pos=Cam.at; // set ball at camera focus
move=Cam.matrix.pos-ball.pos; // set movement from focus to "look from" point
if(Physics.sweep(ball,move,&phys_hit,actor_groups))
{
Cam.dist*=phys_hit.frac;
Cam.setSpherical();
}
}
if(Cam.pitch > 0.1f)
{
head =plr.cskel.skeleton()->getPoint("Head").pos+Vec(0,camPitchInterp,0) // calculate behind and above head position relative to body bone
-plr.cskel.skeleton()->getBone ("Body").pos;
}
else
{
head =plr.cskel.skeleton()->getPoint("Head").pos+Vec(0,-camPitchInterp,0) // calculate behind and above head position relative to body bone
-plr.cskel.skeleton()->getBone ("Body").pos;
}
Cam.setPosDir(Cam.matrix.pos+Vec(0,camPitchInterp,0), Cam.matrix.z, Cam.matrix.y);
Cam.updateVelocities();
Cam.set();
}else
{
CamHandle(0.1f,1000,(zoom?CAMH_ZOOM:0)|(Ms.b(0)?CAMH_MOVE_XZ:Ms.b(1)?CAMH_MOVE:CAMH_ROT));
}
}
/*****************************************/