-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera2D.h
67 lines (52 loc) · 1.54 KB
/
Camera2D.h
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
//
// Created by flo on 10/28/2021.
//
#ifndef NOMPROJET_CAMERA2D_H
#define NOMPROJET_CAMERA2D_H
class Camera2D{
private:
float x=0.0F,y=0.0F;
float targetX=0.0F,targetY=0.0F;
const float defaultZoom = 1.0F,maxZoom=0.90F;
float zoom = defaultZoom;
// from 0 to 1 plz
float cameraSmoothnessX = 0.15F;
float cameraSmoothnessY = 0.15F;
float zoomSmoothNess = 0.10F;
public:
int W,H;
Camera2D(int W,int H){this->W = W;this->H = H;}
Camera2D(float x,float y){setX(0.0F);setY(0.0F);}
float getX(){return x;}
float getY(){return y;}
void setX(float newX){x=newX;}
void setY(float newY){y=newY;}
void move(float offsetX,float offsetY){
setX(x+offsetX);
setY(y+offsetY);
}
void center(int x,int y,int pixelPerUnit){
setX( (x * pixelPerUnit)- W/2);
setY( (y * pixelPerUnit) - H/2);
}
void target(float x,float y,int pixelPerUnit){
targetX = ((x*pixelPerUnit) - W/2);
targetY = ((y*pixelPerUnit) - H/2);
}
/**
*
* Smooth Lerp Follow Target camera.
*/
void slerpFollow(){
// update position by 15% of the distance between position and target position
move((targetX- x)*cameraSmoothnessX,(targetY - y)*cameraSmoothnessY);
}
void zoomActived(bool active,float deltaTime){
if(active) zoom += (maxZoom-zoom)*zoomSmoothNess*deltaTime;
else zoom += (defaultZoom-zoom)*zoomSmoothNess*deltaTime;
}
float getZoom(){
return zoom;
}
};
#endif //NOMPROJET_CAMERA2D_H