Games (WISH) Camera Object

ilan

Expert
Licensed User
Longtime User
Hi

in almost any game engine you have a camera object like in libgdx, spritekit... this object allows you to follow another object (player), zoom in , zoom out,...

it is a very important feature in games like platformers.

is there a camera object in xui2d? i saw the x2tiles example and what i see is just a complex calculation of the center of the world. it would be nice if i could initialize an object and just tell him to follow my player and nothing else.

thanx, ilan
 

sorex

Expert
Licensed User
Longtime User
to reposition the camera/viewport you can use

B4X:
X2.UpdateWorldCenter(X2.CreateVec2(0, 3))

just give it the coordinates of your player and it should follow it.

I thought there was a zoom level too, maybe I'm confused with the other game engine Erel made a while ago as I can't find it right away.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
As @sorex wrote it is quite simple to move the world center. X2Tiles is not the best example for this.

This code is from Mario example. It is inside Mario.Tick:
B4X:
If bw.Body.WorldCenter.X > x2.ScreenAABB.Center.X Then
       'update the screen center
       x2.UpdateWorldCenter(x2.CreateVec2(bw.Body.WorldCenter.X, x2.ScreenAABB.Center.Y))
       bw.mGame.WorldCenterUpdated(GS) 'create enemies based on the world position.
   End If

To avoid issues where the world is updated at the middle of the game step, Mario.Tick is called from Game.Tick which is run first:
B4X:
Public Sub Tick (GS As X2GameStep)
   mMario.Tick(GS) 'run first as mario updates the world position
And there is a check in Mario.Tick at the beginning to prevent it from running multiple times.

There is current no built-in support for zooming. This is a more complicated feature. It will probably be added in the future.
 
Top