Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualJevi

Posted 27 January 2013 - 03:59 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);

#5Jevi

Posted 27 January 2013 - 03:54 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;
	}
	else
	{
		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);

#4Jevi

Posted 27 January 2013 - 03:54 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 do 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;
	}
	else
	{
		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);

#3Jevi

Posted 27 January 2013 - 03:51 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

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;
	}
	else
	{
		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);
	}
}

#2Jevi

Posted 27 January 2013 - 03:51 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 then render to the screen.

 

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

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;
	}
	else
	{
		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);
	}
}

#1Jevi

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 witch is what actually has the b2Body associated with it getting updated by the b2World and after its updated my render component takes the new coordinates from that body and updates to those new coordinates so that the results can be rendered to the screen.

 

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

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;
	}
	else
	{
		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);
	}
}

PARTNERS