Sign in to follow this  

Collision Bouncing

Recommended Posts

Hello I am trying to have my collisions cause the objects to bounce from one another hoewever when my objects collide they seem to attract each other(Although not always sometimes they do bounce, or not bounce at all) I don't know what I am doing wrong, any help would be appreciated. Thanks
	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))
		D3DXVECTOR2 momentum;
		momentum.x = mass * velocity.x;
		momentum.y = mass * velocity.y;
		float Cr = 1; //Coefficiente of restitution

		velocity = ((Cr + 1) * other.mass * other.velocity + velocity * (mass - Cr * other.mass))/(mass + other.mass);
		other.set_velocity(((Cr + 1) * mass * velocity + other.velocity * (other.mass - Cr * mass))/(mass + other.mass));
		speed = sqrt(velocity.x*velocity.x + velocity.y*velocity.y);
		other.set_speed(sqrt(other.velocity.x*other.velocity.x + other.velocity.y*other.velocity.y));
		rotation = radians_to_degrees(atan2(velocity.y, velocity.x));
		other.set_rotation (radians_to_degrees(atan2(velocity.y, velocity.x)));

		handle_collision(game, other);

[Edited by - Antonym on January 17, 2009 1:54:41 PM]

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