• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
lukabratzi

Collision resolution between multiple AABB objects

5 posts in this topic

Greetings everyone,

 

I'm working on implementing my own *very* basic 2D collision engine for AABB objects to use in my 2D platformer I'm trying to make.  

 

I've managed to get the following working:

  • Collision detection
  • Collision between one movable sprite and one nonmovable sprite (mass == 0)
  • Collision between exactly two movable sprites

What I'm having trouble with is resolving multiple collisions at a time.  For instance, suppose that the player is pushing another object into a third object along the x axis.  In this case, the second object should push up against the third object with the player pushing against the middle object.  Instead, all of my objects are penetrating each other and the resolution isn't happening.  In the attached image, the player (rectangle 1) is pushing another object (rectangle 2) into a third object (rectangle 3).

 

Here are the relevant portions of my code.  Again, I've never implemented any kind of collision detection before so this is probably hideous and I'm sure that I am doing an unfathomable number of things wrong.  ANY critique or suggestions would be appreciated but I'd specifically like to address the above problem.

 

If anyone needs additional information or code then just let me know and I'd be happy to upload it.

 

void CollisionManager::HandleSpriteCollisions(const std::vector<Sprite*>& sprites)
{
	for(size_t k1 = 0; k1 < sprites.size(); ++k1)
	{
		PhysicsComponent* physics = sprites[k1]->GetFirstComponentOfType<PhysicsComponent>();
		if(physics)
			physics->m_velocityChangesToApply = Vector2D::ZeroVector();

		for(size_t k2 = k1 + 1; k2 < sprites.size(); ++k2)
		{
			if(k1 == k2)
			        continue;

			bool spritesOverlap = DoSpritesOverlap(sprites[k1], sprites[k2]);
			if(spritesOverlap)
				HandleSpriteCollision(sprites[k1], sprites[k2]);
		}
	}

	for(size_t k1 = 0; k1 < sprites.size(); ++k1)
	{
		PhysicsComponent* physics = sprites[k1]->GetFirstComponentOfType<PhysicsComponent>();
		if(physics)
			physics->m_position += physics->m_velocityChangesToApply;
	}
}
void CollisionManager::HandleSpriteCollision( Sprite* spriteA, Sprite* spriteB )
{
	Vector2D overlapAxis;
	float overlapAmount = CalculateOverlapAmount(spriteA, spriteB, overlapAxis);
	if(overlapAmount == 0)
		return;

	PhysicsComponent* aPhysicsComponent = spriteA->GetFirstComponentOfType<PhysicsComponent>();
	PhysicsComponent* bPhysicsComponent = spriteB->GetFirstComponentOfType<PhysicsComponent>();
	if(!aPhysicsComponent || !bPhysicsComponent)
		return;

	float spriteAMass = aPhysicsComponent->m_mass;
	float spriteBMass = bPhysicsComponent->m_mass;
	float totalMass = spriteAMass + spriteBMass;
	float proportionalMassA = spriteAMass / totalMass;
	float proportionalMassB = spriteBMass / totalMass;

	float penetrationDepth = overlapAmount + 1;

	Vector2D spriteDirectionVector = (bPhysicsComponent->m_collisionShape->position - aPhysicsComponent->m_collisionShape->position).Normalize();
	Vector2D hitNormalA = overlapAxis * penetrationDepth;
	Vector2D hitNormalB = hitNormalA * -1.f;

	Vector2D resolutionVector;

	if(aPhysicsComponent->m_mass == 0 && bPhysicsComponent->m_mass == 0)
		return;

	if(aPhysicsComponent->m_mass > 0 && bPhysicsComponent->m_mass > 0)
	{
		Vector2D impulse = hitNormalA;
		float restitutionCoefficient = -0.5f;

		impulse *= (-(1.0f + restitutionCoefficient));
		impulse /= (1/spriteAMass + 1/spriteBMass);

		aPhysicsComponent->m_velocityChangesToApply += (hitNormalA);
		bPhysicsComponent->m_velocityChangesToApply += (hitNormalB);
	}
	else if(aPhysicsComponent->m_mass == 0)
	{
		bPhysicsComponent->m_velocityChangesToApply += ( hitNormalB );
	}
	else if(bPhysicsComponent->m_mass == 0)
	{
		aPhysicsComponent->m_velocityChangesToApply += ( hitNormalA );
	}
}

 

 

 

0

Share this post


Link to post
Share on other sites

if(aPhysicsComponent->m_mass > 0 && bPhysicsComponent->m_mass > 0)
{
Vector2D impulse = hitNormalA;
float restitutionCoefficient = -0.5f;

impulse *= (-(1.0f + restitutionCoefficient));
impulse /= (1/spriteAMass + 1/spriteBMass);

aPhysicsComponent->m_velocityChangesToApply += (hitNormalA);
bPhysicsComponent->m_velocityChangesToApply += (hitNormalB);
}

 

 

Would this work?

 

    aPhysicsComponent->m_velocityChangesToApply += (hitNormalA) * impulse / spriteAMass;
    bPhysicsComponent->m_velocityChangesToApply += (hitNormalB* impulse / spriteBMass;

1

Share this post


Link to post
Share on other sites

if(aPhysicsComponent->m_mass > 0 && bPhysicsComponent->m_mass > 0)
{
Vector2D impulse = hitNormalA;
float restitutionCoefficient = -0.5f;

impulse *= (-(1.0f + restitutionCoefficient));
impulse /= (1/spriteAMass + 1/spriteBMass);

aPhysicsComponent->m_velocityChangesToApply += (hitNormalA);
bPhysicsComponent->m_velocityChangesToApply += (hitNormalB);
}

 

 

Would this work?

 

    aPhysicsComponent->m_velocityChangesToApply += (hitNormalA) * impulse / spriteAMass;
    bPhysicsComponent->m_velocityChangesToApply += (hitNormalB* impulse / spriteBMass;

Well both hitNormalA/B and impulse are both vectors.  Ideally impulse should be some force and direction applied on collision to both objects for a bounce effect, but I'm not even using that right now.  currently I'm just trying to move the objects apart by the penetration amount (hitNormalA = hitNormal * penetrationDepth) to separate them.

 

What's happening is that my method breaks when any one object is colliding with more than one other object.  I think that the middle object is being acted upon by both sides and can't resolve either collision as its always blocked by one of the other two objects.

 

So that makes me think that it may not necessarily be a problem with my collision resolution code but is instead more of a fundamental, conceptual problem with how I'm conducting my collision tests?

Edited by lukabratzi
0

Share this post


Link to post
Share on other sites
In the first loop you clear the velocity change when you first encounter a sprite, but it may have already been in a collision by that point.

For example if the first rectangle hits the second, the second will receive a velocity change, which will be cleared before it is applied.

You may also need to run through the whole list multiple times to resolve all intersections.
1

Share this post


Link to post
Share on other sites

In the first loop you clear the velocity change when you first encounter a sprite, but it may have already been in a collision by that point.

For example if the first rectangle hits the second, the second will receive a velocity change, which will be cleared before it is applied.

You may also need to run through the whole list multiple times to resolve all intersections.

Good god, I feel ridiculously foolish for missing that.  I'll test that out later today and see if it fixes it, but it would explain a lot.  Sometimes its the simple things I guess.

 

I had tried running through the process multiple times and that lessened the problem.  Is this an actual approach that some physics engines take?  It seems so inefficient.

0

Share this post


Link to post
Share on other sites
Yes, but they will narrow down the list by finding potential collisions first, and the iteration will be a tight loop over the minimum amount of data possible.
0

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  
Followers 0