From 2c9275642a3a5755dadb7401d2db121afc955e8e Mon Sep 17 00:00:00 2001 From: BuildTools Date: Tue, 26 Jan 2021 08:04:33 -0500 Subject: [PATCH] failsafe distance limiter on rays --- main.cpp | 4 +++- stuff/objects/ray2D.h | 7 +++++-- stuff/objects/vector2.h | 10 ++++++++++ 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/main.cpp b/main.cpp index 30d4635..52196bb 100644 --- a/main.cpp +++ b/main.cpp @@ -32,13 +32,15 @@ int main(int argc, char** argv) { callbacks::handleCallbacks(&eventHandler); + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); + SDL_RenderFillRect(renderer, NULL); + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); ray2D ray = ray2D::trace(vector2(0, 201), vector2(.01, 0)); ray2D ray2 = ray2D::trace(vector2(201, 0), vector2(0, .01)); SDL_RenderDrawLine(renderer, ray.start.x, ray.start.y, ray.end.x, ray.end.y); SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y); - global::entList.run(renderer); global::entList3D.run(renderer); diff --git a/stuff/objects/ray2D.h b/stuff/objects/ray2D.h index 804ae57..4cf117e 100644 --- a/stuff/objects/ray2D.h +++ b/stuff/objects/ray2D.h @@ -16,7 +16,7 @@ struct ray2D float dist; static ray2D trace(vector2 start, vector2 direction) { - ray2D ray { start, start, 19 }; + ray2D ray { start, start, -1 }; vector2 point = start; bool hit = false; while (!hit) @@ -27,11 +27,14 @@ struct ray2D { ray.start = start; ray.end = point; - ray.dist = 19; + ray.dist = start.distance(point); hit = true; break; } } + ray.dist = start.distance(point); + if (ray.dist > 1000) + break; point.x += direction.x; point.y += direction.y; } diff --git a/stuff/objects/vector2.h b/stuff/objects/vector2.h index 80ffcec..c895772 100644 --- a/stuff/objects/vector2.h +++ b/stuff/objects/vector2.h @@ -1,4 +1,5 @@ #pragma once +#include /* vector2s hold 2D cartesian coordinates */ @@ -15,4 +16,13 @@ struct vector2 this->x = x_; this->y = y_; } + float length() + { + return sqrtf((x * x) + (y * y)); + } + float distance(vector2& other) + { + vector2 dist = vector2(this->x - other.x, this->y - other.y); + return dist.length(); + } }; \ No newline at end of file