Repository navigation
Expand file tree
/
Copy pathrender_console.cpp
More file actions
187 lines (146 loc) · 4.6 KB
/
Copy pathrender_console.cpp
File metadata and controls
187 lines (146 loc) · 4.6 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
//
// Created by oguzh on 29.12.2025.
//
#include <iostream>
#include <algorithm>
#include "render_console.h"
#include <thread>
#include <chrono>
#include <cmath>
#include "contact.h"
float interpolate(float prev, float curr, float alpha) {
return prev + (curr - prev) * alpha;
}
glm::vec2 interpolate(const glm::vec2& prev,
const glm::vec2& curr,
float alpha)
{
return glm::vec2{
interpolate(prev.x, curr.x, alpha),
interpolate(prev.y, curr.y, alpha)
};
}
void render_console(float x, float wall_x) {
constexpr int width = 60;
int pos = static_cast<int>((x / wall_x) * width);
pos = std::clamp(pos, 0, width - 1);
std::string line(width, '-');
line[pos] = 'o';
line[width - 1] = '|';
std::cout << "\33[2K\r"; // satırı temizle + başa dön
std::cout << line << std::flush;
}
void render_console_2d(
const glm::vec2& pos,
float wall_x,
int width,
int height,
const std::vector<ContactManifold>& manifolds
) {
if (width <= 0 || height <= 0)
return;
if (!std::isfinite(pos.x) || !std::isfinite(pos.y))
return;
// workaround for render
if (std::abs(wall_x) < 1e-6)
wall_x = 1.0;
if (!std::isfinite(wall_x) || std::abs(wall_x) < 1e-6)
return;
float world_y_max = wall_x * static_cast<float>(height) / width;
if (!std::isfinite(world_y_max) || std::abs(world_y_max) < 1e-6)
return;
float nx = pos.x / wall_x;
float ny = pos.y / world_y_max;
if (!std::isfinite(nx) || !std::isfinite(ny))
return;
nx = std::clamp(nx, 0.0f, 1.0f);
ny = std::clamp(ny, 0.0f, 1.0f);
int sx = static_cast<int>(nx * (width - 1));
int sy = static_cast<int>(ny * (height - 1));
sx = std::clamp(sx, 0, width - 1);
sy = std::clamp(sy, 0, height - 1);
std::vector<std::string> grid(height, std::string(width, ' '));
for (int r = 0; r < height; ++r)
grid[r][width - 1] = '|';
int draw_y = height - 1 - sy;
if (draw_y >= 0 && draw_y < height)
grid[draw_y][sx] = 'o';
for (const auto& m : manifolds) {
if (m.pointCount <= 0)
continue;
for (int i = 0; i < m.pointCount; ++i) {
const ContactPoint& cp = m.points[i];
if (!std::isfinite(cp.position.x) ||
!std::isfinite(cp.position.y))
continue;
if (!std::isfinite(cp.normal.x) ||
!std::isfinite(cp.normal.y))
continue;
draw_contact_point(grid, cp.position, wall_x);
draw_contact_normal(grid, cp.position, cp.normal, wall_x);
}
}
std::cout << "\33[2J\33[H";
for (const auto& row : grid)
std::cout << row << '\n';
std::cout << std::flush;
}
inline ScreenPoint world_to_screen(
const glm::vec2& p,
float wall_x,
int width,
int height
) {
float world_y_max = wall_x * static_cast<float>(height) / width;
float nx = p.x / wall_x;
float ny = p.y / world_y_max;
nx = std::clamp(nx, 0.0f, 1.0f);
ny = std::clamp(ny, 0.0f, 1.0f);
int sx = static_cast<int>(nx * static_cast<float>(width - 1) + 0.5f);
int sy = static_cast<int>(ny * static_cast<float>(height - 1) + 0.5f);
return {
sx,
height - 1 - sy
};
}
void draw_contact_point(
std::vector<std::string>& grid,
const glm::vec2& p,
float wall_x
) {
const size_t height = grid.size();
const size_t width = grid[0].size();
ScreenPoint sp = world_to_screen(p, wall_x, static_cast<int>(width), static_cast<int>(height));
grid[sp.y][sp.x] = 'x';
}
void draw_contact_normal(
std::vector<std::string>& grid,
const glm::vec2& p,
const glm::vec2& n,
float wall_x
) {
int height = grid.size();
int width = grid[0].size();
constexpr int len = 3;
for (int i = 1; i <= len; ++i) {
glm::vec2 q = {
p.x + n.x * 0.1 * i,
p.y + n.y * 0.1 * i
};
ScreenPoint sp = world_to_screen(q, wall_x, width, height);
if (sp.x >= 0 && sp.x < width &&
sp.y >= 0 && sp.y < height)
{
grid[sp.y][sp.x] = (n.x < 0 ? '<' :
n.x > 0 ? '>' :
n.y > 0 ? '^' : 'v');
}
}
}
void render_smoke_test()
{
std::cout << "\33[2J\33[H";
std::cout << "SMOKE_TEST_RENDER\n";
std::cout << "X\n";
std::cout << std::flush;
}