• Advertisement

Archived

This topic is now archived and is closed to further replies.

2D collision detection & STL

This topic is 5900 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

  
	for(ImgIt2 = MyMovingImages.begin(); ImgIt2 != ImgEndIt; ++ImgIt2)
	{
		if(ImgIt2 != this)
		{
			if(Bottom <= ImgIt2->Top || Right <= ImgIt2->Left || Top >= ImgIt2->Bottom || Left >= ImgIt2->Right)
				break;
				
			x -= xv;
			y -= yv;
			xv = 0;           
			yv = 0;
			
		}
	}
  
Does anyone know what''s wrong with this? I''m pretty sure that the collsion code is fine, so I''m betting there''s something wrong with the STL.

Share this post


Link to post
Share on other sites
Advertisement
No, there''s something wrong with your post. You''ve not said what the problem is, so how can you expect an answer? Compilation error? Run-time crash? Lack of expected behaviour?

Why use ImgEndIt instead of MyMovingImages.end() ? Why compare ImgIt2 (an iterator) against this (a pointer)? What is the function supposed to do? Where are you getting Top, Bottom, Left and Right from? Why use <= when it should probably be < (as equals would imply overlap)?

We need more information. I am 99.99% certain that it''s your code that is broken, not the STL.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I was wondering if there was a problem with my implementation of STL. Not the STL itself.

It seems like ImgEndIt (which == MyMovingImages.end() ) should be faster, since it doesn''t call a function or anything. I dunno. I could be wrong, but let''s get to the problem.

This code:
x -= xv;
y -= yv;
xv = 0;
yv = 0;

is never executed. I want it to execute whenever two elements of the MyMovingImages vector collide.

Share this post


Link to post
Share on other sites
I was wondering if there was a problem with my implementation of STL. Not the STL itself.

It seems like ImgEndIt (which == MyMovingImages.end() ) should be faster, since it doesn''t call a function or anything. I dunno. I could be wrong, but let''s get to the problem.

This code:
x -= xv;
y -= yv;
xv = 0;
yv = 0;

is never executed. I want it to execute whenever two elements of the MyMovingImages vector collide.

Share this post


Link to post
Share on other sites
Don''t optimise before you know you need to. That function call probably takes zero time anyway.

Like I said, comparing an iterator to a pointer is a bad thing to do. I can''t say much more because you''ve not even told us what the type of your container is, or what it stores, or indeed where all those variables come from in your collision detection condition.

[ MSVC Fixes | STL | SDL | Game AI | Sockets | C++ Faq Lite | Boost ]

Share this post


Link to post
Share on other sites
My container is a vector. It stores sprites. x = the x coordinate of the top left corner. y = the y coordinate of the top left corner.

Top is refreshed after every time the sprite is drawn. Top = (int)y + UBound; Bottom = (int)y + BBound; Right (int)x + RBound, you get the picture.

I get to assign any value to the bounding variables.

That loop that I showed in my original post is called every time before a sprite is drawn and moved. the "this" keyword refers to the sprite that''s being moved. I hope that helped.

Share this post


Link to post
Share on other sites
If I''m not supposed to compare an iterator to a pointer, then should I take the collision code out of the scope of the sprite class?

Still haven''t fixed it, but I figured out the break shouldn''t really be there. It stops the for loop.

Share this post


Link to post
Share on other sites
I guess I could make a new iterator and then assign it to "this".

BTW.... Now they collide when they aren't supposed to. I can't figure out waht triggers it, though.

Edited by - Forcas on February 23, 2002 4:07:53 PM

Share this post


Link to post
Share on other sites

  • Advertisement