Games XUI2D : How to hide an object (Body) ?

TelKel81

Active Member
Licensed User
I tried X2BoddyWrapper.IsVisible = False but to no avail.

I checked most games in the example pack and I only see deletion of bodies.

I just need to make the body invisible/contactless while I animate an explosion over it.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is from the main loop (X2Utils - you can see it in the X2 Source Folder project):
B4X:
mGame.Tick(gs)
        Dim VisibleBodies As Map = mWorld.QueryAABBToMapOfBodies(ScreenAABB)
        Dim AllBodies As List = mWorld.AllBodies
        For Each body As B2Body In AllBodies
            Dim bw As X2BodyWrapper = body.Tag
            bw.IsVisible = VisibleBodies.ContainsKey(body)
            If bw.IsVisible Then
                bw.Tick(gs)
            Else
                If bw.TickIfInvisible Then
                    bw.Tick(gs)
                Else If bw.DestroyIfInvisible Then
                    bw.Delete(gs)
                End If
            End If
        Next
As you can see, IsVisible is set right before BodyWrapper.Tick is called.
Worth going over the code in X2BodyWrapper.Tick. The bottom line is that it will be drawn unless you have created a separate class for that body (this is demonstrated in some of the examples).

You can set X2BodyWrapper.GraphicName = "" to prevent the body from being drawn.
 

ilan

Expert
Licensed User
Longtime User
I tried X2BoddyWrapper.IsVisible = False but to no avail.

I checked most games in the example pack and I only see the deletion of bodies.

I just need to make the body invisible/contactless while I animate an explosion over it.

i think you misunderstood how box2d (that is part of x2) works.

you don't hide bodies, bodies are basically a shape in your box2dworld that is not visible. with very complex mathematical formulas box2d is generating physics behavior on this shape (that is called PhysicBody). so box2d is just a library that calculates the physics for you then you need to draw with any rendering library the image to that body. so you cannot hide a body you can destroy it or create it but what you can do is stop rendering the image that is connected to that body.

the " X2BoddyWrapper.IsVisible " property just checks if the body is still inside the screen area. let say you create a body in your game and you have set the gravity to (0,-9.8)
which means the body will fall down. once it is outside your screen it will keep falling until another body (static body) will stop it like a ground. so there is no need to keep this body in the world and calculate all forces on it and to prevent unseen bodies to stay in the world you can check if there are inside the screen area and if not destroy them.

what you are describing is a different scenario. you don't need to hide the body you can destroy it and just hold a reference to the last position of this body and draw your explosion to that position.

why do you want to keep the body after you draw an explosion? (what should basically mean that the character or that object is destroyed in the game)
 

TelKel81

Active Member
Licensed User
Thanks for the clarification.

I was trying to see If I could avoid spending CPU time again on CreateObject, MotorJoint, and setting other variables too (such as LinearDamping, MaxMotorForce...).

...Despite the very small CPU time involved.

Also : I wasn't sure if I had missed something because usually Visible = False gives a pretty straightfoward result I should have checked the X2Utils code as suggested by Erel.
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…