Im planning to do a game based on walking on small planets, if i could just put a grid map on a sphere the level making process would be much faster. the ideal grid tile shape would be hexagon or triangle (like the icosphere example image).
thanks for reading this.
Is it posible to put a grid map with snapping function on a mesh (sphere)?
does exist a way to merge the grid map and the mesh? or changing the shape of the grid? it is really discouraging, had the idea of this game for many months and got stuck on the first step
Unfortunately, I do not think it is possible to change the GridMap node so that it snaps on a sphere without editing the C++ code. There should be a way to snap objects to a sphere through code, but I haven't done it myself and Google searches are not yielding helpful results.
I think the code for keeping an object on the surface of a sphere would look something like this (untested):
tool
class_name Spatial_On_Sphere
extends Spatial
export (NodePath) var path_to_sphere setget _sphere_path_changed
var sphere
export (float) var sphere_radius = 2
export (bool) var snap_position_to_sphere = true
func _ready():
if path_to_sphere != null:
sphere = get_node(path_to_sphere)
else:
sphere = null
func _physics_process(delta):
if snap_position_to_sphere == true:
if sphere != null:
var direction_vector = (sphere.gobal_transform.origin - global_transform.origin).normalized()
direction_vector = direction_vector * sphere_radius
global_transform.origin = sphere.global_transform.origin
global_transform.origin += direction_vector
func _sphere_path_changed(new_value):
path_to_sphere = new_value
if path_to_sphere != null:
get_node(path_to_sphere)
I added a few extra things, like tool
and the ability to turn the snapping on/off, but I have no idea if the script will work and there is probably a better way to do it. Hopefully it helps give an idea on how it could be achieved though.
I'll keep my eyes open for a better solution and will post if I find something. (Side note: Welcome to the forums!)
I'm not sure about gridmaps, but I'm sure that you don't necessarily have to use gridmaps.
Now off the top of my head, if say you managed to take your icosphere from blender into godot and then parse it's each vertices location as vector positional data into an array and the respective vertices normals as direction/orientation vectors into an array, you could then iterate through the arrays and for each vertex location you instance your tile piece followed by setting the rotation/orientation of the tile piece according to the vertex normals derived directions. This way you should get just about what you want, I'd think.
For the sake of the vertex normals actually pointing to exactly where you want you have to make sure it's smooth shaded, that is, each triangle faces corner/vertex normal would be averaged with it's neighbors.
TBH I'm not sure how clear this is, it's late and I'm about to hit the bed, so a bit hazy. If someone feels they can explain it better or is willing to go into further detail that would be great.
Hey, thanks for answering, Im new to coding so i was hoping the answer was just click here and there haha, but well, TwistedTwigleg, how can i access the grid map code? modifying a code seems easier than writing one. And Megalomaniak, i think i understand, or at least the idea i got by reading your comment was to make a vector with snapping properties on a tile of the icosphere and make an array that puts that vector in every tile of the icosphere.
You can find the source code for the GridMap node on the Godot engine GitHub repository. Its all within a module and written in C++, which depending on what programming language you are using, it might be easier or harder.
- Edited
You could also use a generic (flat) GridMap and distort the game world using a "curvature" shader, so it looks like a sphere. I remember seeing some Minecraft shader mods do this.