Page 2 of 2

Re: 2d Platform Slope Collision

Posted: Mon Jul 11, 2011 7:01 pm
by avansc
GyroVorbis wrote:
like80ninjas wrote:At the moment it isn't suppose to be any sort of fast, I just want to get it to work first lol.
This isn't a matter of speed, optimization, or design. This is a matter of you're committing an unforgivable C/++ crime.

You're leaking 256 bytes of memory every frame.

How much harder would it be to change that array of pointers to just an array and not call new? It would be even easier. :)

You don't happen to come from a C#/Java background, do you?
heh, yeah dont do that, I dont even know why I did that, it may have just been easier at the time, or maybe it was just a hack job, but yeah, dont do that. ever.

Re: 2d Platform Slope Collision

Posted: Mon Jul 11, 2011 7:41 pm
by like80ninjas
I know I know, i'll change it! I got the code almost directly from avansc so that's why it's like that. I'd just make it 4 not call new!

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 9:00 am
by Falco Girgis
like80ninjas wrote:I know I know, i'll change it! I got the code almost directly from avansc so that's why it's like that. I'd just make it 4 not call new!
I'll have a look at the actual algorithm hopefully in a few. I only bitched about the dynamic allocation because I am/was sitting on the shitter at work on my iPad...

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 11:34 am
by like80ninjas
I could see how that combination of events could lead to anger lol.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 12:59 pm
by Falco Girgis
Who said anything about me being angry? :shock:

Trying to help people fix memory leaks is not something one does just because they're angry.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 4:23 pm
by EccentricDuck
GyroVorbis wrote:
like80ninjas wrote:At the moment it isn't suppose to be any sort of fast, I just want to get it to work first lol.
This isn't a matter of speed, optimization, or design. This is a matter of you're committing an unforgivable C/++ crime.

You're leaking 256 bytes of memory every frame.

How much harder would it be to change that array of pointers to just an array and not call new? It would be even easier. :)

You don't happen to come from a C#/Java background, do you?
I just realized that doing that in Java equates to calling the GC (with typical settings where it's invoked ~1 MB) almost once a minute more than you would otherwise at 60 fps.

256 bytes/s * 60 fps = 15 kB per second

1 MB / 15 kB per second = 68.27 seconds

That's not a huge deal, but insert more than one entity doing that check into there and you may get some noticeable slowdowns in Java (or C#).


In my XNA game I believe I just assigned the values to the existing vector fields and then created a 3D transformation matrix out of them (which is a struct) which needed to be dynamically created every frame for rendering. Luckily it's a struct though (can't do that in Java since there are no structs) and I just re-used the same reference for it every frame.

I'm still not entirely sure where I took my biggest memory hit with the game but it came from creating a trail of hundreds of spheres that trailed behind the game entities. I used a static List to store all spheres as simply a list of Vector3s with an additional Color parameter using a struct to contain the two of them. All the information for those spheres was stored in a single object which used the list to render each sphere by re-using the same model, but it still managed to take up a massive chunk of memory that I couldn't account for (keep in mind this is C#). Still, it was way better than creating an entirely new sphere object for every sphere I laid (each entity of 6 laid several a second). In the end my biggest limitations came from the Xbox 360 being ridiculously slow with memory allocation compared to PC and I wasn't able to get the same fluid performance out of it as I could on my computer. It's definitely worth thinking about how you allocate memory though. I wonder if Falco, Avansc, or another members could create (or point to) an article on how to take proper advantage of caching. <---------------- (I'm requesting this mostly for myself since it's something I'd like to take advantage of)

EDIT: Actually, now that I think about it, I think I may have been unnecessarily calculating the transformation matrix for every sphere on the list rather than pre-calculating the first part of the matrix and storing it. To the OP, it's not a huge deal when you only have a few objects, but it's definitely good to become aware of seemingly minor performance enhancements like the one pointed out above. I learned a hell of a lot from my mistakes - mostly because I was actively looking for what mistakes I made and how I could improve upon them.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 4:52 pm
by avansc
at the time of writing that it was when everyone and there mom on here was interested in sat, and it was "sat" out quick. You can go look at pretty much any code i post on here, i dont do clean up, i dont care if it leaks, its just the idea, that being said, I still have no idea why i would use new there, maybe i ported java code and it was an non mutable object, i just dont know.

Anyways, the algorithm as far as I know worked. If you are really interested in having it clean version I'll pop one out for you.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 6:41 pm
by EccentricDuck
avansc wrote:at the time of writing that it was when everyone and there mom on here was interested in sat, and it was "sat" out quick. You can go look at pretty much any code i post on here, i dont do clean up, i dont care if it leaks, its just the idea, that being said, I still have no idea why i would use new there, maybe i ported java code and it was an non mutable object, i just dont know.

Anyways, the algorithm as far as I know worked. If you are really interested in having it clean version I'll pop one out for you.
Frankly, I think most of your code examples are great. I wouldn't sweat it.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 6:43 pm
by like80ninjas
I still haven't gotten it to work right.

and Falco I was kidding I knew you weren't angry lol.

Re: 2d Platform Slope Collision

Posted: Tue Jul 12, 2011 7:07 pm
by avansc
yeah im not sweating it, but that is an crappy bit of code, but yeah, I think there might be something wrong with thecode. I'll check it out when I have some time.

meanwhile this should work for you too.

Code: Select all


int side_of_line(Vec2 &a, Vec2 &b, Vec2 &p)
{
   return (p.y - a.y) * (b.x - a.x) - (p.x - a.x) * (b.y - a.y);
}

bool point_inside_poly(std::vector<Vec2> &poly, Vec2 &point)
{
   for(int i = 0; i < poly.size()-1; i++)
   {
      if(side_of_line(poly[i], poly[i+1], point) > 0)
         return false;

   }
   if(side_of_line(poly[poly.size()-1], poly[0], point) > 0)
         return false;
   return true;
}