Creating programmatically sphere problem.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheLoraxPl

I tried to create a sphere from vertices;

using Godot;
using System;
public class new_script : StaticBody{
     public override void _Ready(){
			//get the collisionShape vertices
			CollisionShape collisionShape = (CollisionShape)GetNode("CollisionShape");
			ConvexPolygonShape cPolygonS =  (ConvexPolygonShape)collisionShape.Shape;
			Vector3[] points = cPolygonS.Points;
			//create visual copy
			SurfaceTool st = new SurfaceTool();
			for(int i=0; i<points.Length; i++){
			//instantiate it
			((MeshInstance)GetNode("MeshInstance")).Mesh = st.Commit();

(the collision shape is made by Mesh->Create Convex Collision Sibling) and that happened:

looking at y+

then I tried to download sphere vertices creating code, but it has the same result.
I am using C# and Godot Mono v3.1.1
Anyone know what is going on?

:bust_in_silhouette: Reply From: Zylann
        for(int i=0; i<points.Length; i++){

This is adding a vertex for every point of the sphere as you got them from the collision shape. I believe those points really don’t work the same way as what the rendering engine expects (which is understandable since they come from the physics engine). Visual meshes require to be built by defining triangles (vertices connected together by groups of 3).

If all you want is to spawn a sphere matching the size of your collision shape, you can just use a SphereMesh. However your sphere comes from a ConvexPolygonShape, which is quite a strange choice for such a simple shape (you could have used SphereShape).

So in order to try to help still with that specific situation, I generated a convex polygon shape from a simple CubeMesh and used the same code as yours. While the points obtained are not just the 8 cube corners, that still doesn’t work, as it looks like points are listed quite randomly. If that’s the case, it makes reconstruction as a visual mesh near impossible, as the orientation of faces also plays a role in the order they come in…
It’s quite chaotic. For example I tried to plot which triplets of vertices I got, and it was describing triangles going in diagonal through the cube, not even faces, which makes no sense.

So I had a look at the code in the engine to see at least how it was drawing them as lines (not even solids!), and there I found it uses a C++ internal helper to compute a hull from it:
For which there is no binding to GDScript. Looks like points of a ConvexPolygon are completely useless here.

However, that doesn’t mean you can’t generate a sphere from code. It just means “generating a mesh representing the same points as those found on a ConvexPolygonShape solely using the script API is not possible”.

I want create a sphere, which has ArrayMesh and vertices. Ok thanks. You’re right now I see. Even if i have sphere point i have to order it and copy (more triangles contains the same point) . So I found that site:

and convert it into C# Godot code, works Great :smiley:

TheLoraxPl | 2020-05-12 12:17

Yeah if what you want is solely to create a sphere mesh, there are a number of simple ways to do it, and it doesn’t involve collision shapes at all.

Zylann | 2020-05-12 17:09

Can I create icosphere using Godot?

TheLoraxPl | 2020-05-13 10:39

Using code, yes

Zylann | 2020-05-13 15:00