refactor entitylists to use square*s, get pointer to hit entity if one is hit by ray

master
BuildTools 4 years ago
parent af8b91ab33
commit a8d8616b77
  1. 10
      main.cpp
  2. 6
      stuff/objects/entityList2D.h
  3. 6
      stuff/objects/ray2D.h

@ -24,7 +24,7 @@ int main(int argc, char** argv)
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
SDL_Event eventHandler; SDL_Event eventHandler;
global::entList.push_back(square(200, 200, 50)); global::entList.push_back(&square(200, 200, 50));
//global::entList3D.push_back(cube(200, 200, 100, 50)); //global::entList3D.push_back(cube(200, 200, 100, 50));
@ -40,10 +40,12 @@ int main(int argc, char** argv)
ray2D ray2 = ray2D::trace(vector2(201, 0), vector2(0, 1)); ray2D ray2 = ray2D::trace(vector2(201, 0), vector2(0, 1));
SDL_RenderDrawLine(renderer, ray.start.x, ray.start.y, ray.end.x, ray.end.y); 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); SDL_RenderDrawLine(renderer, ray2.start.x, ray2.start.y, ray2.end.x, ray2.end.y);
std::cout << global::entList.at(0) << std::endl;
if (ray.hitEnt)
std::cout << std::hex << ray.hitEnt << std::endl;
//demo rays with moving object //demo rays with moving object
global::entList.at(0).x = global::mousePos.x; global::entList.at(0)->x = global::mousePos.x;
global::entList.at(0).y = global::mousePos.y; global::entList.at(0)->y = global::mousePos.y;
global::entList.run(renderer); global::entList.run(renderer);
global::entList3D.run(renderer); global::entList3D.run(renderer);

@ -6,14 +6,14 @@
/* /*
entityList2D is the 2D entity container class entityList2D is the 2D entity container class
*/ */
class entityList2D : public std::vector<square> class entityList2D : public std::vector<square*>
{ {
public: public:
void run(SDL_Renderer* renderer) void run(SDL_Renderer* renderer)
{ {
for (square entity : *this) for (square* entity : *this)
{ {
entity.run(renderer); entity->run(renderer);
} }
} }
}; };

@ -14,6 +14,7 @@ struct ray2D
vector2 start; vector2 start;
vector2 end; vector2 end;
float dist; float dist;
square* hitEnt;
static ray2D trace(vector2 start, vector2 direction) static ray2D trace(vector2 start, vector2 direction)
{ {
ray2D ray { start, start, -1 }; ray2D ray { start, start, -1 };
@ -21,13 +22,14 @@ struct ray2D
bool hit = false; bool hit = false;
while (!hit) while (!hit)
{ {
for (square object : global::entList) for (square* object : global::entList)
{ {
if (inRect(point, vector2(object.x, object.y), vector2(object.x + object.radius, object.y + object.radius))) if (inRect(point, vector2(object->x, object->y), vector2(object->x + object->radius, object->y + object->radius)))
{ {
ray.start = start; ray.start = start;
ray.end = point; ray.end = point;
ray.dist = start.distance(point); ray.dist = start.distance(point);
ray.hitEnt = object;
hit = true; hit = true;
break; break;
} }

Loading…
Cancel
Save