Games Track the position of a body

Discussion in 'Game Development' started by ElliotHC, Apr 9, 2019.

  1. ElliotHC

    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
     
    fredo likes this.
  2. Erel

    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:
    Code:
    Dim worldpoint As B2Vec2 = X2.ScreenPointToWorld(X, Y)
    For Each TouchedBody As X2BodyWrapper In X2.GetBodiesIntersectingWithWorldPoint(worldpoint)
     
  3. ElliotHC

    ElliotHC Active Member Licensed User

    Is there a way to debug the position of the bodies as they travel down the screen?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    Sure. You can find the position of all bodies.

    Add this code to the Game Tick sub:
    Code:
    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
     
    wyatt420 and ElliotHC like this.
  5. ElliotHC

    ElliotHC Active Member Licensed User

    Thanks Erel, I'll give it a try.
     
  6. ElliotHC

    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: Apr 12, 2019
  7. Erel

    Erel Administrator Staff Member Licensed User

    I don't remember the id of the power bodies but you can do something like:
    Code:
    For Each bw As X2BodyWrapper In X2.GetBodiesWrappersByIds(Array(IdOfPowerBodyThatYouShouldFindInTiled))
     
    Log(bw.Body.Position.Y)
    Next
     
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