Jump to content
  • Advertisement
Sign in to follow this  
Antonym

Colision detection

This topic is 3494 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

I am trying to set up collision detection however it doesnt seem to work.. the code compiles fine but no collisions seem to be detected, I tried two methods, both similar but with different implementation(One uses helper functions, the commented out one and the other doesnt). I can't figure out why aren't any collisions detected in either method. The methods work by iterating through all objects in game and then for each of those iterating through all others to check if it's in collision with any of them, if it is a boolean member of both(initialized as true) is set to false. Then it iterates again through all objects in game to check if they have 'alive' set to false(meaning they have collided with something) and removes them from the vector of game objects. Objects that aren't in the game objects vector aren't drawn but all objects continue to get drawn wether they seem to have collided or not..
void update(Application &application,
			Input &input, 
			std::vector<Object> &game_objects)
{
	std::vector<Object>::iterator it = game_objects.begin();

	std::vector<Object>::iterator it2;
	for(; it != game_objects.end(); it++)
	{
		it2 = game_objects.begin();
		Object object1 = *it;
		for(; it2 != game_objects.end(); it2++)
		{
			Object object2 = *it2;
			check_collision(object1,object2);
		}
	}
	
	it = game_objects.begin();
	for(; it != game_objects.end(); it++)
	{
		if(!it->get_state())
		{
			game_objects.erase(it);
			it = game_objects.begin();
		}
	}

	it = game_objects.begin();

//The following code is an alternative collision detection..
/*	it = game_objects.begin();
	for(; it != game_objects.end(); it++)
	{
		Object object = *it;
		std::for_each(it, game_objects.end(), CollisionFunctor(object));
	}

	it = game_objects.begin();
	std::vector<Object>::iterator end;
	end = std::remove_if(game_objects.begin(), game_objects.end(), check_state);
	game_objects.erase(end, game_objects.end());

	it = game_objects.begin();*/
	it++;
	it++;
	for(; it < game_objects.end(); it++)
	{
		it->move(1);
	}
}

//Check collision
void check_collision(Object &object, Object &object2)
{

	if((object.get_position().x < (object2.get_position().x + 32) &&
	    object.get_position().x > (object2.get_position().x - 32) &&
	    object.get_position().y < (object2.get_position().y + 32) &&
	    object.get_position().y > (object2.get_position().y - 32)))
	{
		object.set_state(false);
		object2.set_state(false);
	}
}

void Object::set_state(bool new_state)
{
	alive = new_state;
}


This code belongs to the commented out section of code..
#include "CollisionFunctor.h"
#include "Object.h"

CollisionFunctor::CollisionFunctor(Object &object1)
:
object(object1)
{}

void CollisionFunctor::operator()(Object &object2) 
{
	collision_check(object2);
}

void CollisionFunctor::collision_check(Object &object2)
{
	if((object.get_position().x < (object2.get_position().x + 32) &&
	    object.get_position().x > (object2.get_position().x - 32) &&
	    object.get_position().y < (object2.get_position().y + 32) &&
	    object.get_position().y > (object2.get_position().y - 32)))
	{
		object.set_state(false);
		object2.set_state(false);
	}
}


[Edited by - Antonym on December 25, 2008 4:34:26 PM]

Share this post


Link to post
Share on other sites
Advertisement
I'm not too sure about the prioritys of < > and &&, you should try this:

<code lang="c++">void CollisionFunctor::collision_check(Object &object2)
{
if((object.get_position().x < (object2.get_position().x + 32)) &&
(object.get_position().x > (object2.get_position().x - 32)) &&
(object.get_position().y < (object2.get_position().y + 32)) &&
(object.get_position().y > (object2.get_position().y - 32)))
{
object.set_state(false);
object2.set_state(false);
}
}</code>

Share this post


Link to post
Share on other sites
It looks like that collision check is only checking a collision of object1 (x,y) most likely the top left corner, with a 64 unit box around object2's (x,y). Thats really only useful if object1 is a mouse cursor.

Check the thread '2D collisions' on the next page from yours it has bounding box collision code. http://www.gamedev.net/community/forums/topic.asp?topic_id=518947

Also: It's better to get the width and height from somewhere not just use the constant 32. And looping through the list of objects will probably cause a problem if object1 == object2 I didn't notice a check for that. Only checking collisions on things that need checking is much more efficient. For example, checking between the player and player bullets is probably not needed. But worry about that after the collision works

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!