tristan1333

Box2D collisions

Recommended Posts

I'm really new to using Box2D. I've setup a small thing with a Dynamic body that falls onto a static one. When the box hits the body, it begins "vibrating" for a bit and shaking, then suddenly stops. If anyone has used it or might know what is happening I'd appreciate it, 

That's the setup of the boxes.

	b2BodyDef groundBodyDef;
	groundBodyDef.position.Set(100.0f, 400.0f);
	
	b2Body* groundBody = world.CreateBody(&groundBodyDef);
	
	b2PolygonShape groundBox;
	groundBox.SetAsBox(240, 20);
	
	groundBody->CreateFixture(&groundBox, 0.0f);
	
	b2BodyDef bodyDef;
	bodyDef.type = b2_dynamicBody;
	bodyDef.position.Set(100.0f, 0.0f);
	b2Body* body = world.CreateBody(&bodyDef);
	
	b2PolygonShape dynamicBox;
	dynamicBox.SetAsBox(32.0f, 32.0f);
	
	b2FixtureDef fixtureDef;
	fixtureDef.shape = &dynamicBox;
	fixtureDef.density = 1.0f;
	fixtureDef.friction = 0.3f;
	body->CreateFixture(&fixtureDef);
	body->SetAngularVelocity(20);
	int32 velocityIterations = 8;
	int32 positionIterations = 4;
		world.Step(ticks_per_frame, velocityIterations, positionIterations);
		b2Vec2 position = body->GetPosition();
		
		float32 angle  = body->GetAngle();
		std::cout << angle<< "\n";

		SDL_Rect rect = {position.x, position.y, 32, 32};
		//SDL_Point point = {32, 0};
		SDL_RenderCopyEx(mainren, textures[0], NULL, &rect, angle, NULL, SDL_FLIP_NONE);

 

Share this post


Link to post
Share on other sites

My first guess is that it's due to you using a rather large scale for your objects.

Box2d operates at a real-world scale of meters, so when you do SetAsBox(240, 20) then is actually 240meters x 20meters. Which is really rather large! That's like dropping a building.

What you should be doing is keeping the physics simulation on a more human scale (meters) and have a conversion (e.g. meters-to-pixels) used for rendering.

Edited by dmatter

Share this post


Link to post
Share on other sites

That's a very, very good point, I changed it to 20x10, and the other to 2x2, alas, still the same result

Edit: However rather than tripping out, it slides to the left a bit on landing and then freezes

Edited by tristan1333

Share this post


Link to post
Share on other sites

You do have an angular velocity set on your dynamic body which might cause it to want to slide or bounce on contact.

It shouldn't bounce because (I think) restitution is set to zero by default. But it might slide. Your static body is using the default friction value (0.2 I think) and your dynamic body's is set to 0.3. So the possibility of sliding is there.

So you might try removing the angular velocity or cranking up the friction of your bodies so that it comes to rest sooner.

Edited by dmatter

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