In libgdx how do you attach a circleshape on top of a rectangle?

末鹿安然 提交于 2019-12-04 02:35:33

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!