Doxygen Samurai Engine 0.0.1
Doxygen Samurai Engine Documentation
Loading...
Searching...
No Matches
mario.cpp
Go to the documentation of this file.
1#include "mario.h"
2
3
4
5
7{
8 return position;
9}
10
12{
13 return position + size / 2.f;
14}
15
17{
18 return position + glm::vec2(0, size.y);
19}
20
22{
23 return position + glm::vec2(size.x / 2.f, size.y);
24}
25
26
27
28void Player::move(glm::vec2 dir)
29{
30 if (length(dir) == 0.f) { return; }
31
32 movingThisFrame = true;
33 position.position += dir;
34}
35
36
37float maxMoveVelocity = 25;
38float startVelocity = 10;
39
40void Player::moveVelocityX(float dir)
41{
42 if (dir == 0) { return; }
43
44 movingThisFrame = true;
45
46 if (dir > 0 && velocity.x < 0)
47 {
48 velocity.x = 0;
49 }
50 else if (dir < 0 && velocity.x > 0)
51 {
52 velocity.x = 0;
53 }
54
55 if (dir > 0)
56 {
58 {
59 if (velocity.x == 0)
60 {
62 }
63
64 velocity.x += dir;
65
67 {
69 }
70 }
71 }
72 else if (dir < 0)
73 {
75 {
76 if (velocity.x == 0)
77 {
79 }
80
81 velocity.x += dir;
82
84 {
86 }
87 }
88 }
89
90
91
92}
93
94void Player::jump(float power)
95{
96 velocity.y = -power;
97}
98
99const float terminalVelocity = 70;
100
101void Player::applyGravity(float gravity)
102{
103
105 {
106 velocity.y += gravity;
107
109 {
111 }
112 }
113
114}
115
116
118{
119 if (lastPos.x - position.position.x < 0)
120 {
121 movingRight = true;
122 }
123 else if (lastPos.x - position.position.x > 0)
124 {
125 movingRight = false;
126 }
127
128 if (movingThisFrame)
129 {
130 //playerAnimation.state = PlayerAnimation::STATES::running;
131 }
132 else
133 {
134 //playerAnimation.state = {};
135 }
136
137
139
140 movingThisFrame = false;
141
142}
143
144float groundDrag = 120.f;
145float airDrag = 60.f;
146
147void Player::updatePhisics(float deltaTime)
148{
149
150 glm::vec2 drag = {groundDrag, groundDrag};
151
152 if (!grounded) { drag.x = airDrag; }
153
154 if (!movingThisFrame)
155 {
156 if (velocity.x > 0)
157 {
158 velocity -= drag * deltaTime;
159
160 if (velocity.x < 0)
161 {
162 velocity.x = 0;
163 }
164 }
165 else if (velocity.x < 0)
166 {
167 velocity += drag * deltaTime;
168
169 if (velocity.x > 0)
170 {
171 velocity.x = 0;
172 }
173 }
174 }
175
176 position.position += velocity * deltaTime;
177
178}
179
180void Player::checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
181{
182 glm::vec2 delta = pos - lastPos;
183 const float BLOCK_SIZE = 1.f;
184
185 glm::vec2 &dimensions = position.size;
186
187 glm::ivec2 mapSize = {100,100};
188
189 if (
190 (pos.y < -dimensions.y)
191 || (pos.x < -dimensions.x)
192 || (pos.y > mapSize.x * BLOCK_SIZE)
193 || (pos.x > mapSize.y * BLOCK_SIZE)
194 )
195 {
196 return;
197 }
198
199 glm::vec2 newPos = performCollision(map, {pos.x, lastPos.y}, {dimensions.x, dimensions.y}, {delta.x, 0},
200 upTouch, downTouch, leftTouch, rightTouch);
201 pos = performCollision(map, {newPos.x, pos.y}, {dimensions.x, dimensions.y}, {0, delta.y},
202 upTouch, downTouch, leftTouch, rightTouch);
203
204}
205
206glm::vec2 Player::performCollision(Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
207{
208 glm::ivec2 mapSize = {100,100};
209
210 int minX = 0;
211 int minY = 0;
212 int maxX = mapSize.x;
213 int maxY = mapSize.y;
214
215 auto &dimensions = position.size;
216
217 const float BLOCK_SIZE = 1.f;
218
219 minX = (pos.x - abs(delta.x) - BLOCK_SIZE) / BLOCK_SIZE;
220 maxX = ceil((pos.x + abs(delta.x) + BLOCK_SIZE + size.x) / BLOCK_SIZE);
221
222 minY = (pos.y - abs(delta.y) - BLOCK_SIZE) / BLOCK_SIZE;
223 maxY = ceil((pos.y + abs(delta.y) + BLOCK_SIZE + size.y) / BLOCK_SIZE);
224
225 minX = std::max(0, minX);
226 minY = std::max(0, minY);
227 maxX = std::min(mapSize.x, maxX);
228 maxY = std::min(mapSize.y, maxY);
229
230 auto getMapBlockUnsafe = [&](int x, int y) -> Block *
231 {
232 return &map[x + y * mapSize.x];
233 };
234
235 for (int y = minY; y < maxY; y++)
236 for (int x = minX; x < maxX; x++)
237 {
238 if (getMapBlockUnsafe(x, y)->isCollidable())
239 {
240 if (aabb({pos,dimensions}, {x * BLOCK_SIZE, y * BLOCK_SIZE, BLOCK_SIZE, BLOCK_SIZE}, 0.0001f))
241 {
242 if (delta.x != 0)
243 {
244 if (delta.x < 0) // moving left
245 {
246 leftTouch = 1;
247 pos.x = x * BLOCK_SIZE + BLOCK_SIZE;
248 goto end;
249 }
250 else
251 {
252 rightTouch = 1;
253 pos.x = x * BLOCK_SIZE - dimensions.x;
254 goto end;
255 }
256 }
257 else if (delta.y != 0)
258 {
259 if (delta.y < 0) //moving up
260 {
261 upTouch = 1;
262 pos.y = y * BLOCK_SIZE + BLOCK_SIZE;
263 goto end;
264 }
265 else
266 {
267 downTouch = 1;
268 pos.y = y * BLOCK_SIZE - dimensions.y;
269 goto end;
270 }
271 }
272
273 }
274 }
275
276 }
277
278end:
279 return pos;
280
281}
282
284{
285 glm::ivec2 mapSize = {100,100};
286
287 bool upTouch = 0;
288 bool downTouch = 0;
289 bool leftTouch = 0;
290 bool rightTouch = 0;
291
292 glm::vec2 &pos = position.position;
293
294 float distance = glm::length(lastPos - pos);
295 const float BLOCK_SIZE = 1.f;
296
297 if (distance < BLOCK_SIZE)
298 {
300 lastPos,
301 map,
302 upTouch,
303 downTouch,
304 leftTouch,
305 rightTouch
306 );
307 }
308 else
309 {
310 glm::vec2 newPos = lastPos;
311 glm::vec2 delta = pos - lastPos;
312 delta = glm::normalize(delta);
313 delta *= 0.9 * BLOCK_SIZE;
314
315 do
316 {
317 newPos += delta;
318 glm::vec2 posTest = newPos;
319 checkCollisionBrute(newPos,
320 lastPos,
321 map,
322 upTouch,
323 downTouch,
324 leftTouch,
325 rightTouch);
326
327 if (newPos != posTest)
328 {
329 pos = newPos;
330 goto end;
331 }
332
333 } while (glm::length((newPos + delta) - pos) > 1.0f * BLOCK_SIZE);
334
336 lastPos,
337 map,
338 upTouch,
339 downTouch,
340 leftTouch,
341 rightTouch);
342 }
343
344end:
345
346 //clamp the box if needed
347 if (pos.x < 0) { pos.x = 0; leftTouch = true; }
348 if (pos.x + position.size.x > (mapSize.x) * BLOCK_SIZE)
349 {
350 pos.x = ((mapSize.x) * BLOCK_SIZE) - position.size.x; rightTouch = true;
351 }
352
353
354 if (leftTouch && velocity.x < 0) { velocity.x = 0; }
355 if (rightTouch && velocity.x > 0) { velocity.x = 0; }
356
357 if (upTouch && velocity.y < 0) { velocity.y = 0; }
358 if (downTouch && velocity.y > 0) { velocity.y = 0; }
359
360 if (downTouch) { grounded = true; }
361
362}
float startVelocity
Definition mario.cpp:38
float airDrag
Definition mario.cpp:145
float groundDrag
Definition mario.cpp:144
float maxMoveVelocity
Definition mario.cpp:37
const float terminalVelocity
Definition mario.cpp:99
bool aabb(glm::vec4 b1, glm::vec4 b2, float delta)
bool movingRight
Definition mario.h:36
void jump(float power)
Definition mario.cpp:94
void updatePhisics(float deltaTime)
Definition mario.cpp:147
void move(glm::vec2 dir)
Definition mario.cpp:28
void resolveConstrains(Block *map)
Definition mario.cpp:283
Transform position
Definition mario.h:30
glm::vec2 performCollision(Block *map, glm::vec2 pos, glm::vec2 size, glm::vec2 delta, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
Definition mario.cpp:206
bool movingThisFrame
Definition mario.h:47
bool grounded
Definition mario.h:37
glm::vec2 velocity
Definition mario.h:34
void applyGravity(float gravity)
Definition mario.cpp:101
void moveVelocityX(float dir)
Definition mario.cpp:40
void checkCollisionBrute(glm::vec2 &pos, glm::vec2 lastPos, Block *map, bool &upTouch, bool &downTouch, bool &leftTouch, bool &rightTouch)
Definition mario.cpp:180
void updateMove()
Definition mario.cpp:117
glm::vec2 lastPos
Definition mario.h:32
glm::vec2 getBottomLeftCorner()
Definition mario.cpp:16
glm::vec2 size
Definition mario.h:17
glm::vec2 getCenter()
Definition mario.cpp:11
glm::vec2 getTopLeftCorner()
Definition mario.cpp:6
glm::vec2 getBottomCenter()
Definition mario.cpp:21
glm::vec2 position
Definition mario.h:16