failsafe distance limiter on rays

master
BuildTools 5 years ago
parent 59eb847be5
commit 2c9275642a
  1. 4
      main.cpp
  2. 7
      stuff/objects/ray2D.h
  3. 10
      stuff/objects/vector2.h

@ -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);

@ -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;
}

@ -1,4 +1,5 @@
#pragma once
#include <math.h>
/*
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();
}
};
Loading…
Cancel
Save