Box2D for Collision in C++

Hey guys. I’m an Amateur here, and I trying to get one sprite to disappear when it collides with the other.

I’m interested in using Box2D to achieve this. I know it’s a lot, but if someone could help me out with code to achieve this, I’d be grateful! Thank you!

You can do it like this:

b2CircleShape *circle;
b2Transform *circleTransform;

b2PolygonShape *poly;
b2Transform *polyTransform;

circle = new b2CircleShape();
circle->m_radius = 25.0f / PTM_RATIO;
circleTransform = new b2Transform();
circleTransform->Set(b2Vec2(0.0f / PTM_RATIO, 100.0f / PTM_RATIO), 0.0f);

poly = new b2PolygonShape();
poly->SetAsBox(25.0f / PTM_RATIO, 25.0f / PTM_RATIO);
polyTransform = new b2Transform();
polyTransform->Set(b2Vec2(50.0f / PTM_RATIO, 100.0f / PTM_RATIO), 0.0f);

b2Manifold manifold;
b2CollidePolygonAndCircle(&manifold, poly, *polyTransform, circle, *circleTransform);

//If you just want to see if they collide and don't care about the collision point then you can 
//just check if manifold.m_m_pointCount > 0
if(manifold.pointCount > 0)
{
	//Collision
	//If you want to get the points you probably want to convert the local manifold points to
	//a b2WorldManifold for world coordinates.

	b2WorldManifold worldManifold;
	worldManifold.Initialize(&manifold, *boxTransform_, box_->m_radius, *circleTransform_, circleRegion_->m_radius);
		
	//Then there should be two collided points for each contact from the other shape. Convert 
	//that to your pixel space ratio and you're done.
	Vec2 collisionPoint1 = Vec2(worldManifold.m_points[0].x * PTM_RATIO, (worldManifold.m_points[0].y * PTM_RATIO);
	Vec2 collisionPoint2 = Vec2(worldManifold.m_points[1].x * PTM_RATIO, (worldManifold.m_points[1].y * PTM_RATIO);
} else
	{
		//No collision
	}
1 Like

Thanks for that mate.

However, the b2 variables aren’t recognized by VS 2012. Am I doing something wrong?

#include "Box2D\Box2D.h"

If that doesn’t work, you must add the box2d project to your solution, but I don’t really know how to do that. I use Visual Studio 2015 and I can include box2d.h without doing anything, the solution already contains the box2d project.

1 Like

Maybe this would help.

Apparently that worked.

I used #include “Box2d.h”

Now time to test the code itself. Will tell you how it goes.

Okay, was about to enter the code, but I’m a bit of an Amateur.

You might have to dumb it down for me. Does all that code go into the cpp? Or the first 4 lines into the header?

And we are assuming one of the sprites is polygon and the other is called circle?

You can enter the first 4 lines in the header if you want to make them class members. For testing it is easier to copy all lines inside a function, even the init function should be ok. And yes, we assume that one is a circle and one is a polygon that we set as a box.

1 Like

Thank you for that. After entering the code, I’m being told that PTM_RATIO is undeclared.

Yeah you have to declare that. Box2d uses meters to measure distance, so you can’t really set a box or a circle depending on your sprite’s size, a 100px x 100px sprite would translate in a 100x100 meters box, so you need to have a ratio between pixels and meters. Usually a value of 32 is used. You can define it in your header file:

#define PTM_RATIO 32
1 Like

Ah, I see!

Thanks so much for the explanations and the help so far! I really appreciate it!

Okay, now that that has been handled, I’ve typed in all the code, and run it.

I don’t see anything happening, is that right?. Is there a way to bind my sprites to the body I just made? And apart from that, the part where you said I should check the manifold point count, can you elaborate on that part downwards?

Everything following the if statement, is it necessary, or just optional?

Yes, everything following the if statement is optional, that is only if you need to find the exact collision points. Moving and binding sprites to your box2d bodies must be done manually. You should move you sprites where you want, and while moving them, in a update function you should change your shapes’ transforms and redo the collision check.

1 Like

Thank you very much for everything Boby! I appreciate it all!

I’ve got one more thing to try and fix here. I wanna upload the collision code I was previously using to see if you can tell me what I was doing wrong.

I am modifying this post, because the code was jumbled when I posted it before. I’ll upload it in 2 pdf files this time. One is the header, the other is the cpp.

The error is at the end of the cpp file, in the implementation of the onCollision function. At least when I get the runtime error, there’s an arrow pointing at the enemy->setVisible(false) line.

Codecpp.pdf (206.0 KB)
Codeheader.pdf (108.0 KB)

Don’t worry, Ive actually seen what my mistake was!

I declared the variable type for the enemy variable in the cpp after having already declared it as a sprite in the header.