所以我的身體是一個矩形。我如何獲得其半寬和半高(我找不到答案其他地方)如何從jbox2d中獲取身體的半寬和半身
1
A
回答
0
不幸的是,它不是Box2D(因此JBox2D)本身沒有任何矩形概念。該矩形是PolygonShape
,其形狀可能是使用setAsBox(halfWidth, halfHeight)
指定的。
無論如何,在創建Fixture
之後,我們如何得到halfWidth
和halfHeight
?
請不要複製&粘貼此代碼;按照您的應用程序需要重構它。
public void checkOutThisFixture(Fixture fixture) {
Shape fixtureShape = fixture.getShape();
if (fixtureShape instanceof PolygonShape) {
PolygonShape polygonShape = (PolygonShape) fixtureShape;
Float minX = null;
Float maxX = null;
Float minY = null;
Float maxY = null;
for (int i = 0; i < polygonShape.getVertexCount(); i++) {
Vec2 nextVertex = polygonShape.getVertex(i);
float x = nextVertex.x;
float y = nextVertex.y;
if (minX == null || x < minX) {
minX = x;
}
if (maxX == null || x > maxX) {
maxX = x;
}
if (minY == null || y < minY) {
minY = y;
}
if (maxY == null || y > maxY) {
maxY = y;
}
}
float width = maxX - minX;
float height = maxY - minY;
float halfWidth = width/2;
float halfHeight = height/2;
System.out.println("The polygon has half width & height of: " + halfWidth + " & " + halfHeight);
} else if (fixtureShape instanceof CircleShape) {
float radius = ((CircleShape) fixtureShape).m_radius;
System.out.println("The circle has a radius of : " + radius);
} else {
// TODO handle other shapes
}
}
而且,你的問題專門詢問從Body
得到這個信息。在這裏你去:
public void checkOutTheseFixtures(Body body) {
for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
checkOutThisFixture(fixture);
}
}
和幾個測試:
World world = new World(new Vec2(0, 0), true);
Body body = world.createBody(new BodyDef());
// Add a circle
CircleShape circle = new CircleShape();
circle.m_radius = 20;
body.createFixture(circle, 5);
// Add a box
PolygonShape rectangle = new PolygonShape();
rectangle.setAsBox(137, 42);
body.createFixture(rectangle, 10);
// Add a more complex polygon
PolygonShape polygon = new PolygonShape();
Vec2[] vertices = new Vec2[5];
vertices[0] = new Vec2(-1, 2);
vertices[1] = new Vec2(-1, 0);
vertices[2] = new Vec2(0, -3);
vertices[3] = new Vec2(1, 0);
vertices[4] = new Vec2(1, 1);
polygon.set(vertices, 5);
body.createFixture(polygon, 10);
checkOutTheseFixtures(body);
打印:
多邊形具有半寬&高度:1.0 & 2.5
多邊形有半身寬度&身高:137.0 & 42.0
圓的面積半徑:20.0
希望有所幫助。
0
相關問題
- 1. 我如何獲得JBox2d身體尺寸
- 2. 身高一半fill_parent xml
- 3. 較低身材的身高和體寬?
- 4. 正確的scaleFactor,minNeighbor和使用hacascascade檢測上半身和下半身的尺寸
- 5. 摧毀身體碰撞JBox2D android
- 6. 獲取網頁的身體或窗口的高度和寬度
- 7. 從web_custom_request的身體獲取ID
- 8. 如何用selenium和python獲取網站的身寬?
- 9. 如何使用Django的靜態文件緩存半身像?
- 10. 如何旋轉第一人稱角色的上半身(統一)
- 11. 閃亮的renderText:半斜體,一半不?
- 12. 如何使用Cobol複製書從EBCDIC中讀取半寬和全角字符
- 13. jquery身體的寬度和高度
- 14. 如何從3點和半徑找到球體中心?
- 15. C#HTTP POST和獲取身體
- 16. 獲取響應頭/身體
- 17. 如何將半徑添加到jboss ldap身份驗證?
- 18. 一個矢量的上半部和下半部的媒體
- 19. 定義取決於本身就是一個半羣實例
- 20. 從一個字節獲取半字節
- 21. 從半動態JSON文件獲取值
- 22. 如何從身體獲取特定的數據值?
- 23. 獲取半徑大小
- 24. ob_get_contents() - 從開放的身體標記獲取數據以關閉身體標記
- 25. 身體背景和身高
- 26. 身體寬度100%問題
- 27. 如何從使用jQuery的元素獲取邊框半徑?
- 28. 如何在SaveChanges中獲取實體框架中的身份值?
- 29. SilverStripe獲取圖像寬度/高度的一半
- 30. 使用Javascript獲取圖像寬度的一半