Circular inventory in 3D

Godot Version

4.3

Question

Hey gamedevs, I’m almost near to achieve the circular inventory I want for my game like this one displayed in the image but I have a problem when displaying odd number of items

The display with a odd number of items (like 4) work as I expected

even_inventory

The problem appears when the item number is odd (like 5)

odd_inventory

This is the code I use to adjust the items on the start and move items when action is just pressed:

func adjust_items() -> void:
    var number_of_items = mini(max_items, items_container.get_child_count())
    
    for index in number_of_items:
        var angle: float = deg_to_rad(360.0 / number_of_items * index)
        var x: float = radius * cos(angle)
        var z: float = radius * sin(angle) 
        
        var child = items_container.get_child(index)
        child.position = Vector3(x, 0, z)
        
        inventory.append(child)

func next_item(direction: Vector2):
	var inventory_size: int = inventory.size()
	
	if inventory_size > 1:    
		var angleIncrement: float = 360.0 / inventory_size
		
		for i in range(inventory_size):
			var item = inventory[i]
			var angle: float = deg_to_rad(angleIncrement * (i + direction.x))

			var tween: Tween = create_tween()
			tween.tween_property(item, "position", Vector3(radius * cos(angle), 0, radius * sin(angle)), next_item_spin_duration)
			

Are you sure it’s even versus odd and not whether the number of items is a divisor of 360? I would try using index - 1 in assigning the angle instead of index.

My guess is that is has to do with where the top of your circle is. Try offsetting the angle by 90 degrees… something like :

var angle: float = deg_to_rad((360.0 / number_of_items * index)+90)
or
var angle: float = deg_to_rad((360.0 / number_of_items * index)-90)

1 Like

Oh this probably won’t work since index should already start at 0. At any rate it’s just about shifting the angle based on the increment for the given number of objects.

An alternative approach might be to just place the camera along the direction corresponding to whatever index should be in front, rather than tweaking the locations of the items.

Deeply thanks on this, I didn’t think about the angle offset and it works perfect now

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.