-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scene2.cpp
131 lines (104 loc) · 3.19 KB
/
Scene2.cpp
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "Scene2.h"
void Scene2::onLoad() {
ground = new Polygon({ Vec2(0, Draw::H - 80), Vec2(Draw::W, Draw::H - 80) , Vec2(Draw::W, Draw::H) , Vec2(0, Draw::H) });
ground2 = new Rectangle(Vec2(100, Draw::H - 200), 300, 30);//new Polygon({ Vec2(0, Draw::H - 280), Vec2(Draw::W, Draw::H - 280) , Vec2(Draw::W, Draw::H-220) , Vec2(0, Draw::H-220) });
ground3 = new Polygon({ Vec2(0, 0), Vec2(0, Draw::H) , Vec2(-5, Draw::H - 220) , Vec2(-5, 0) });
rect = new Rectangle(Vec2(100, Draw::H - 150), 20, 30); //new Polygon({Vec2(-15, 40), Vec2(15, 40) , Vec2(15, -30) , Vec2(-20, -30) });
tri = new Polygon({ Vec2(-20, 10), Vec2(0, -30) , Vec2(20, 10) });
tri->setPosition(Vec2(300, Draw::H - 90));
addChild("ground", ground);
addChild("ground2", ground2);
addChild("ground3", ground3);
addChild("rect", rect);
addChild("tri", tri);
}
void Scene2::onUpdate(double dt) {
Scene::onUpdate(dt);
this->dt = dt;
if (game->keys['A']) {
XTargetVel.x = -speed;
}
else if (game->keys['D']) {
XTargetVel.x = speed;
}
else {
XTargetVel.x = 0;
}
if (game->keys[VK_SPACE]) {
if (!jumping) {
jumping = true;
velocity.y = -jumpSpeed;
}
}
YTargetVel = YTargetVel.moveToword(Vec2(0, gravity), 0, gravity * dt);
Vec2 kvel = XTargetVel + YTargetVel; //目标合速度
bool move = (fabs(XTargetVel.x) > eps || fabs(XTargetVel.y) > eps);
bool jp = (fabs(YTargetVel.y) > eps);
if (move || jp) {
velocity = velocity.moveToword(kvel, (move ? acceleration : friction) * dt, YFirction * dt);
}
else {
velocity = velocity.moveToword(Vec2(0, 0), friction * dt, YFirction * dt);
}
Node* p = rect;
Vec2 re = moveAndCollide(p, velocity * dt);
if (fabs(re.x) > eps || fabs(re.y) > eps) {
if (velocity.y >= 0 && !game->keys[VK_SPACE]) {
jumping = false;
}
YTargetVel = Vec2(0, jumpSpeed);
if (fabs(re.y) > eps)
velocity.y = 0;
}
}
void Scene2::onExit() {
}
/*
* 该函数将返回一个受力方向
*/
Vec2 Scene2::moveAndCollide(Node* p, Vec2 velocity, bool slide) {
Vec2 res(0, 0);
Vec2 first(0, 0), all(0, 0);
Vec2 tmp = p->getPosition();
p->setPosition(tmp + velocity);
map<string, Node* > ch = getAllChilden();
for (auto n : ch) {
if (n.first != p->name && p->getPolygonCollider()->checkCollision(*n.second->getPolygonCollider(), first)) {
all = all + first;
}
}
res = all;
double err = 1; //使用遍历搜出刚好不发生碰撞的位置。。有点莽,理论上可以用二分改进
bool flag = false;
int cnt = 0;
while (cnt < 20 && (all.x != 0 || all.y != 0))
{
p->setPosition(tmp);
if (all.x != 0 || all.y != 0) {
double angle = all.angleWith(velocity);
velocity = Vec2(fabs(all.x) < eps ? 0 : velocity.x, fabs(all.x) < eps ? velocity.y : 0);
if (slide) {
if (angle < 90) {
velocity = velocity.project(all.normalize());
p->setPosition(tmp + velocity * err);
}
else if (angle > 90) {
velocity = velocity.project(-all.normalize());
p->setPosition(tmp + velocity * err);
}
}
else {
p->setPosition(tmp + velocity * err);
}
err -= 0.1;
cnt++;
}
all = Vec2(0, 0);
for (auto n : ch) {
if (n.first != p->name && p->getPolygonCollider()->checkCollision(*n.second->getPolygonCollider(), first)) {
all = all + first;
}
}
}
return res.perpendicular();
}