Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Box2D Incorrect Collision Calculation


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 Jevi   Members   -  Reputation: 127

Like
0Likes
Like

Posted 27 January 2013 - 03:48 PM

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, 27 January 2013 - 03:59 PM.


Sponsor:

#2 Jevi   Members   -  Reputation: 127

Like
1Likes
Like

Posted 27 January 2013 - 07:23 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS