Games Track the position of a body

ElliotHC

Active Member
Licensed User
Having some real fun learning B4j.

In the Space Shooter example it creates a body. There can be multiple bodies on the screen at any one time.

I want to look at a section of the screen and identify if there is a body in that section/square. Anyone know how I might do that?

Thanks
 

Erel

Administrator
Staff member
Licensed User
This question is not specific to B4J. It is a XUI2D question. I've moved it to the games forum.

Check Ilan's First App Example from the examples pack.

There is a panel named PanelForTouch. When the user touches it, there is a check for nearby switches. If none are found then a new ball is created.
The relevant sub is PanelForTouch_Touch and the important code is:
B4X:
Dim worldpoint As B2Vec2 = X2.ScreenPointToWorld(X, Y)
For Each TouchedBody As X2BodyWrapper In X2.GetBodiesIntersectingWithWorldPoint(worldpoint)
 

ElliotHC

Active Member
Licensed User
Is there a way to debug the position of the bodies as they travel down the screen?
 

Erel

Administrator
Staff member
Licensed User
Sure. You can find the position of all bodies.

Add this code to the Game Tick sub:
B4X:
Dim bodies As List = world.AllBodies
For Each body As B2Body In bodies
   Dim bw As X2BodyWrapper = body.Tag
   If bw.Name <> "" Then
       Log(bw.Name & ": " & bw.Body.Position)
   End If
Next
 

ElliotHC

Active Member
Licensed User
I need to now the 'X' position of the lowest Power body. Do I need to post this as a separate question?

I have noticed that if there are more than 1 'power' on the screen it seems to only report the position of the newest one. I'm getting position data until the next one is generated, then I start getting position info for that one. What I am trying to do is get the X position of all the powers when they are withing a certain distance from the bottom edge of the screen.

Am I going about this the right way?

Thanks
 
Last edited:

Erel

Administrator
Staff member
Licensed User
I don't remember the id of the power bodies but you can do something like:
B4X:
For Each bw As X2BodyWrapper In X2.GetBodiesWrappersByIds(Array(IdOfPowerBodyThatYouShouldFindInTiled))
 Log(bw.Body.Position.Y)
Next
 
Top