Circle drawing - using ArrayMesh , how to adjust point to be equal distance

Godot Version

4.6 stable

Question

So after great feedback from @normalized I’m tried a few logical thing which come to my mind and added extra variable for holding divided angle , and one more variable for current_vector which is calculated before loop from start by rotation and then in loop by further rotation from current not start .

Also numbers of steps reduce to 32 . with point it looks “nearly ok” with lines - " someone made circle with mikado ? "

With points there seem to be greater distance between some points so not equally divided .

current code

extends MeshInstance3D

func _ready() -> void:
	var surface_array = []
	surface_array.resize(Mesh.ARRAY_MAX)
	
	var start_vector = Vector3(1.0, 0 ,0)
	var current_vector: Vector3
	var full_circle_degree_value = 360.0
	var rotation_degree_value : float
	var current_step_iteration = 0
	var numbers_of_steps = 32
	
	var my_vertices = PackedVector3Array()
	var my_uvs = PackedVector2Array()
	var my_normals = PackedVector3Array()
	
	rotation_degree_value = full_circle_degree_value / numbers_of_steps
	current_vector = Vector3(start_vector.rotated(Vector3.UP, rotation_degree_value))
	# loop 
	for i in numbers_of_steps:
		current_step_iteration += 1
		my_vertices.push_back(Vector3(current_vector.rotated(Vector3.UP, rotation_degree_value * current_step_iteration)))
		my_uvs.push_back(Vector2(0,0))
		my_normals.push_back(Vector3.UP)

	surface_array[Mesh.ARRAY_VERTEX] = my_vertices
	surface_array[Mesh.ARRAY_TEX_UV] = my_uvs
	surface_array[Mesh.ARRAY_NORMAL] = my_normals
	  
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_POINTS, surface_array)

Visualization


Best example be with numbers of steps : 4 .

somewhere is some offset , because it should be opposite each .

rotated() takes angle value in radians. You’re giving it degrees.

1 Like

rotation_degree_value = 2 * PI / numbers_of_steps

Saw a float and thought it be 0-360 or -180 to 180 but I see now :slight_smile:
1.5707963267949
never would figure out this many decimals from head

It’d be a bit more elegant to just rotate the vertex position iteratively:

var point := Vector3.RIGHT
var steps := 32
var angle_step := TAU / steps
for i in steps:
	vertices.append(point)
	point = point.rotated(Vector3.UP, angle_step)
1 Like

yes that made a code lot shorter , and looking tidy nice touch with TAU ( PI *2 )

extends MeshInstance3D

func _ready() -> void:
	var surface_array = []
	surface_array.resize(Mesh.ARRAY_MAX)
	var my_vertices = PackedVector3Array()
	var my_uvs = PackedVector2Array()
	var my_normals = PackedVector3Array()
	
	var point := Vector3.RIGHT
	var steps := 32
	var angle_step := TAU / steps
	for i in steps:
		my_vertices.append(point)
		point = point.rotated(Vector3.UP, angle_step)
		my_uvs.push_back(Vector2(0,0))
		my_normals.push_back(Vector3.UP)
	
	surface_array[Mesh.ARRAY_VERTEX] = my_vertices
	surface_array[Mesh.ARRAY_TEX_UV] = my_uvs
	surface_array[Mesh.ARRAY_NORMAL] = my_normals
	  
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_POINTS, surface_array)

1 Like