Skip to content

Commit 1a9b5b2

Browse files
author
David Arutiunian
committed
adjust collisions
now playable refactor code
1 parent e6ded80 commit 1a9b5b2

File tree

11 files changed

+82
-35
lines changed

11 files changed

+82
-35
lines changed

Doodler.cpp

Lines changed: 42 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,38 @@ Doodler::Doodler()
2020

2121
void Doodler::updatePosition(const float deltaTime)
2222
{
23-
checkCollision();
24-
setVerticalPosition(MOVE_SPEED, deltaTime);
25-
m_timeAccumulator += deltaTime * TIME_ACCELERATOR;
26-
double nextY = m_position.y - m_initialSpeed * m_timeAccumulator + 0.5 * G * std::pow(m_timeAccumulator, 2);
27-
const sf::Vector2f nextPosition = {m_position.x, static_cast<float>(nextY)};
28-
m_shape.setPosition(nextPosition);
29-
setFallingState(static_cast<float>(nextY));
30-
setPosition(nextPosition);
23+
const float dtPhysics = deltaTime / MAX_PRECISION_COUNT;
24+
for (unsigned i = 0; i < MAX_PRECISION_COUNT; ++i)
25+
{
26+
setVerticalPosition(MOVE_SPEED, dtPhysics);
27+
m_timeAccumulator += dtPhysics * TIME_ACCELERATOR;
28+
const float nextY = getNextY();
29+
const sf::Vector2f nextPosition = {m_position.x, nextY};
30+
m_shape.setPosition(nextPosition);
31+
setFallingState(nextY);
32+
setPosition(nextPosition);
33+
checkCollision();
34+
}
3135
}
3236

3337
void Doodler::checkCollision()
3438
{
35-
const float currentBottomPosition = m_shape.getPosition().y + m_size.x / 2 + m_outlineThickness;
39+
const float currentBottomPosition = getBounds().height;
3640
const bool isAtZeroLevel = m_areFuzzyEqual(currentBottomPosition, m_floor);
37-
const bool isGameOverLevel = m_floor == WINDOW_HEIGHT;
41+
const bool isGameOverLevel = m_areFuzzyEqual(m_floor, WINDOW_HEIGHT);
3842
if (isAtZeroLevel && m_isFalling && !isGameOverLevel)
3943
{
40-
printf("%f\n", m_floor);
44+
setNextY();
4145
m_timeAccumulator = 0;
4246
}
4347
}
4448

45-
// TODO: fix side collision check
4649
void Doodler::setVerticalPosition(const float nextX, const float deltaTime)
4750
{
4851
const KeysMap &keysMap = m_p_keyboardState->getKeysMap();
49-
const bool isMaxRightPosition = m_position.x + m_size.x / 2 + m_outlineThickness < WINDOW_WIDTH;
50-
const bool isMaxLeftPosition = m_position.x - m_size.x / 2 - m_outlineThickness > 0;
52+
const sf::FloatRect bounds = getBounds();
53+
const bool isMaxRightPosition = bounds.width < WINDOW_WIDTH;
54+
const bool isMaxLeftPosition = bounds.left > 0;
5155
if (keysMap.at(sf::Keyboard::Right) && isMaxRightPosition)
5256
{
5357
m_position.x += nextX * deltaTime;
@@ -62,7 +66,7 @@ Types Doodler::getType() const
6266
return Types::Doodler;
6367
}
6468

65-
const sf::Vector2f &Doodler::getBounds() const
69+
const sf::Vector2f &Doodler::getSize() const
6670
{
6771
return m_size;
6872
}
@@ -74,15 +78,14 @@ void Doodler::setFloor(const float nextFloor)
7478
return;
7579
}
7680
m_floor = nextFloor;
77-
m_position.y = m_floor - m_size.x / 2 - m_outlineThickness;
7881
}
7982

8083
void Doodler::setFallingState(float nextY)
8184
{
8285
m_isFalling = getPosition().y - nextY <= 0;
8386
}
8487

