-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollision.h
51 lines (42 loc) · 1.77 KB
/
Collision.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
#pragma once
#include "Paddle.h"
#include "Ball.h"
#include "Block.h"
template<class T1, class T2> bool rectCollision(const T1& a, const T2& b)
{
return !(b.left() >= a.right() || b.right() <= a.left() || b.top() >= a.bottom() || b.bottom() <= a.top());
}
void checkPaddleBallCollision(Paddle& paddle, Ball& ball)
{
if (!rectCollision(paddle, ball)) return;
if (ball.bottom() >= paddle.top())
{
auto ballVelocity = ball.getVelocity();
auto paddleVelocity = paddle.getVelocity();
float ballMaxVelocity = ball.getMaxVelocity();
float sumX = ballVelocity.x + paddleVelocity.x;
float newXVelocity = (abs(sumX) <= ballMaxVelocity) ? sumX : ballMaxVelocity;
newXVelocity = (abs(newXVelocity) >= 0.25f) ? newXVelocity : ((newXVelocity > 0) ? newXVelocity + 0.15f : newXVelocity - 0.15f);
ball.setVelocity({ newXVelocity, ballVelocity.y * -1 });
}
}
void checkBlockBallCollision(Block& block, Ball& ball)
{
if (!rectCollision(block, ball))
return;
block.setRequiredHits(block.getRequiredHits() - 1);
if (block.getRequiredHits() <= 0)
block.destroy();
float overlapLeft{ ball.right() - block.left() };
float overlapRight{ block.right() - ball.left() };
float overlapTop{ ball.bottom() - block.top() };
float overlapBottom{ block.bottom() - ball.top() };
bool ballFromLeft(abs(overlapLeft) < abs(overlapRight));
bool ballFromTop(abs(overlapTop) < abs(overlapBottom));
float minOverlapX{ ballFromLeft ? overlapLeft : overlapRight };
float minOverlapY{ ballFromTop ? overlapTop : overlapBottom };
if (abs(minOverlapX) < abs(minOverlapY))
ball.setVelocity({ ballFromLeft ? -abs(ball.getVelocity().x) : abs(ball.getVelocity().x), ball.getVelocity().y });
else
ball.setVelocity({ ball.getVelocity().x, ballFromTop ? -abs(ball.getVelocity().y) : abs(ball.getVelocity().y) });
}