2
\$\begingroup\$

I am working on a game in Libgdx framework. I have to get width and height from a body in box2d. is their any way to get width an d height from a body in box2d??

\$\endgroup\$
1
\$\begingroup\$

Bodies are made up of one or more fixtures and fixtures define a shape. This means, for each fixture, you need to get the shape and its size, then combine those to get the total width and height. It looks like the easiest way is with getRadius(), but that doesn't give you much. You may want to get the shape type, then create methods for getting more detailed size information based on the shape type. Iterating through their vertices, to determine the extents.

Better yet, you can maintain your own lookup tables when the objects are created, to find their height and width. Have some kind of object manager that creates your bodies for you, when they're created they store a unique ID in either the user data of the body, or of the fixture. Or, if you're not going to use the user data for anything else, just store the size there.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Actually I was destroying an d recreating 5 bodies again and again after they reach one side of screen so that I can resize them. Like jumping and running where plateforms are kinematic bodies and player is dynamic body. problem is that body height is not same as the height i m giving to sprite. \$\endgroup\$ – MGDroid Mar 29 '14 at 7:39
1
\$\begingroup\$

I know that this is a very old thread but I just made my own way of getting the size of a Body here: https://github.com/Wasupmacuz/Box2DBodySize
Here's the code if links are too spooky for you:

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.ChainShape;
import com.badlogic.gdx.physics.box2d.CircleShape;
import com.badlogic.gdx.physics.box2d.EdgeShape;
import com.badlogic.gdx.physics.box2d.Fixture;
import com.badlogic.gdx.physics.box2d.PolygonShape;
import com.badlogic.gdx.physics.box2d.Shape;

/**
 * Finds the size (width and height) of a body by checking its fixtures' shapes
 */
public class Sizerinator3000
{
    public static Vector2 getBodySize(Body body)
    {
        float maxTop = 0, maxRight = 0, maxBottom = 0, maxLeft = 0;
        for(Fixture f : body.getFixtureList())
        {
            MyVector4 vec = outerVals(f);
            if(vec.a > maxTop)
                maxTop = vec.a;
            if(vec.b > maxRight)
                maxRight = vec.b;
            if(vec.c > maxBottom)
                maxBottom = vec.c;
            if(vec.d > maxLeft)
                maxLeft = vec.d;
        }
        
        return new Vector2(Math.abs(maxRight - maxLeft), Math.abs(maxTop - maxBottom));
    }
    
    public static Vector2 getBodySize(Body body, float scale)
    {
        float maxTop = 0, maxRight = 0, maxBottom = 0, maxLeft = 0;
        for(Fixture f : body.getFixtureList())
        {
            MyVector4 vec = outerVals(f).scale(scale);
            if(vec.a > maxTop)
                maxTop = vec.a;
            if(vec.b > maxRight)
                maxRight = vec.b;
            if(vec.c > maxBottom)
                maxBottom = vec.c;
            if(vec.d > maxLeft)
                maxLeft = vec.d;
        }
        
        return new Vector2(Math.abs(maxRight - maxLeft), Math.abs(maxTop - maxBottom));
    }

    /**
     * Changing which method is called based on the fixture's shape
     * @param fixture the current fixture being analyzed
     * @return the outermost points of the fixture's shape
     */
    private static MyVector4 outerVals(Fixture fixture)
    {
        Shape shape = fixture.getShape();
        Shape.Type type = shape.getType();
        MyVector4 size = new MyVector4();
        switch(type)
        {
        case Polygon: 
        size = shapeVals((PolygonShape)shape);
        break;
        case Chain: 
        size = shapeVals((ChainShape)shape);
        break;
        case Edge: 
        size = shapeVals((EdgeShape)shape);
        break;
        case Circle: 
        size = shapeVals((CircleShape)shape);
        break;
        }
        
        return size;
    }

    /**
     * Calculates the outermost points for a PolygonShape
     * @param s the shape
     * @return the maximum upper, lower, left, and right values; given by the vertices
     */
    private static MyVector4 shapeVals(PolygonShape s)
    {
        MyVector4 size = new MyVector4(); // top, right, bottom, left
        for(int i = 0; i < s.getVertexCount(); i++)
        {
            Vector2 probe = new Vector2();
            s.getVertex(i, probe);
            if(probe.x > size.b) // right
                size.b = probe.x;
            if(probe.x < size.d) // left
                size.d = probe.x;
            
            if(probe.y > size.a) // top
                size.a = probe.y;
            if(probe.y < size.c) // bottom
                size.c = probe.y;
        }
        return size;
    }

    /**
     * Calculates the outermost points for a ChainShape
     * @param s the shape
     * @return the maximum upper, lower, left, and right values; given by the vertices
     */
    private static MyVector4 shapeVals(ChainShape s)
    {
        MyVector4 size = new MyVector4(); // top, right, bottom, left
        for(int i = 0; i < s.getVertexCount(); i++)
        {
            Vector2 probe = new Vector2();
            s.getVertex(i, probe);
            if(probe.x > size.b) // right
                size.b = probe.x;
            if(probe.x < size.d) // left
                size.d = probe.x;
            
            if(probe.y > size.a) // top
                size.a = probe.y;
            if(probe.y < size.c) // bottom
                size.c = probe.y;
        }
        return size;
    }

    /**
     * Calculates the outermost points for an EdgeShape
     * @param s the shape
     * @return the maximum upper, lower, left, and right values; given by the vertices
     */
    private static MyVector4 shapeVals(EdgeShape s)
    {
        MyVector4 size = new MyVector4(); // top, right, bottom, left
        Vector2 probe = new Vector2();
        s.getVertex1(probe);
        if(probe.x > size.b) // right
            size.b = probe.x;
        if(probe.x < size.d) // left
            size.d = probe.x;
        
        if(probe.y > size.a) // top
            size.a = probe.y;
        if(probe.y < size.c) // bottom
            size.c = probe.y;

        s.getVertex2(probe);
        if(probe.x > size.b) // right
            size.b = probe.x;
        if(probe.x < size.d) // left
            size.d = probe.x;
        
        if(probe.y > size.a) // top
            size.a = probe.y;
        if(probe.y < size.c) // bottom
            size.c = probe.y;
        
        return size;
    }

    /**
     * Calculates the outermost points for a CircleShape
     * @param s the shape
     * @return the maximum upper, lower, left, and right values; given by radius and position
     */
    private static MyVector4 shapeVals(CircleShape s)
    {
        MyVector4 size = new MyVector4(); // top, right, bottom, left
        size.a = s.getPosition().y + s.getRadius(); // top
        size.b = s.getPosition().x + s.getRadius(); // right
        size.c = s.getPosition().y - s.getRadius(); // bottom
        size.d = s.getPosition().x - s.getRadius(); // left
        
        return size;
    }
    
    /**
     * using this just to store the variables easily
     */
    private static class MyVector4
    {
        public float a, b, c, d;
        
        public MyVector4()
        {
            this.a = 0;
            this.b = 0;
            this.c = 0;
            this.d = 0;
        }
        
        @SuppressWarnings("unused")
        public MyVector4(float a, float b, float c, float d)
        {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }
        
        @SuppressWarnings("unused")
        public void set(float a, float b, float c, float d)
        {
            this.a = a;
            this.b = b;
            this.c = c;
            this.d = d;
        }
        
        public MyVector4 scale(float val)
        {
            this.a *= val;
            this.b *= val;
            this.c *= val;
            this.d *= val;
            
            return this;
        }
    }
}
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

You can use this:

((Sprite)body.getUserData()).getHeight();

it should answer your question if I guessed your problem correctly.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.