There are various conventions for which vector is "up" in 3D world and, consequently, which vector corresponds to the "direction" the creature/player is facing.
By default, our engine follows the convention that "up" is in +Y axis.
This is consistent with X3D. And exporters from 3D authoring software are ready for this — e.g. Blender X3D exporter by default rotates models to change +Z axis (traditional "up" vector in Blender) to the +Y axis. So things just work.
If you have existing models oriented such that +Z is "up", but you would like to use the engine default convention that +Y is "up", you can simply rotate them. You can rotate things in Castle Game Engine using the TCastleTransform.Rotation property. Note that TCastleScene also descends from TCastleTransform, so you can directly rotate a scene.
To rotate each single model from +Z to +Y, just set rotation for every TCastleScene:
Deprecated: Function create_function() is deprecated in /var/www/cge-www-preview/geshi/geshi.php on line 4751
// rotate by -90 degrees around X axis Scene.Rotation := Vector4(1, 0, 0, -Pi/2);
Here's a complete source code:
uses CastleWindow, CastleSceneCore, CastleScene, CastleVectors, CastleFilesUtils, CastleViewport; var Window: TCastleWindowBase; Viewport: TCastleViewport; Scene: TCastleScene; begin Window := TCastleWindowBase.Create(Application); Window.Open; Viewport := TCastleViewport.Create(Application); Viewport.FullSize := true; Viewport.AutoCamera := true; Viewport.AutoNavigation := true; Window.Controls.InsertFront(Viewport); Scene := TCastleScene.Create(Application); Scene.Load('castle-data:/monkey_z_up.x3d'); Scene.Spatial := [ssRendering, ssDynamicCollisions]; Scene.ProcessEvents := true; // rotate by -90 degrees around X axis Scene.Rotation := Vector4(1, 0, 0, -Pi/2); Viewport.Items.Add(Scene); Viewport.Items.MainScene := Scene; Application.Run; end.
The above option is best if you prefer to think "+Y direction is up" as soon as possible.
Alternatively, you could rotate a whole group of models (with their local transformations) from +Z to +Y, by using TCastleTransform as a group:
uses CastleWindow, CastleSceneCore, CastleScene, CastleVectors, CastleFilesUtils, CastleViewport, CastleTransform; var Window: TCastleWindowBase; Viewport: TCastleViewport; Scene1, Scene2: TCastleScene; Transform: TCastleTransform; begin Window := TCastleWindowBase.Create(Application); Window.Open; Viewport := TCastleViewport.Create(Application); Viewport.FullSize := true; Viewport.AutoCamera := true; Viewport.AutoNavigation := true; Window.Controls.InsertFront(Viewport); Transform := TCastleTransform.Create(Application); // rotate by 90 degrees around X axis Transform.Rotation := Vector4(1, 0, 0, -Pi/2); Scene1 := TCastleScene.Create(Application); Scene1.Load('castle-data:/monkey_z_up.x3d'); Scene1.Spatial := [ssRendering, ssDynamicCollisions]; Scene1.ProcessEvents := true; Scene1.Translation := Vector3(1, 1, 0); Scene2 := TCastleScene.Create(Application); Scene2.Load('castle-data:/monkey_z_up.x3d'); Scene2.Spatial := [ssRendering, ssDynamicCollisions]; Scene2.ProcessEvents := true; Scene2.Translation := Vector3(-1, -1, 0); Transform.Add(Scene1); Transform.Add(Scene2); Viewport.Items.Add(Transform); Application.Run; end.
If you want to follow "+Y axis is up" convention (easier, i.e. you don't really need to do anything):
When exporting from Blender (levels, creatures etc.), let it rotate the model, i.e. change +Z to +Y. This is the default behavior of the X3D exporter. You can verify that it happens by checking in the exporter settings that:
Make sure the
Viewpoint node in X3D (default camera)
indicates +Y as the up vector.
This is the default X3D value. You can always
just remove the Blender's camera and setup the default camera position
You can also set the viewpoint using the
"Console -> Print Current Camera (Viewpoint)".
Paste the generated
Viewpoint code into your X3D file
(or into an X3D "wrapper" file, that includes another X3D using the
Leave TCastleTransform.DefaultOrientation at the default value:
If you want to follow "+Z axis is up" convention:
When exporting from Blender (levels, creatures etc.), always select to not rotate the model, i.e. keep Blender's original coordinate system. To do this, set in the exporter settings:
Make sure you use view3dscene (or other VRML/X3D editor) to
Viewpoint in your level that makes gravity
working in the Z axis.
You can set the viewpoint using the view3dscene feature "Console -> Print Current Camera (Viewpoint)", just make sure to set earlier the "Navigation -> Set Up (and Gravity Up) +Z".
Set TCastleTransform.DefaultOrientation :=
There are two common conventions:
Most 3D modeling software, like Blender, prefers the "up" vector to be +Z.
This is natural if you think about a level map spread on the XY plane. The Z coordinate is representing the height above the ground.
X3D, default OpenGL camera, and various game engines (Castle Game Engine, Unity3d, Unreal Engine) by default prefer the "up" vector to be +Y.
This makes sense if you think about the screen as being the XY plane. Then if you look straight (in the right-hand coordinate system, as used by X3D, OpenGL and Castle Game Engine) you look in the -Z direction.
One argument in favor of this convention is that the default camera makes sense for both 2D and 3D games. For 2D games, X goes to the right, Y goes up, and Z doesn't matter (or is used to position layers relative to each other). For 3D games, again X goes to the right, again Y goes up, and Z represents the depth. When your game character jumps, the Y coordinate increases — whether it's a 2D or 3D game.
As you can imagine, other conventions are possible, as you can pick any 3D vector as "up", and pick anything orthogonal to it as "direction".
While our engine defaults to the convention "+Y is up", it is actually configurable:
Following the X3D standard about X3DViewpointNode, the gravity is working along the negative Y-axis of the coordinate system of the currently bound X3DViewpointNode node.
In practice, you can just set the viewpoint using the
"Console -> Print Current Camera (Viewpoint)".
Before doing it, you can use the "Navigation -> Set Up (and Gravity Up) ..." menu item.
Viewpoint node will have correct settings.
Alternatively you can set the camera using
method with an explicit
To make things work smoothly, you usually want to use the same conventions for "up" throughout your asset creation process. Be wary of this when creating TCastleTransform instances in the engine, when exporting 3D models from Blender, when setting viewpoint (with gravity) in whatever way etc.
Note that Blender (and other 3D modeling software?) by default rotates models when exporting to X3D, to turn +Z into +Y. On one hand, this means that some things will "just work" (you use +Z convention inside Blender, and you get +Y convention inside VRML/X3D). On the other hand, this may create a little confusion if you manually probe some positions in Blender model and type them in X3D code (or ObjectPascal code using our engine): since Blender rotated the models, we necessarily "see" a different coordinate system than what you see in Blender.
For this reason, you may decide to disable this "rotation on export" feature, and instead decide to work with VRML/X3D files where +Z is "up".
VRML/X3D is flexible in this regard: although the default is to have
up in +Y, the specification says that up is +Y transformed by the
Viewpoint node transformation, and we honour it. In short, this means
that gravity is configurable in VRML/X3D file. You can setup your
camera in view3dscene,
use "Navigation -> Set Up (and Gravity Up) ...", then use
"Console -> Print Current Camera..." option, and copy-paste the
generated code from the console to your VRML/X3D file. This will set a
Viewpoint with desired up vector, which will be correctly used by our
engine (and other good VRML/X3D browsers actually) for gravity.
The notion of direction/up is used by our engine in two places:
Gravity pulls things (player, items, creatures...) down in the -up
vector. We automatically detect this based on the gravity vector of
Viewpoint inside your Viewport.Items.MainScene (you usually
want to set this to your level 3D model). This means that we follow
VRML/X3D specification, and gravity vector is derived from the 3D
model of your level. You can use e.g. view3dscene to generate
Viewpoint node with a desired gravity vector. You can read this vector
by looking at
If you use
TCastleTransform.Up properties to rotate your models
(which is the most natural way to transform creatures, player, and items)
then they need to know what is your default orientation.
That is, how does the model look like when rotation is at zero.
You configure this using the
Usually, you want to just set TCastleTransform.DefaultOrientation, and then it will be used for all your models.
Copyright Michalis Kamburelis and other Castle Game Engine developers.
Thank you to Paweł Wojciechowicz from Cat-astrophe Games for various graphics.
This documentation is also open-source and you can even redistribute it on open-source terms.