Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9014457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T03:30:42+00:00 2026-06-16T03:30:42+00:00

So I have Body body that is a rectangle. How do I get its

  • 0

So I have Body body that is a rectangle. How do I get its half width and half height?(I could not find an answer anywhere else)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T03:30:43+00:00Added an answer on June 16, 2026 at 3:30 am

    Unfortunately, it’s not perfectly straight forward as Box2D (and thus JBox2D) does not have any concept of rectangles per se. The rectangle is a PolygonShape, whose shape was probably specified using setAsBox(halfWidth, halfHeight).

    To get that halfWidth and halfHeight after creating a Fixture, consider the following (please refactor it as needed):

    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
        }
    }
    

    To get this information from a Body use:

    public void checkOutTheseFixtures(Body body) {
        for (Fixture fixture = body.getFixtureList(); fixture != null; fixture = fixture.getNext()) {
            checkOutThisFixture(fixture);
        }
    }
    

    And a few tests:

    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);
    

    Prints:

    The polygon has half width & height of: 1.0 & 2.5

    The polygon has half width & height of: 137.0 & 42.0

    The circle has a radius of : 20.0

    Hope that helps.


    On a similar note, here’s a terse way to get the dimensions for a PolygonShape:

      public static Vec2 getDimensions( final PolygonShape shape ) {
        float minX = Float.MAX_VALUE;
        float maxX = Float.MIN_VALUE;
        float minY = Float.MAX_VALUE;
        float maxY = Float.MIN_VALUE;
        
        final int vertices = shape.getVertexCount();
        
        for( int i = 0; i < vertices; i++ ) {
          final Vec2 v = shape.getVertex( i );
          
          minX = (v.x < minX) ? v.x : minX;
          maxX = (v.x > maxX) ? v.x : maxX;
          minY = (v.y < minY) ? v.y : minY;
          maxY = (v.y > maxY) ? v.y : maxY;
        }
            
        return new Vec2( maxX - minX, maxY - minY );
      }
    

    Dividing the returned Vec2‘s dimensions in half will also retrieve the desired value.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have a div in my body that's a percentage width, and inside
I know that an interface does not have a body, just a method definition.
On my website I have a main body that has a height of 100%,
I'm writing a webmail product and some emails have body css that changes the
I have a table body that grabs it's rows (and form fields) via AJAX
I have a background on a page body that is an image which I
I have 1.5 million records, each with a text field body that contains a
I have data-segment attribute in my body tag that is changed by a slider.
I have a java script function that i have called on body load mousemove()
I have a page that looks like this: Head and body tags... <form runat=server>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.