Android Question Libgdx - Box2D , how to integrate pictures?

FJS

Active Member
Licensed User
Longtime User
Hello,

I work with box2D objets and I wold like to know how integrate a picture with a objet.
In fact I would like to integrate a picture in a poligone (5,6,7,8 o 9 sides). In others words, I have a piture and I would like to delimite it with a poligone wich is an objet, in this way I could fill the poligone with a picture (and with two pictures is posible, one is draw and another will be a shadow??, for example)
Please, could you give us any clue to try to do it?

Thank you very much
 

walterf25

Expert
Licensed User
Longtime User
Hello,

I work with box2D objets and I wold like to know how integrate a picture with a objet.
In fact I would like to integrate a picture in a poligone (5,6,7,8 o 9 sides). In others words, I have a piture and I would like to delimite it with a poligone wich is an objet, in this way I could fill the poligone with a picture (and with two pictures is posible, one is draw and another will be a shadow??, for example)
Please, could you give us any clue to try to do it?

Thank you very much
There is a few examples in the tutorials, if you look careful you'll find it.
Walter
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
ok, thank you, but is possible to integrate a picture in a objet without the white border??
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
ok, thank you, but is possible to integrate a picture in a objet without the white border??
You don't integrate anything. You display an image at the position of the Box2D object, with the same size and orientation. That's all. Box2D doesn't display anything, except if you use the debug renderer (but it's just to debug, it should not be used in your app).
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Please, could you tell us an example to display an image at the position of the box2d object?

I dont know if use several screen (for the future game), scene2d, or how to do it, is there any example in the tutorial? any simple?. In my app I use "LG_Render" and "LG_Update" because I use box2D, so How to use it to integrate pictures??

I would like to create a simple game, but with the idea to increase the capabilities of the game, picture... please, could you tell us any recomendation?

Thank you as always
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Please, could you tell us an example to display an image at the position of the box2d object?

I dont know if use several screen (for the future game), scene2d, or how to do it, is there any example in the tutorial? any simple?. In my app I use "LG_Render" and "LG_Update" because I use box2D, so How to use it to integrate pictures??

I would like to create a simple game, but with the idea to increase the capabilities of the game, picture... please, could you tell us any recomendation?

Thank you as always
Box2D_PhysicsBodyEditorLoader and Map_Box2D are 2 examples of displaying sprites with the position and orientation of a Box2d object.
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Great, thank you the first example, I do it,
B4X:
    Batch.ProjectionMatrix = Camera.Combined
    Batch.Begin
        spaceshipsprite.Draw(Batch)
        spaceshipsprite.X=spaceship.Position.x -spaceshipsprite.Width*0.5
        spaceshipsprite.y=spaceship.Position.y -spaceshipsprite.Height*0.5
Batch.End

But I have two diference questions:

1-it is posible to cut a picture (.png, or jpg) from a texture, with a polygon (lgBox2DPolygonShape)?, I generate many objets (lgBox2DBody) thanks to polygones and I would like cut a picture just with the same polygon.

2-When a objet is moving quicky the objet and the sprite dont have the same position, it seems the position has a gap between the position of the objet and the position of the picture ( I proved with a simple ball), I used the previus code, inside "LG_Render". Please, could you tell us how to solve this gap?


Thank you very much for the help, I hope it is useful for the forum ;)
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Great, thank you the first example, I do it,
B4X:
    Batch.ProjectionMatrix = Camera.Combined
    Batch.Begin
        spaceshipsprite.Draw(Batch)
        spaceshipsprite.X=spaceship.Position.x -spaceshipsprite.Width*0.5
        spaceshipsprite.y=spaceship.Position.y -spaceshipsprite.Height*0.5
Batch.End
You can modify the origin of a sprite to get rid of this kind of computation.

1-it is posible to cut a picture (.png, or jpg) from a texture, with a polygon (lgBox2DPolygonShape)?
Look at PolygonSpriteBatch in the documentation.

