Games Find bounding box of game shape

Andrew Gray

Member
Licensed User
Longtime User
I'm trying to find the (approximate) boundaries of a simple game object created from a tile map.

I tried the following but it just returns a single point:

B4X:
private Sub BodyAABB(b As B2Body) As B2AABB
   
    Dim tr As B2Transform
    tr.Initialize
    Dim AABB As B2AABB
    AABB.Initialize
    b.FirstFixture.Shape.ComputeAABB(AABB, tr)
    Return AABB
   
End Sub

What is wrong here? Is there really no simpler way of doing this?
 

Andrew Gray

Member
Licensed User
Longtime User
Actually I think I've just figured out the solution. The transformation needs to be set to 1x1:

tr.Translation = X2.CreateVec2(1,1):
tr.Translation = X2.CreateVec2(1,1)

Still amazes me there isn't a simpler way of finding the boundaries of a shape though.
 

ilan

Expert
Licensed User
Longtime User
Still amazes me there isn't a simpler way of finding the boundaries of a shape though.
it is because it is not a simple task to do. if you have a circle it is simple but if you have a rotating polygon ?!
so this is why box2d has the collision event where you can check if 2 bodies collide with each other.

you could get the center of the body and if you want it approximately than calculate a radius around this point and make your checks.
the real question is why do you need it? maybe there is another way to solve your issue so you should describe the situation in your game and why you need to get the boundaries.

tr.Translation = X2.CreateVec2(1,1)
hmmm... in "most" cases a physic body should be moved by adding forces to it. if you want to move it in a constant speed use velocity but translation???!!!! i would not use it at all but again i have no idea what you are doing.

if you could describe the type of game and what you want to archive we could help you with a better approach.
 

Andrew Gray

Member
Licensed User
Longtime User
Just realised I'm still getting this wrong. Ignore the comment above about 1x1 translation.

I want something to happen while a rectangular object is (at least partially) visible on screen. (It's a field of boiling lava in which I want to animate bubbles.) I am doing that by testing whether the object AABB intersects with the screen AABB.

The following, based on Erel's suggestion, works beautifully. Thanks as always Erel!

B4X:
private Sub BodyAABB(b As B2Body) As B2AABB
    
    Dim size As B2Vec2 = X2.GetShapeWidthAndHeight(b.FirstFixture.Shape)
    Dim AABB As B2AABB
    AABB.Initialize2(X2.CreateVec2(b.Position.X - size.X / 2, b.Position.Y - size.Y / 2), X2.CreateVec2(b.Position.X + size.X / 2, b.Position.Y + size.Y / 2))
    Return AABB
    
End Sub
 
Top