0 votes

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();
            st.Begin(Mesh.PrimitiveType.Triangles);
            for(int i=0; i<points.Length; i++){
                st.AddVertex(points[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?

in Engine by (32 points)

1 Answer

+1 vote
        for(int i=0; i<points.Length; i++){
            st.AddVertex(points[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: https://github.com/godotengine/godot/blob/66b0b0c153aa6b811c4ea3c0e5edddd7f2cb8062/scene/resources/convex_polygon_shape_3d.cpp#L43
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".

by (29,034 points)
edited by

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:

http://blog.andreaskahler.com/2009/06/creating-icosphere-mesh-in-code.html

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

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.

Can I create icosphere using Godot?

Using code, yes

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.