-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugDraw.cpp
More file actions
115 lines (101 loc) · 4.28 KB
/
debugDraw.cpp
File metadata and controls
115 lines (101 loc) · 4.28 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
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
#include "debugDraw.h"
namespace debugDraw {
SFMLDebugDraw::SFMLDebugDraw(sf::RenderWindow* window) : m_window(window) {}
void SFMLDebugDraw::DrawPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::ConvexShape polygon(vertexCount);
sf::Vector2f center;
for (int i = 0; i < vertexCount; i++)
{
//polygon.setPoint(i, SFMLDraw::B2VecToSFVec(vertices[i]));
sf::Vector2f transformedVec = SFMLDebugDraw::B2VecToSFVec(vertices[i]);
polygon.setPoint(i, sf::Vector2f(std::floor(transformedVec.x), std::floor(transformedVec.y)));
}
polygon.setOutlineThickness(-1.f);
polygon.setFillColor(sf::Color::Transparent);
polygon.setOutlineColor(SFMLDebugDraw::GLColorToSFML(color));
m_window->draw(polygon);
}
void SFMLDebugDraw::DrawSolidPolygon(const b2Vec2* vertices, int32 vertexCount, const b2Color& color)
{
sf::ConvexShape polygon(vertexCount);
for (int i = 0; i < vertexCount; i++)
{
//polygon.setPoint(i, SFMLDraw::B2VecToSFVec(vertices[i]));
sf::Vector2f transformedVec = SFMLDebugDraw::B2VecToSFVec(vertices[i]);
polygon.setPoint(i, sf::Vector2f(std::floor(transformedVec.x), std::floor(transformedVec.y)));
}
polygon.setOutlineThickness(-1.f);
polygon.setFillColor(SFMLDebugDraw::GLColorToSFML(color, 60));
polygon.setOutlineColor(SFMLDebugDraw::GLColorToSFML(color));
m_window->draw(polygon);
}
void SFMLDebugDraw::DrawCircle(const b2Vec2& center, float radius, const b2Color& color)
{
const float k_segments = 16.0f;
sf::CircleShape circle(radius * sfdd::SCALE);
circle.setOrigin({ radius * sfdd::SCALE, radius * sfdd::SCALE });
circle.setPosition(SFMLDebugDraw::B2VecToSFVec(center));
circle.setFillColor(sf::Color::Transparent);
circle.setOutlineThickness(-1.f);
circle.setOutlineColor(SFMLDebugDraw::GLColorToSFML(color));
circle.setPointCount(k_segments);
m_window->draw(circle);
}
void SFMLDebugDraw::DrawSolidCircle(const b2Vec2& center, float radius, const b2Vec2& axis, const b2Color& color)
{
const float k_segments = 16.0f;
sf::CircleShape circle(radius * sfdd::SCALE);
circle.setOrigin({ radius * sfdd::SCALE, radius * sfdd::SCALE });
circle.setPosition(SFMLDebugDraw::B2VecToSFVec(center));
circle.setFillColor(SFMLDebugDraw::GLColorToSFML(color, 60));
circle.setOutlineThickness(1.f);
circle.setOutlineColor(SFMLDebugDraw::GLColorToSFML(color));
circle.setPointCount(k_segments);
b2Vec2 endPoint = center + radius * axis;
sf::Vertex line[2] =
{
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(center), SFMLDebugDraw::GLColorToSFML(color)),
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(endPoint), SFMLDebugDraw::GLColorToSFML(color)),
};
m_window->draw(circle);
m_window->draw(line, 2, sf::Lines);
}
void SFMLDebugDraw::DrawSegment(const b2Vec2& p1, const b2Vec2& p2, const b2Color& color)
{
sf::Vertex line[] =
{
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(p1), SFMLDebugDraw::GLColorToSFML(color)),
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(p2), SFMLDebugDraw::GLColorToSFML(color))
};
m_window->draw(line, 2, sf::Lines);
}
void SFMLDebugDraw::DrawTransform(const b2Transform& xf)
{
float lineLength = 0.4;
/*b2Vec2 xAxis(b2Vec2(xf.p.x + (lineLength * xf.q.c), xf.p.y + (lineLength * xf.q.s)));*/
b2Vec2 xAxis = xf.p + lineLength * xf.q.GetXAxis();
sf::Vertex redLine[] =
{
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(xf.p), sf::Color::Red),
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(xAxis), sf::Color::Red)
};
// You might notice that the ordinate(Y axis) points downward unlike the one in Box2D testbed
// That's because the ordinate in SFML coordinate system points downward while the OpenGL(testbed) points upward
/*b2Vec2 yAxis(b2Vec2(xf.p.x + (lineLength * -xf.q.s), xf.p.y + (lineLength * xf.q.c)));*/
b2Vec2 yAxis = xf.p + lineLength * xf.q.GetYAxis();
sf::Vertex greenLine[] =
{
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(xf.p), sf::Color::Green),
sf::Vertex(SFMLDebugDraw::B2VecToSFVec(yAxis), sf::Color::Green)
};
m_window->draw(redLine, 2, sf::Lines);
m_window->draw(greenLine, 2, sf::Lines);
}
void SFMLDebugDraw::DrawPoint(const b2Vec2& p, float size, const b2Color& color)
{
//m_points->Vertex(p, color, size);
sf::Vertex point(SFMLDebugDraw::B2VecToSFVec(p), SFMLDebugDraw::GLColorToSFML(color));
m_window->draw(&point, 1, sf::Points);
}
} // namespace debugDraw