Public Group

# simple collision detection jitters

This topic is 4136 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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.

1. 1
2. 2
Rutin
26
3. 3
4. 4
5. 5

• 9
• 13
• 19
• 14
• 9
• ### Forum Statistics

• Total Topics
632940
• Total Posts
3009330
• ### Who's Online (See full list)

There are no registered users currently online

×