No announcement yet.

Box2D: unexpected lengths

  • Filter
  • Time
  • Show
Clear All
new posts

  • Box2D: unexpected lengths


    im experimenting this with the WaveEngine physics system. As you can see on the image, I have two CircleCollider2D with radius=1. The base they are laid on is 20 units long. I expect then to be enough space for (20/(2*2)) = 4 circles, not just two. As a reference, I put another 4 units long EdgeCollider2D.

    picture upload

    As you can the, it seems that the reference system is different from CircleCollider2D and EdgeCollider2D.

    How is that possibile?


  • #2
    Hi NiG !

    Are you taking into account the size of the sprite? if the sprite is 3 pixels, radious 1 means 3. :-)

    Was this any helpful?


    • #3
      Hello Mgomar

      well, actually I'm drawing with the PhysicsManager.Simulation2D.DebugDraw, so I expect that what I see is what there's inside the Box2D model. There is no sprite involved apart from the red and white point you can see in the screenshot. The entities perimeters are drawn by the DebugDraw, and they seem not coherent.

      thank you very much


      • #4
        Originally posted by mgomar View Post
        Hi NiG !

        Are you taking into account the size of the sprite? if the sprite is 3 pixels, radious 1 means 3. :-)

        Was this any helpful?
        ok, I figured that's the issue.
        Well, I'd like to ask why you joined the texture size with the box2d reppresentation. IMHO this leads to unwanted behaviors. So every time I change the texture, my box2d size changes also? What to do when I need the box2d size to be fixed?

        Is there a way to disable this relation?



        • #5
          hi NiG !

          I know it is a little tricky that the size is related with the size of the sprite, the only way you can easily manage this situation is to use sprites of 1 pixel and them uses color tint and opacity to change color.

          And this is the default scenario for wave.

          But this is not a silver bullet, because if you want circles there is no easy way you can do a circle with just one pixel.

          If you want them to be independent, you have to separeta then in two entites.

          One have the collider, you set the scale to Vector2.One and then manage the rectangle in the Transform2D (yo have to set the size explicitly) as you like and now add a child with the desired texture.

          By doing this you have them separated (note that parent relations Scale is Affected, so thats parent has to have Scale Vector.One)

          var parent = new Entity()
                              .AddComponent(new Transform2D()
                                      Scale = Vector2.One, // Scale affects children
                                      Rectangle = new RectangleF(0,0,20,20), // Size of the collider
                          .AddComponent(new CircleCollider2D());
                      var sprite = new Entity()
                              .AddComponent(new Transform2D())
                              .AddComponent(new Sprite(texturePath))
                              .AddComponent(new SpriteRenderer());


          • #6

            yeah this sounds as a nice solution. Until now, I resolved it by using some tricky math when assigning the scale, but this method is far more simple. I'll try it.