2.5D game, how to start?

Hi everyone, I’ve been away for a while, now I’ve installed the latest version of Lazarus 64bit and CGE 6.5. I would like to rebuild a scene like this: a 2D background with a mesh in front of the background that represents the floor on which a 3D character will move. Where can I start? Is there any example demo? I would rather work from the Lazarus IDE.

Place TCastleSceneManager inside TCastleControlBase or TCastleWindowBase, and inside TCastleSceneManager add multiple TCastleScene, one for “floor mesh”, one for the 3D character. (The background can be done either as 3rd mesh in the background, just a quad with a texture, or it can be a user interface item like TCastleImageControl – it depends whether you need the background to move.)

You will want to go through initial chapters of manual https://castle-engine.io/manual_intro.php , from start to at least Transform, animate, duplicate, build a scene.

You can do it all in Lazarus IDE of course. Choose TCastleControlBase if you want the game to be inside an LCL form, or choose TCastleWindowBase if you do not need any other LCL controls and you want 100% of the form to contain stuff rendered by CGE.

Hi michalis, it’s possible that I didn’t understand your directions.
The first thing that confuses me is that in the palette there is no TCastleWindowBase but only TCastleControlBase.
Then I still placed TCastleControlBase on the form but it doesn’t contain SceneManager as it is with TCastleControl (as in the example in the first pages of the manual).

Scene := TCastleScene.Create(Application);
Scene.Load(‘car.x3d’);
Scene.Spatial := [ssRendering, ssDynamicCollisions];
Scene.ProcessEvents := true;
CastleControl1.SceneManager.Items.Add(Scene);
CastleControl1.SceneManager.MainScene := Scene;

How should I do to place TCastleSceneManager in TCastleControlBase?

As for TCastleWindow and TCastleWindowBase:

They are indeed not available on the component palette. You should create an instance of TCastleWindowBase (or TCastleWindow) by code. The window created this way does not use Lazarus LCL forms. The initial pages of the manual (like https://castle-engine.io/manual_window.php ) describe both the process of using TCastleWindowXxx and TCastleControlXxx.

You can use either TCastleWindowXxx and TCastleControlXxx, both approaches work 100%. I advise using TCastleWindowXxx if you only want a simplest window that displays the game. Use TCastleControlXxx if you want to mix CGE rendering with other LCL forms and visual components.

As for creating scene manager:

  • Indeed if you use TCastleControlBase (instead of TCastleControl) then it will not have a scene manager. You can trivially create it yourself, by doing

    SceneManager := TCastleSceneManager.Create(Application);
    CastleControl1.Controls.InsertFront(SceneManager);
    

    I advise doing this (instead of using TCastleControl that already created scene manager for you) in future code.

    This gives you more flexibility. This way you create scene manager just like all others 2D user interface controls (https://castle-engine.io/manual_user_interface_and_2d_drawing.php ), you can also design it using CGE editor (https://castle-engine.io/manual_editor.php – we heavily work on it, to allow designing there user interface stuff, and 2D and 3D worlds inside scene manager).

  • A similar situation is with TCastleWindowBase and TCastleWindow. TCastleWindow just has a scene manager created automatically. I advise now using TCastleWindowBase, and doing it yourself.

  • The manual is indeed not yet updated to show the approach of TCastleControlBase/TCastleWindowBase (and manual creattion of TCastleSceneManager). Most of the manual and examples instead show usage of TCastleControl/TCastleWindow. I plan to fix this ASAP :slight_smile: Sorry for confusing, I should have mentioned it earlier.

Indeed if you use TCastleControlBase (instead of TCastleControl ) then it will not have a scene manager. You can trivially create it yourself, by doing

SceneManager := TCastleSceneManager.Create(Application);
CastleControl1.Controls.InsertFront(SceneManager);

Sorry but I still don’t understand well. If instead of the TCastleControl component I use TCastleControlBase why is there a CastleControl1 in your example? I thought it might be a transcription error but CastleControlBase1 has no InsertFront in Controls, only InsertComponent.
What I’m trying to do is very simple but I have to be able to understand the various steps.
I would like to use the Lazarus form and load in it a 2D background with a mesh as a floor in front of it where my 3D character should walk. Of course I will then have to set the camera to the correct position.
I already have all the elements ready (it’s a small project made with Unity) and I’m trying to get it on your engine.I would like to use the Lazarus form because I then need to use its components.
Isn’t there any demo available that I can use as a reference?

edit: Do I have to use CastleControlBase1.Container.Controls.InsertFront(SceneManager)?

  1. Indeed, use the CastleControlBase1 instead of CastleControl1, if you use the names assigned by Lazarus by default.

    Sorry, I wasn’t paying attention to this when I wrote my short example :slight_smile:

    Note that these are just the default names assigned by Lazarus to the components, you can adjust them (change the “Name” in object inspector) to whatever you like. Usually it’s a good idea to adjust them (to something more specific to your application, e.g. maybe MainControl or GameControl instead of CastleControlBase1).

  2. Doing the CastleControlBase1.Controls.InsertFront(...) should definitely work. It is equivalent to doing CastleControlBase1.Container.Controls.InsertFront(...) .

    Checking, it seems that Lazarus Code Tools (when you press Ctrl + Space etc.) do not understand it correctly (they indeed show InsertComponent, which is not really available, and they do not show InsertFront, InsertBack etc. which are available). I didn’t notice it before. I’ll make a note to see if we can “workaround” it (on CGE side), and also submit as Lazarus bug to make it fix in future Lazarus. You can consult the documentation (API reference of TCastleControlBase in CGE 6.5 is on https://castle-engine.io/apidoc-unstable/html/CastleControl.TCastleControlBase.html ), it is reliable.

    See e.g. examples/lazarus/model_3d_with_2d_controls/ where I add various user interface controls (in this example we have TCastleControl instance named Browser). Scene manager (instance of TCastleSceneManager that you create) is just another user-interface control, so it can be added to TCastleControlBase the same way. More information about 2D user interface controls is on https://castle-engine.io/manual_2d_user_interface.php .

Done in https://bugs.freepascal.org/view.php?id=36189 :slight_smile: Several related issues have already been reported, hopefully it’ll get fixed soon.

1 Like

Basically this is a rather specific case (having 3D objects over 2D background). While it was very often used in “early games” like Alone in the Dark, it’s rarely directly used nowadays. Usually if you create a 2D game you create everything in 2D, and if you have 3D elements, then you create everything in 3D as @michalis suggested earlier, by adding the background as a 3D quad and forcing the camera into a specific orientation.

As for examples simply using Lazarus you can check lazarus folder in examples. E.g. examples/lazarus/model_3d_viewer/ - you have a TCastleControl together with LCL compnents.

Note, that last unstable version of Castle Game Engine has a very decent Castle Editor, which enables creation of UI almost the same way as Lazarus does, but much more efficiently. However, at the moment it’s still a bit more complex, because you’ll have to load and the designed UI and assign some values (like events) manually. Thou it provides for much better flexibility, e.g. you can cross-compile such program to Android much easier.

1 Like