Android Question X2 Camera Object?

ilan

Expert
Licensed User
Longtime User
hi

i am giving x2 another chance (since the last try was a fail) but i am stuck on creating a camera object where the camera follows the main character.
the problem i am facing is that the Objects that i draw are shaking and not staying in place like they should.
why are they shaking if i update the word screen center?

please look at the video maybe i am doing something wrong.


everything that is drawn in the tilemap layers in tiled are draw ok without shaking but all object inside the object layer are shaking when the camera moves.

1725019984730.png
1725020007400.png


is there something i can do?
i looked at the character walking examples (and all other X2 examples) but i could not figure out what i am doing wrong.

any help will be appreciated.
thanks, ilan
 

ilan

Expert
Licensed User
Longtime User
It looks like you are updating the background center after the objects were already drawn. This is just a guess.
yes, i saw that in the x2 WalkingCharacter example

this is the code:

B4X:
Public Sub updateCamera
    Dim fuerzaX As Float = 0.06
    Dim fuerzaY As Float = 0.04              
    Dim posx As Float = x2.ScreenAABB.Center.X
    Dim posy As Float =  x2.ScreenAABB.Center.Y
    Dim difX, difY As Float
    difY = ((bw.Body.Position.Y+0.6-posy)*fuerzaY)
    If FaceRight = False Then
        difX = ((bw.Body.Position.x+0.5-posx)*fuerzaX)
    Else
        difX =((bw.Body.Position.x-0.5-posx)*fuerzaX)  
    End If
    If difX > 0.2 Then difX = 0.2
    If difX < -0.2 Then difX = -0.2      
    posx = posx + difX
    posy = posy + difY
    x2.UpdateWorldCenter(getVec(x2.ScreenAABB.Center.X,posy))
    'SET LIMITS HERE
    '...
End Sub

is it wrong? should i do it differently?

(i pm you the project link, if you would have time and would like to have a look at it i would really appreciate it)

thank you
 
Upvote 0

ilan

Expert
Licensed User
Longtime User
The question is whether the other elements are drawn before or after this call.
they were drawn before now i changed it to be drawn after and it looks much better.
all STATIC BODIES do not shake anymore BUT the dynamic bodies still shake.
i will make a video in 1 minute
 
Last edited:
Upvote 0

ilan

Expert
Licensed User
Longtime User
so this is the Tick event of the game module

B4X:
Public Sub Tick (GS As X2GameStep)
    updateCamera
    TileMap.DrawScreen(Array("background", "foreground"), GS.DrawingTasks)
    mKingHuman.Tick(GS)
    HandleTouches
End Sub

Public Sub updateCamera
    Dim fuerzaX As Float = 0.06
    Dim fuerzaY As Float = 0.04               
    Dim posx As Float = X2.ScreenAABB.Center.X
    Dim posy As Float =  X2.ScreenAABB.Center.Y
    Dim difX, difY As Float
    difY = ((mKingHuman.bw.Body.Position.Y+0.6-posy)*fuerzaY)
    If mKingHuman.FaceRight = False Then
        difX = ((mKingHuman.bw.Body.Position.x+0.5-posx)*fuerzaX)
    Else
        difX =((mKingHuman.bw.Body.Position.x-0.5-posx)*fuerzaX)   
    End If
    If difX > 0.2 Then difX = 0.2
    If difX < -0.2 Then difX = -0.2       
    posx = posx + difX
    posy = posy + difY
    X2.UpdateWorldCenter(getVec(X2.ScreenAABB.Center.X,posy))
    'SET LIMITS HERE
    '...
End Sub

the UpdateCamera is called first and it really fixed the shaking of the static bodies like windows, coins, ...
but the boxes (dynamic bodies) still shake.

 
Upvote 0

ilan

Expert
Licensed User
Longtime User
Test it in release mode. It will run in 60 fps and should be smoother.
ok thank you for your fast response.
i tried it in release mode.
for some reason on B4J i dont get more then 30 fps (also in release mode)
so i tried it on b4a using emulator. i do get 60 fps and it looks much better.
there is sometime a small shaking on the dynamic bodies but it is much better now.
i think i can live with that and continue the game :)

thank you very much for the support Erel ?

ps:
1. do you know why i get only 30 fps on b4j?
2. how do you like the game until now? :)
 
Upvote 0
Top