2-When a objet is moving quicky the objet and the sprite dont have the same position, it seems the position has a gap between the position of the objet and the position of the picture ( I proved with a simple ball), I used the previus code, inside "LG_Render". Please, could you tell us how to solve this gap?
I don't understand what you mean. Your problem comes probably from your code.
 
Upvote 0

FJS

Active Member
Licensed User
Longtime User
Sorry, could you give us a example for cut a texture with a polygon? ( I cant do it by means polygonspritebatch dobumentation)

I generate a lgbox2dpolygonshape, and I would like to generate a "polygonal region" with the exact polygon. And then draw it

Thank you very much for the help



B4X:
Dim nvert As Int
                    nvert=Rnd(6,11)
           
                    Dim poligones(nvert) As lgMathVector2
                        For n=0 To (nvert-1)
                        ndeform=Rnd(70,131)*0.01   
                        poligones(n).set(nsize*Cos(6.2832/nvert*n)*ndeform,nsize*Sin(6.2832/nvert*n)*ndeform)
                        Next
                    polygon.Set(poligones)

                    Dim triangleShapeDef As lgBox2DFixtureDef
                    triangleShapeDef.shape = polygon
                    triangleShapeDef.restitution=1.0
                    triangleShapeDef.Density = 1.0

                    Dim triangleBodyDef As lgBox2DBodyDef
                    triangleBodyDef.Type = World.BODYTYPE_Dynamic
                    triangleBodyDef.position.set(a0+3.0, b0*0.5+4.0-nposition)

                            meteorite = World.createBody(triangleBodyDef)
                            meteorite(i).createFixture(triangleShapeDef)                   
                               
                            meteorite.UserData=("meteorite" & i)
                            Dim V1 As lgMathVector2
                            meteorite(i).LinearVelocity=V1.set(-1*nspeed*Cos(nangle),nspeed*Sin(nangle))

                           
                            meteorite.AngularVelocity=nangular
 
Upvote 0

Informatix

Expert
Licensed User
Longtime User
Sorry, could you give us a example for cut a texture with a polygon? ( I cant do it by means polygonspritebatch dobumentation)

I generate a lgbox2dpolygonshape, and I would like to generate a "polygonal region" with the exact polygon. And then draw it

Thank you very much for the help



B4X:
Dim nvert As Int
                    nvert=Rnd(6,11)
        
                    Dim poligones(nvert) As lgMathVector2
                        For n=0 To (nvert-1)
                        ndeform=Rnd(70,131)*0.01
                        poligones(n).set(nsize*Cos(6.2832/nvert*n)*ndeform,nsize*Sin(6.2832/nvert*n)*ndeform)
                        Next
                    polygon.Set(poligones)

                    Dim triangleShapeDef As lgBox2DFixtureDef
                    triangleShapeDef.shape = polygon
                    triangleShapeDef.restitution=1.0
                    triangleShapeDef.Density = 1.0

                    Dim triangleBodyDef As lgBox2DBodyDef
                    triangleBodyDef.Type = World.BODYTYPE_Dynamic
                    triangleBodyDef.position.set(a0+3.0, b0*0.5+4.0-nposition)

                            meteorite = World.createBody(triangleBodyDef)
                            meteorite(i).createFixture(triangleShapeDef)                
                            
                            meteorite.UserData=("meteorite" & i)
                            Dim V1 As lgMathVector2
                            meteorite(i).LinearVelocity=V1.set(-1*nspeed*Cos(nangle),nspeed*Sin(nangle))

                        
                            meteorite.AngularVelocity=nangular
To display only a part of a texture, you can:
- use a texture region object but it's only rectangular;
- create your own mesh but I don't think you're ready for that;
- use a polygon region (lgPolygonRegion). The documentation for PolygonSpriteBatch mentions the tool to use, the example to look at... The hardest part is to create the Box2D object from the created region. You can create the Box2D shape from the region vertices, or you can recreate the polygon with the Physics Body Editor tool.
Are you sure that you need to "cut" your texture? Is it not possible to create the texture with the wanted shape in your favourite paint program?
 
Upvote 0
Top