-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCCameraFollower.cpp
More file actions
42 lines (36 loc) · 1.1 KB
/
CCameraFollower.cpp
File metadata and controls
42 lines (36 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "CCameraFollower.h"
#include "CObjectShapes.h"
CCameraFollower::CCameraFollower(CObject* followed,
float distance, float stiffness, float speed,
const vec3& displacement, const vec3& viewDisplacement,
float fov, float near_, float far_,
const vec3& pos, const vec3& rot) :
CCamera(vec3(), 1.f, fov, near_, far_, 0, pos, rot),
FollowedObject(followed), Distance(distance),
Stiffness(stiffness), Speed(speed)
{
ViewObject = new CObject(followed, displacement);
DisplacementObject = new CObject(ViewObject, viewDisplacement);
}
CCameraFollower::~CCameraFollower()
{
delete ViewObject;
delete DisplacementObject;
}
void CCameraFollower::setFollowedObject(CObject* followed)
{
FollowedObject = followed;
ViewObject->setParent(FollowedObject);
DisplacementObject->setParent(ViewObject);
}
void CCameraFollower::animate(float dt)
{
if (!FollowedObject)
return;
vec3 D(ViewObject->getAbsolutePosition() - getPosition());
float Dlen = D.len();
Dlen -= Distance;
Dlen *= Speed * exp(-Stiffness * dt);
setPosition(getPosition() + D.norm() * Dlen);
setFocus(DisplacementObject->getAbsolutePosition());
}