Sign in to follow this  

Collision Problem

Recommended Posts

Hello I am trying to having elastic collisions, they work half of the time but the other half the colliding objects stick together and don't let go.. Can someone help me find out why/fix this? Object is the class that contains the radius, mass, velocity, position.
void Object::check_collision(Game &game, Object &other)
	float dx = position.x - other.get_position().x;
	float dy = position.y - other.get_position().y;
	float d = sqrt(dx*dx + dy*dy);

	if(d < (radius + other.radius))
		float Cr = 1; //Coefficiente of restitution
		float m1 = mass;
		float m2 = other.get_mass();
		D3DXVECTOR2 v1 = velocity;
		D3DXVECTOR2 v2 = other.get_velocity();

		D3DXVECTOR2 Vf1 = ((Cr + 1) * m2 * v2 + v1 * (m1 - Cr * m2))/(m1 + m2);
		D3DXVECTOR2 Vf2 = ((Cr + 1) * m1 * v2 + v2 * (m2 - Cr * m2))/(m1 + m2);

		velocity = Vf1;

		speed = sqrt(Vf1.x*Vf1.x + Vf1.y*Vf1.y);
		other.set_speed(sqrt(Vf2.x*Vf2.x + Vf2.y*Vf2.y));

		rotation = radians_to_degrees(atan2(Vf1.y, Vf1.x));
		other.set_rotation (radians_to_degrees(atan2(Vf2.y, Vf2.x)));

		handle_collision(game, other);

Share this post

Link to post
Share on other sites
I think more info is needed here, but from what I can tell you from past experience such situations mean that even if you have a solution to a collision some objects may behave in a way to always have a collision be true, possible making the "stick" you are seeing. I'd suggest making a vector to store the position of an object in the last frame to revert to in case of a collision. Of course, that won't be smart if the last position is really far away from a high velocity object.

You may want to look at ray-triangle collisions for high velocity objects, and maybe methods like yours for more slowly moving ones. Admittedly I know nothing about ray-tri and little about the latter, but I do know those are possible avenues.

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this