2d Platform Slope Collision

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 2d Platform Slope Collision

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: 2d Platform Slope Collision

Post 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!
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 2d Platform Slope Collision

Post 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...
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: 2d Platform Slope Collision

Post by like80ninjas »

I could see how that combination of events could lead to anger lol.
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: 2d Platform Slope Collision

Post 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.
User avatar
EccentricDuck
Chaos Rift Junior
Chaos Rift Junior
Posts: 305
Joined: Sun Feb 21, 2010 11:18 pm
Current Project: Isometric "2.5D" Airship Game
Favorite Gaming Platforms: PS2, SNES, GBA, PC
Programming Language of Choice: C#, Python, JScript
Location: Edmonton, Alberta

Re: 2d Platform Slope Collision

Post 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.
Last edited by EccentricDuck on Tue Jul 12, 2011 6:39 pm, edited 1 time in total.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 2d Platform Slope Collision

Post 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.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
EccentricDuck
Chaos Rift Junior
Chaos Rift Junior
Posts: 305
Joined: Sun Feb 21, 2010 11:18 pm
Current Project: Isometric "2.5D" Airship Game
Favorite Gaming Platforms: PS2, SNES, GBA, PC
Programming Language of Choice: C#, Python, JScript
Location: Edmonton, Alberta

Re: 2d Platform Slope Collision

Post 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.
like80ninjas
Chaos Rift Regular
Chaos Rift Regular
Posts: 101
Joined: Thu Dec 09, 2010 2:13 am

Re: 2d Platform Slope Collision

Post by like80ninjas »

I still haven't gotten it to work right.

and Falco I was kidding I knew you weren't angry lol.
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: 2d Platform Slope Collision

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

Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Post Reply