问题
I have created a body and I have created two separate fixtures, one fixture creates a rectangle shape and the other fixture creates a circle shape. But when I use the .createfixture it puts the circle in the centre of the rectangle, I want the circle on top of the rectangle like a matchstick.
here is my code don't know what to do ...
rectangleBodyDef = new BodyDef();
rectangleBodyDef.type = BodyType.DynamicBody;
rectangleBodyDef.position.set(10,20);
rectangleBody = world.createBody(rectangleBodyDef);
rectangleBodyShape = new PolygonShape();
rectangleBodyShape.setAsBox(2f, 0.75f);
rectangleBodyFixtureDef = new FixtureDef();
rectangleBodyFixtureDef.shape = rectangleBodyShape;
rectangleBodyFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(rectangleBodyFixtureDef);
/**********************CREATING THE SECOND BODY (CIRCLE BODY) ************/
circleShape = new CircleShape();
circleShape.setRadius(0.75f);
circleFixtureDef = new FixtureDef();
circleFixtureDef.shape = circleShape;
circleFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(circleFixtureDef);
回答1:
Fixture Definitions are relative to the body position, set the CircleShape position to be a little above the rectangle one:
CircleShape circleShape = new CircleShape();
circleShape.setRadius(0.75f);
circleShape.setPosition(new Vector2(0,2)); //<------Add this
FixtureDef circleFixtureDef = new FixtureDef();
circleFixtureDef.shape = circleShape;
circleFixtureDef.restitution = 0.8f;
rectangleBody.createFixture(circleFixtureDef);
回答2:
The location of the circleFixtureDef is relative to the location of your rectangleBody. Set the location of circleFixtureDef to something other than 0,0.
来源:https://stackoverflow.com/questions/21139801/in-libgdx-how-do-you-attach-a-circleshape-on-top-of-a-rectangle