• Advertisement
Sign in to follow this  

Box2D Incorrect Collision Calculation

This topic is 1816 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

So i decided to add Box2D functionality to my little game engine as a component which can be added to an entity. As of right now the physics component, which is what actually has the b2Body associated with it and is getting updated by the b2World, is also updating the render component for the entity which is the results from the physics component are render to the screen (via the render component)

 

I am new to Box2D and it looks like I have everything correct but the collisions aren't being calculated correctly.

 

Here is the code which initializes and updates the render component from the physics component update results:

 

 

Here is a video of the collision sort of working: https://www.dropbox.com/s/ykm0camzs287jdu/Engine%202013-01-27%2015-46-41-63.avi

 

And here it is colliding on the other side and not working at all: https://www.dropbox.com/s/6dcqu6d3eofga8h/Engine%202013-01-27%2015-41-07-74.avi

 

Am I not converting/initializing something?

void PhysicsComponent::Update(unsigned long dt)
{
	if (!synched)
	{
		b2World* world = Engine::GetWorld();
		b2BodyDef BodyDef;
		BodyDef.position.Set(entity->transform.p.x, entity->transform.p.y);
		BodyDef.angle = entity->transform.q.GetAngle();

		switch (bodyType)
		{
			case 0:
				BodyDef.type = b2_staticBody;
				break;
			case 1:
				BodyDef.type = b2_dynamicBody;
			default:
				BodyDef.type = b2_dynamicBody;
				break;
		}
		body = world->CreateBody(&BodyDef);

		b2PolygonShape shape;
		int width = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->width;
		int height = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->height;
		float mwidth = EngineMath::PixelsToMeters((float) width) / 2.0f;
		float mheight = EngineMath::PixelsToMeters((float) height) / 2.0f;
		Debug::Log(Debug::LOG_DEBUG, "width: %f height: %f density: %f", mwidth, mheight, density);
		Debug::Log(Debug::LOG_DEBUG, "width: %i height: %i density: %f", width, height, density);
		shape.SetAsBox(mwidth, mheight);

		b2FixtureDef FixtureDef;
		FixtureDef.shape = &shape;
		FixtureDef.density = density;
		FixtureDef.friction = 0.5f;
		body->CreateFixture(&FixtureDef);
		synched = true;
	}
		entity->transform = body->GetTransform();
		// Debug collision outline
		int width = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->width;
		int height = ((RenderComponent*) entity->GetComponent("render"))->GetSprite()->height;
		Graphics::DrawQuad(entity->transform.p.x, entity->transform.p.y, (float) width, (float) height, entity->transform.q.GetAngle(), 255, 0, 0, 255);
		//Graphics::DrawQuad(body->GetTransform().p.x, body->GetTransform().p.y, (float) width, (float)height, body->GetTransform().q.GetAngle(), 0, 255, 0, 255);
}

 

world->Step(1.0f / 60.0f, 5, 5);
Edited by Jevi

Share this post


Link to post
Share on other sites
Advertisement

I figured it out. Make sure you take into consideration where the point of origin is in a b2Body which is the center point of the b2Shape. I was not translating my images to the center and that's why they looked off.

Share this post


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

  • Advertisement