85-
void Doodler::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
88+
void Doodler::addKeyboardState(const std::shared_ptr<KeyboardState> &p_keyboardState)
8689
{
8790
m_p_keyboardState = p_keyboardState;
8891
}
@@ -91,3 +94,25 @@ bool Doodler::getFallingState() const
9194
{
9295
return m_isFalling;
9396
}
97+
98+
void Doodler::setNextY()
99+
{
100+
m_position.y = m_floor - m_size.y / 2 - m_outlineThickness;
101+
}
102+
103+
float Doodler::getNextY() const
104+
{
105+
const float lhs = m_initialSpeed * m_timeAccumulator;
106+
const auto rhs = static_cast<const float>(0.5 * G * std::pow(m_timeAccumulator, 2));
107+
return m_position.y - lhs + rhs;
108+
}
109+
110+
sf::FloatRect Doodler::getBounds() const
111+
{
112+
const sf::Vector2f position = m_shape.getPosition();
113+
const float left = position.x - m_size.x / 2 - m_outlineThickness;
114+
const float right = position.x + m_size.x / 2 + m_outlineThickness;
115+
const float top = position.y - m_size.y / 2 - m_outlineThickness;
116+
const float bottom = position.y + m_size.y / 2 + m_outlineThickness;
117+
return sf::FloatRect(left, top, right, bottom);
118+
}

Doodler.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ class Doodler : public IEntity
1919

2020
Types getType() const override;
2121

22-
const sf::Vector2f &getBounds() const override;
22+
const sf::Vector2f &getSize() const override;
2323

24-
void addKeyboardState(std::shared_ptr<KeyboardState> p_keyboardState);
24+
void addKeyboardState(const std::shared_ptr<KeyboardState> &p_keyboardState);
2525

2626
bool getFallingState() const override;
2727

@@ -65,6 +65,12 @@ class Doodler : public IEntity
6565
void checkCollision() override;
6666

6767
void setVerticalPosition(float nextX, float deltaTime);
68+
69+
void setNextY();
70+
71+
sf::FloatRect getBounds() const;
72+
73+
float getNextY() const;
6874
};
6975

7076
#endif //DOODLE_JUMP_DOODLER_H

Engine.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ void Engine::processCollision(const std::shared_ptr<IEntity> &p_entity)
2121
if (doesIntersect(p_entity) && m_p_doodler->getFallingState() && !m_shouldSetFloor)
2222
{
2323
m_shouldSetFloor = true;
24-
m_floor = p_entity->getPosition().y - p_entity->getBounds().y;
24+
m_floor = p_entity->getPosition().y - p_entity->getSize().y;
2525
}
2626
}
2727

28-
// TODO: adjust intersection check
2928
bool Engine::doesIntersect(const std::shared_ptr<IEntity> &p_entity) const
3029
{
31-
sf::Rect<float> rhs(p_entity->getPosition(), p_entity->getBounds());
32-
sf::Rect<float> lhs(m_p_doodler->getPosition(), m_p_doodler->getBounds());
30+
sf::Rect<float> rhs(p_entity->getPosition(), p_entity->getSize());
31+
sf::Rect<float> lhs(m_p_doodler->getPosition(), m_p_doodler->getSize());
3332
return lhs.intersects(rhs);
3433
}

EventLoop.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,12 @@ const sf::RenderWindow &EventLoop::getWindow() const
6363
return m_window;
6464
}
6565

66-
void EventLoop::addKeyboardState(const std::shared_ptr<KeyboardState> p_keyboardState)
66+
void EventLoop::addKeyboardState(const std::shared_ptr<KeyboardState> &p_keyboardState)
6767
{
6868
m_p_keyboardState = p_keyboardState;
6969
}
7070

71-
void EventLoop::addView(const std::shared_ptr<View> p_view)
71+
void EventLoop::addView(const std::shared_ptr<View> &p_view)
7272
{
7373
m_p_view = p_view;
7474
}

EventLoop.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ class EventLoop
2121

2222
void redrawFrame(const Entities &entities);
2323

24-
void addKeyboardState(std::shared_ptr<KeyboardState> p_keyboardState);
24+
void addKeyboardState(const std::shared_ptr<KeyboardState> &p_keyboardState);
2525

