Skip to content

Instantly share code, notes, and snippets.

@L0laapk3
Last active March 1, 2022 17:05
Show Gist options
  • Save L0laapk3/32544c6b92f32e85b951863991301959 to your computer and use it in GitHub Desktop.
Save L0laapk3/32544c6b92f32e85b951863991301959 to your computer and use it in GitHub Desktop.
#include "utils/renderBall.h"
#include "utils/bridge.h"
#include <array>
#include <iostream>
constexpr float M_PI = 3.14159265358979323846;
void renderCircle(RLURenderer& renderer, const vec3& center, float radius, const vec3& axis, const size_t numPoints, rlbot::Color color) {
std::vector<vec3> points{ numPoints + 1 };
float angle = 0;
const vec3 orth1 = normalize(vec3{ 0, -axis[2], axis[1] });
const vec3 orth2 = normalize(cross(axis, orth1));
for (int i = 0; i < numPoints; i++) {
angle += 2 * M_PI / numPoints;
points[i] = center + radius * (cosf(angle) * orth1 + sinf(angle) * orth2);
}
points.back() = points.front();
renderer.DrawPolyLine3D(color, points);
}
void renderBall(RLURenderer& renderer, const RLBotBM::Ball& ball, rlbot::Color color) {
auto center = vec3ToRLU(ball.position);
constexpr int stepsPerAxis = 4;
const std::array axes { vec3{ 1, 0, 0 }, vec3{ 0, 1, 0 }, vec3{ 0, 0, 1 } };
const auto ballRotator = quatToRLU(ball.orientation);
int totalPoints = 0;
int totalPolyLines = 0;
for (const auto& axis : axes) {
auto localAxis = dot(ballRotator, axis);
// renderer.DrawLine3D(rlbot::Color::green, center - ball.radius * localAxis, center + ball.radius * localAxis);
for (float t = -1.f + 1.f / stepsPerAxis; t < 1.f; t += 2.f / stepsPerAxis) {
vec3 circleCenter = center + ball.radius * t * localAxis;
float relSize = sqrtf(1 - t * t);
int numPoints = 6 + 6 * sqrtf(1 - t * t);
renderCircle(renderer, circleCenter, ball.radius * relSize, localAxis, numPoints, color);
totalPolyLines++;
totalPoints += numPoints + 1;
}
}
// std::cout << "Total polylines: " << totalPolyLines << " Total points: " << totalPoints << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment