Godot Version
4.5.stable
Question
I’ve been writing a ton of scripts and logic for a game set on a grid. I have scripts that handle things like grid size, object coordinates, converting from grid-space to real-space, etc etc. The entire game has, so far, been developed in 2D.
However, I’d like for this game to make be set in a 3D scene, making use of 3D models and camera. Ideally, all the 2D game logic I already have would just function as-is, and “exist” on the XY plane of the 3D environment, and 3D nodes would simply follow their 2D counterparts, essentially just providing them with models and necessary visuals.
This feels like it should be possible to me, in theory, but I’m not really finding any good resources on how to do something like this online. Is this doable?
An example of how I was hoping this might work (it doesn’t, for clarity):
GridManager and GridObject, here, are my own Node2D subclasses for handling the grid.
i dont think the position gets inherited between 2d and 3d nodes. You would have to approach this differently (add the third axis to your 2d code or some converter).
for my grid-based 3d game i used astargrid2d and just added the third coordinate everytime i needed to move my characters
1 Like
Just adapt the code to work for 3D. Any kind of “mixing” will not work.
2 Likes
The question wasn’t about 2D logic. It was about 2D nodes driving a 3D scene. That would be really really cumbersome and impractical. It’s much better to adapt the 2D logic code to run directly on 3D objects.
2 Likes
Hm, I just realized I responded to ai generated shite. Flagged.
2 Likes
I’m doing this right now, except I created 2D logic using a matrix and rendered graphics directly in 3D. Now I’m making a map and mini-map in 2D. It’s basically the same thing but in reverse.
You can’t do this automatically, but it should be really easy to transfer your 2D logic space into 2D graphics and 3D graphics spaces with scripts. I think you can just add 3D children to your 2D nodes… 2D and 3D render to separate places… the only issue might be which is drawn on top of the other. If not, then you could just dynamically instance 3D nodes in a more appropriate place.
So assuming you have a grid based game and you work with grid locations and your game logic is stored in a matrix and thus your base grid unit in game logic space is 1
In 2D what if your tile graphics are 10x10 pixels. Then a tile position is Vector2(x * 10, y * 10) plus some offset if you like (I’m just rendering tile 0,0 around the origin 0,0 personally).
In 3D sizes are in meters. So you’d have a generic 3D tile you can instance which is 5x5 meters. Your 3D tile positions are Vector3(x * 5, h, y * 5). You simply map 2D x to 3D x and 2D y to 3D z and set the height to something fixed (assuming your game isn’t using height) like 0 or whatever you’d like height to be.
Note that I’m assuming your 2D coordinates follow screen space layout where (0,0) is top left and x is left/right and y is down/up (positive Y values move you downward in screen space). In 3D space X is east/west, Y is up/down and z south/north. So you just use your 2D y and your 3D z. You move the 3D camera in the same way. Assuming tile 0,0 and posistion 0,0 the camera defaults to tile 0. You move it with x=position * tile_size and z = position * tile_size.
The only big gotcha with this is rotations. In 2D screen space, a standard rotation will be clockwise, but in 3D if you’re looking down on your world from above and oriented such that North (-z direction) is at the top of the screen, a positive rotation will be counterclockwise or reverse from what it is in 2D space. So if you need to rotate anything, you just calculate your 2D rotations and negate them for use in 3D.
This was the answer that got me to my solution. Setting up 3D objects to update their positions programmatically based on the position of the underlying 2D logic is what I needed to get everything up and running. Thanks to everyone who offered suggestions!
1 Like