26-
void addView(std::shared_ptr<View> p_view);
26+
void addView(const std::shared_ptr<View> &p_view);
2727

2828
const sf::RenderWindow &getWindow() const;
2929

IPhysicsObject.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
class IPhysicsObject
88
{
99
public:
10-
virtual const sf::Vector2f &getBounds() const = 0;
10+
virtual const sf::Vector2f &getSize() const = 0;
1111

1212
virtual Types getType() const = 0;
13+
14+
virtual sf::FloatRect getBounds() const = 0;
1315
};
1416

1517
#endif //DOODLE_JUMP_IPHYSICSOBJECT_H

Platform.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@ Platform::Platform()
1111
m_position.x = rand() % WINDOW_WIDTH;
1212
m_position.y = rand() % WINDOW_HEIGHT;
1313

14-
const bool isOverRightSide = m_position.x + m_size.x > WINDOW_WIDTH;
15-
const bool isOverLeftSide = m_position.x - m_size.x < 0;
14+
const sf::FloatRect bounds = getBounds();
15+
const bool isOverRightSide = bounds.width > WINDOW_WIDTH;
16+
const bool isOverLeftSide = bounds.left < 0;
17+
1618
if (isOverRightSide)
1719
{
1820
m_position.x -= m_size.x;
@@ -44,7 +46,17 @@ Types Platform::getType() const
4446
return Types::Platform;
4547
}
4648

47-
const sf::Vector2f &Platform::getBounds() const
49+
const sf::Vector2f &Platform::getSize() const
4850
{
4951
return m_size;
5052
}
53+
54+
sf::FloatRect Platform::getBounds() const
55+
{
56+
const sf::Vector2f position = m_shape.getPosition();
57+
const float left = position.x - m_size.x / 2 - m_outlineThickness;
58+
const float right = position.x + m_size.x / 2 + m_outlineThickness;
59+
const float top = position.y - m_size.y / 2 - m_outlineThickness;
60+
const float bottom = position.y + m_size.y / 2 + m_outlineThickness;
61+
return sf::FloatRect(left, top, right, bottom);
62+
}

Platform.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class Platform : public IEntity
1818

1919
Types getType() const override;
2020

21-
const sf::Vector2f &getBounds() const override;
21+
const sf::Vector2f &getSize() const override;
2222

2323
private:
2424
sf::RectangleShape m_shape;
@@ -28,6 +28,8 @@ class Platform : public IEntity
2828
const unsigned m_outlineThickness = 2;
2929

3030
void draw(sf::RenderTarget &target, sf::RenderStates states) const override;
31+
32+
sf::FloatRect getBounds() const;
3133
};
3234

3335
#endif //DOODLE_JUMP_PLATFORM_H

View.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ View::View()
77
m_view.setViewport(sf::FloatRect(0, 0, 1, 1));
88
}
99

10-
void View::followTo(const std::shared_ptr<Doodler> p_doodler)
10+
void View::followTo(const std::shared_ptr<Doodler> &p_doodler)
1111
{
1212
m_view.setCenter(WINDOW_WIDTH / 2, p_doodler->getPosition().y);
1313
}

View.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class View
1111

1212
const sf::View &getView() const;
1313

14-
void followTo(std::shared_ptr<Doodler> p_doodler);
14+
void followTo(const std::shared_ptr<Doodler> &p_doodler);
1515

1616
private:
1717
sf::View m_view;

consts.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@ static const sf::Vector2u ICON_SIZE = {32, 32};
1717

1818
/// Game params
1919
static const float G = 9.8f;
20-
static const float TIME_ACCELERATOR = 15.f;
20+
static const float TIME_ACCELERATOR = 7.5f;
2121
static const float MOVE_SPEED = 500.f;
2222
static const size_t PLATFORM_COUNT = 30;
23+
static const unsigned MAX_PRECISION_COUNT = 128;
2324

2425
template<typename Signature>
2526
using Lambda = std::function<Signature>;

0 commit comments

Comments
 (0)