Games Xui2D - image > body different size

ilan

Expert
Licensed User
Longtime User
hi

i have started a simple game with xui2d. and the questions list is growing :)

1. when i used libgdx and box2d i could define a polyline body. the adventage is that i can also create concave body shape (and not only convex) although in tiled i use polygone but in libgdx i have defined it as polyline body. anyway i used instead several bodies in xui2d to create my character but now question 2 is coming.

2. if i have a body and i would like to attach an image to it how do i do it if the body and the image are not in same size. like i have a circle but defined as the player body and i would like to put the whole character so the complete image of the player including the head. i am using for the head another body so both bodies are attached to each other but need to use the same sprite.

thanx
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
SS-2019-04-25_14.37.36.png


One way to do it:

- Create an object layer that will only be used to add images. Add the image with the Insert Tile option and resize it however you like. Lock this layer when done.
Note that this layer is not used at runtime. It only helps you to correct set the shapes.

- Add the various shapes over the image:

SS-2019-04-25_14.38.46.png


- Add the full body rectangle shape. This is the only body with its graphic file property set.
- Set the full body mask bits to 0.
- Set the full body density to 0.01.

- Create wedge joints between the parts and the full body.

A B4J example is attached.
This way is good for building physical bodies made of several shapes.
 

Attachments

  • Test.zip
    28.6 KB · Views: 404

ilan

Expert
Licensed User
Longtime User
thanx erel, acctually this is how i solved it i only set the big body as SENSOR and used it for the image but it seems to me not to be the propper way to do it.
i really like a lot the way iSpritekit works or box2d a libgdx. anyway i will need to get used to the logic of xui2d.

how about drawing an image somewhere in the canvas without using a physic body? can you help me to understand how it does?
is it ok if i keep asking quetion in the same thread or should i open for each question a new thread?
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
It is better to start a new thread for each question, otherwise it will be difficult to learn from these discussions in the future.

how about drawing an image somewhere in the canvas without using a physic body?
Step 1:
Add the bitmap to the graphic cache when the game starts
B4X:
X2.GraphicCache.PutGraphic2("mystery", Array(X2.LoadBmp(File.DirAssets, "mystery.png", 1, 0.5, True)), True, 2)

Step 2:
B4X:
Public Sub Tick (GS As X2GameStep)
   Dim position As B2Vec2 = X2.WorldPointToMainBC(3, 3)
   GS.DrawingTasks.Add(X2.GraphicCache.GetDrawTask("mystery", 0, X2.B2AngleToDegrees(45), False, False, position.X, position.y))
End Sub
SS-2019-04-26_08.55.48.png


In most cases it is better to create a static body with X2.CreateBodyWrapper. It will not hurt performance and it will give you many features that you will need to implement yourself.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Example of creating a body just as a stub for an image:
B4X:
X2.GraphicCache.PutGraphic2("mystery", Array(X2.LoadBmp(File.DirAssets, "mystery.png", 1, 0.5, True)), True, 2) '<-- only needed once
Dim bd As B2BodyDef
bd.BodyType = bd.TYPE_STATIC
bd.Position = X2.CreateVec2(3, 3)
Dim Mystery  As X2BodyWrapper = X2.CreateBodyAndWrapper(bd, Null, "mystery")
Mystery.GraphicName = "mystery"
Dim shape As B2CircleShape
shape.Initialize(0.1) 'size doesn't matter. The shape is needed as only "visible" bodies are drawn.
Mystery.Body.CreateFixture2(shape, 1).SetFilterBits(0, 0)
 
Last edited:
Top