Games Xui2D - image > body different size

Discussion in 'Game Development' started by ilan, Apr 25, 2019.

  1. ilan

    ilan Expert Licensed 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
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    [​IMG]

    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:

    [​IMG]

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

    Attached Files:

    Johan Hormaza and ilan like this.
  3. ilan

    ilan Expert Licensed 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?
     
  4. Erel

    Erel Administrator Staff Member Licensed 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.

    Step 1:
    Add the bitmap to the graphic cache when the game starts
    Code:
    X2.GraphicCache.PutGraphic2("mystery"Array(X2.LoadBmp(File.DirAssets, "mystery.png"10.5True)), True2)
    Step 2:
    Code:
    Public Sub Tick (GS As X2GameStep)
       
    Dim position As B2Vec2 = X2.WorldPointToMainBC(33)
       GS.DrawingTasks.Add(X2.GraphicCache.GetDrawTask(
    "mystery"0, X2.B2AngleToDegrees(45), FalseFalse, position.X, position.y))
    End Sub
    [​IMG]

    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.
     
    ilan likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    Example of creating a body just as a stub for an image:
    Code:
    X2.GraphicCache.PutGraphic2("mystery"Array(X2.LoadBmp(File.DirAssets, "mystery.png"10.5True)), True2'<-- only needed once
    Dim bd As B2BodyDef
    bd.BodyType = bd.TYPE_STATIC
    bd.Position = X2.CreateVec2(
    33)
    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(00)
     
    Last edited: Apr 26, 2019
    inakigarm and Johan Hormaza like this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice