Jump to content
  • Advertisement
Sign in to follow this  

Collision Problem

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

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
Sign in to follow this  

  • Advertisement

Important Information

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

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!