Collision Rectangles

The Partridge Family were neither partridges nor a family. Discuss.
albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 12th, 2017, 11:11 pm

Now just have ClipTo return a value and you'd be set lol.

Code: Select all

bool Intersect::BoundingBox(const RectF& A, const RectF& B, Vec2f& correction)
{      
   if (!A.Overlaps(B))
   {
      correction = { 0.0f,0.0f };
      return false;
   }
         
   const RectF penetration = A.ClipTo(B);
   correction = Vec2f(penetration.GetWidth(), penetration.GetHeight());
   return true;
}
Just remember, since A is a const reference, Rect::ClipTo would also need to be const.

Code: Select all

Rect ClipTo(const Rect& R)const
{
     return {
          std::max(left, R.left),
          std::max(top, R.top),
          std::min(right, R.right),
          std::min(bottom, R.bottom)
     };
}
Just nit picking. I'm a proponent of using const everywhere and return by value symantics to accomplish more use of const on assignments. Compilers usually optimize returning a temporary and turning it into a move operation, so it optimizes into

Code: Select all

correction = { 
     std::max(left, R.left), std::max(top, R.top), 
     std::min(right, R.right), std::min(bottom, R.bottom)
};
and not just because it probably getting inlined.

Anyway, keep it up man, it's looking good.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Collision Rectangles

Post by MrGodin » November 13th, 2017, 12:20 am

Fixed up the Rect class to do that, but yep all is coming along here.
I do have issues with std::max/min. I include <algorithm> but i still have to use it like this std::max<T>( left,r.left). I don't know why this is but been that way for a while.
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 13th, 2017, 12:54 am

Yeah, both params have to be of the same type (int,int) (float, float) and so on. Otherwise, you do have to use the template parameter <int> or <float>, etc
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 13th, 2017, 12:58 am

another thing that might be the issue is if min/max show as macros it's because they are defined in WinUser.h I believe. This means that it tries using the macro versions defined by the win32 api instead of the stl version. If you replace all #include <Windows.h> with #include "ChiliWin.h" and make sure that it is included before ANY win32 headers (d3d11.h, d2d1.h, dwrite.h, etc.) it should get resolved. In ChiliWin.h there is a NOMINMAX define that prevents the macros from being defined, so you can use the stl versions.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 13th, 2017, 12:59 am

I've been messing with your code and I did as I described above. It took me almost an hour to find all the offending #includes from all the headers and source files, but it did work.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 13th, 2017, 1:11 am

When I say messing with your code, I've been trying to make it more C++ oriented by using RAII ( shared_ptr, ComPtr ), return by value, const initialization, pass by const ref, etc...

Refactoring someone else's code is tough, especially when you aren't sure of the architecture of the project. I do it for fun to though. There is something to be learned by doing it, but I haven't learned it yet. I still make the same mistakes. The biggest one is not getting to know the context in which something is used before making changes. So while everything might compile, it crashes at some point.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Collision Rectangles

Post by MrGodin » November 15th, 2017, 1:57 am

I've been thinking, i am so lazy with my const correctness i think i'm going to refactor a bunch of stuff. I've gotten lazy with some coding and frankly, the way the standard changes so much, it's easy to just spend a whole lotta time learning all this stuff and never really coding up something exciting. If you know what i mean.
Curiosity killed the cat, satisfaction brought him back

MrGodin
Posts: 721
Joined: November 30th, 2013, 7:40 pm
Location: Merville, British Columbia Canada

Re: Collision Rectangles

Post by MrGodin » November 15th, 2017, 2:37 am

I just had a thought about grouping together solid tiles that are touching. Useing pathfinding to start at a tile then collect all the tiles that meet the pathfinding criteria ie: instead of heuristic calculations do it against passable or not ?. I seem to be dead set on making exposed (to the entity )sides of solid tiles' sides as lines with a normal (clockwise winding). Again i just thought of this.. hmm so i set the pathfinding to go until it cannot find any more adjacent solid tiles, all the while creating a "group" of connecting tiles, then simply get all the outside edges of the "group" then make lines. hmm .. sound reasonable ?
Curiosity killed the cat, satisfaction brought him back

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 15th, 2017, 9:52 am

Creating a polygonal shape is something I had considered suggesting, but didn't know if you were up for the challenge and I definitely wasn't.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Collision Rectangles

Post by albinopapa » November 15th, 2017, 8:11 pm

MrGodin wrote:I've been thinking, i am so lazy with my const correctness i think i'm going to refactor a bunch of stuff. I've gotten lazy with some coding and frankly, the way the standard changes so much, it's easy to just spend a whole lotta time learning all this stuff and never really coding up something exciting. If you know what i mean.
I can understand the concern, and it isn't necessary to implement all the latest features the C++ standards come up with.

There are things that keep me guessing though. Pass by value or pass by constant reference. It's usually common knowledge to pass structs/classes by constant reference, but with copy elision and move semantics it's not so black and white. Copy elision is where you instantiate an object using another object either by returning the source from a function or passing to a function that has value parameters.

struct S{};

S Foo( S s )
{
return s;
}

S a;
S b = Foo(a); // b will be instantiated using a copy of a, but only one copy is made, from a to s
S c = Foo( S{} ); // c will be instantiated using default constructor of S, no copies will be made.

This seems trivial, but I'm obsessed with getting the best performance, so for me not having a clear path is crippling. Then there's cache locality. For passing small classes, say a vec3, would it be faster to pass by value so that the data is fresh in the cache?

Aside from that, I tend to give up when I spend a few days on something and can't figure it out. Collision handling is one of the biggest hurdles I've had. Not so much the detection, but the correction portion. Then there is architecture. How to get things to interact with each other without too much coupling. I like the use of a messaging system, and yours is pretty straight forward using defines or ints and switch statements, but I want something a little more object oriented and I haven't been able to grasp that yet. Probably because I'm trying to use polymorphism and inheritance. I should probably try using templates, but then there is the issue of not being able to use std::vector. I have shied away from using map/unordered map, perhaps it's time to look into it.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply