-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcameraBox2DtoSFML.cpp
More file actions
60 lines (51 loc) · 1.4 KB
/
cameraBox2DtoSFML.cpp
File metadata and controls
60 lines (51 loc) · 1.4 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include "cameraBox2DtoSFML.h"
Box2DCamera::Box2DCamera(const b2Vec2& size, const b2Vec2& center, const sf::Vector2f& video_mode_size)
: pos_to_world_(center), size_(size)
, video_mode_center_(video_mode_size / 2.f)
{
SetSize(size);
SetCenter(center);
}
void Box2DCamera::SetSize(const b2Vec2 size)
{
size_ = size;
View::setSize({ size_.x * scale_.x, size.y * scale_.y });
}
const b2Vec2& Box2DCamera::GetSize() const
{
return size_;
}
void Box2DCamera::SetCenter(const b2Vec2 pos_to_world)
{
pos_to_world_ = pos_to_world;
View::setCenter({ pos_to_world.x * scale_.x, pos_to_world.y * scale_.y });
}
const b2Vec2& Box2DCamera::GetCenter() const
{
return pos_to_world_;
}
void Box2DCamera::Move(const b2Vec2& move_to)
{
View::move({ move_to.x * scale_.x, move_to.y * scale_.y });
pos_to_world_ += move_to;
}
sf::Vector2f Box2DCamera::GetPositionInScreen(const b2Vec2& pos_to_world) const
{
b2Vec2 result = pos_to_world - pos_to_world_;
return {
video_mode_center_.x + result.x * scale_.x,
video_mode_center_.y + result.y * scale_.y
};
}
b2Vec2 Box2DCamera::GetPositionInWorld(const sf::Vector2f& pos_to_screen) const
{
sf::Vector2f result = pos_to_screen - video_mode_center_;
return {
pos_to_world_.x + result.x / scale_.x,
pos_to_world_.y + result.y / scale_.y
};
}
b2Vec2 Box2DCamera::GetScaleFromScreenPoint(const b2Vec2& value) const
{
return { value.x / scale_.x, value.y / scale_.y };
}