• Advertisement
Sign in to follow this  

simple collision detection jitters

This topic is 3961 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 everyone, I'm building a simple 2D collision detection engine and have some trouble. 1, entities sometime stick together 2, entities _rarely_ bounce away from each other in the wrong direction (about the opposite they'd need to -- this coould be in conjunction with the earlier problem). it's curious, as they bounce away from their bounding boxes in the right direction. I use polymorphic Shape classes for both collision check and bounding box (the key point being them able to tell if a point is inside them, and give their nearest point to a given point). At the moment it's only circles and rectangles. The collision handling code:
void	Behavs::Separate( Entity *e1, Entity *e2, double tDelta )
{
	Vec3D	e1pos = e1->getPos();
	Vec3D	e2pos = e2->getPos();

	Vec3D	e1vel = e1->getVel();
	Vec3D	e2vel = e2->getVel();

	// set them back where they've been a frame ago
	e1->setPos( e1pos - e1vel * ( tDelta ) );
	e2->setPos( e2pos - e2vel * ( tDelta ) );
}

void	Behavs::BounceEqual( Entity *e1, Entity *e2, double tDelta )
{
	Vec3D	e1pos = e1->getPos();
	Vec3D	e2pos = e2->getPos();
	Vec3D	e1norm = ( e1pos - e2pos ).unit();
	Vec3D	e2norm = ( e2pos - e1pos ).unit();
	Vec3D	e1vel = e1->getVel();
	Vec3D	e2vel = e2->getVel();

	Separate( e1, e2, tDelta );

	e1->setVel( e1vel.reflect( e2norm ) );
	e2->setVel( e2vel.reflect( e1norm ) );
}

// the reflection formula
Vec3D	Vec3D::reflect( const Vec3D &normal ) const
{
	return	*this + normal * -2.0f * ( this->dot( normal ) );
}

is there a more elegant and effective solution? some ideas about how to prevent entities "crushing" each other (i.e. to further move into each other after colliding)? thanks for your help in advance.

Share this post


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

  • Advertisement