Anyone interested in these 4D TESSERACTS code for an own project?

:waving_hand: hi devs, how are you all, I want to share with you and with the forum an old code that I was making along time ago, is about how to use the node4D (rgba in Godot) to make a 4D HYPERCUBE IN GODOT, please take a look in the image bellow​:down_arrow:


the red and light blue in the left side :red_square::blue_square:, well
 they are just some older versions, previous than the green one​:green_square:, and just the green one is the one I was looking for, is an hypercube that looks like a 3D normal cube, but it can fold like origami thanks to the extra dimension, BUT! that’s all I have, I could’nt make something interesting with this code, I was thinking about to make a demo with a kind of 4D dice :game_die: with the green hypercube, but
 I could’nt :thinking: AND THE CODE OF THE GREEN ONE IS REALLY HEAVY and it needs optimizing, and I don’t know how, and mostly AI don’t know very well how to use Godot, so
 here is this

@tool
extends CharacterBody3D

@export var w_position: float = 0.0
@export var w_rotation: float = 0.0

var cube_vertices = [
	Vector3(-1, -1, -1),
	Vector3(1, -1, -1),
	Vector3(1, 1, -1),
	Vector3(-1, 1, -1),
	Vector3(-1, -1, 1),
	Vector3(1, -1, 1),
	Vector3(1, 1, 1),
	Vector3(-1, 1, 1)
]

var hypercube_vertices = []
var debug_printed = false

func _ready():
	print("Calling generate_hypercube from _ready")
	generate_hypercube()
	print("Hypercube vertices after generate_hypercube in _ready: ", hypercube_vertices.size())
	set_process(true)

func _process(_delta):
	if not debug_printed:
		print("Calling draw_hypercube from _process with debug")
		print("Hypercube vertices before draw_hypercube in _process: ", hypercube_vertices.size())
		draw_hypercube(true)
		debug_printed = true
	else:
		draw_hypercube(false)

func generate_hypercube():
	print("Generating hypercube vertices")
	hypercube_vertices = []  # Aseguramos que hypercube_vertices esté inicializado
	for vertex in cube_vertices:
		hypercube_vertices.append(Vector4(vertex.x, vertex.y, vertex.z, -1))
		hypercube_vertices.append(Vector4(vertex.x, vertex.y, vertex.z, 1))
		print("Added vertex: ", Vector4(vertex.x, vertex.y, vertex.z, -1))
		print("Added vertex: ", Vector4(vertex.x, vertex.y, vertex.z, 1))
	print("Hypercube vertices generated: ", hypercube_vertices.size())

func rotate_4d(vertex, angle):
	var cos_angle = cos(angle)
	var sin_angle = sin(angle)
	return Vector4(
		vertex.x * cos_angle - vertex.w * sin_angle,
		vertex.y,
		vertex.z,
		vertex.x * sin_angle + vertex.w * cos_angle
	)

func project_to_3d(vertex4):
	var w = 2 / (2 - vertex4.w + w_position)
	return Vector3(vertex4.x * w, vertex4.y * w, vertex4.z * w)

func sd_hypercube(p, b):
	var d = abs(p) - b
	return min(max(d.x, max(d.y, max(d.z, d.w))), 0.0) + max(d, Vector4.ZERO).length()

func draw_hypercube(should_print_debug=true):
	#print("Drawing hypercube")
	var mesh_instance = get_node("MeshInstance3D")
	if not mesh_instance:
		print("Error: MeshInstance3D no encontrado")
		return
	
	var array_mesh = ArrayMesh.new()
	var vertices = PackedVector3Array()
	var indices = PackedInt32Array()
	var uvs = PackedVector2Array()
	var tangents = PackedFloat32Array()
	
	if hypercube_vertices == null or hypercube_vertices.size() == 0:
		print("Error: hypercube_vertices estĂĄ vacĂ­o o no inicializado")
		return
	
	var projected_vertices = []
	for vertex4 in hypercube_vertices:
		if vertex4 == null:
			print("Error: vertex4 es null")
			continue
		var rotated_vertex = rotate_4d(vertex4, w_rotation)
		var vertex3 = project_to_3d(rotated_vertex)
		projected_vertices.append(vertex3)
		vertices.append(vertex3)
		uvs.append(Vector2(0, 0))  # Añadimos coordenadas UV por defecto
		tangents.append_array([1.0, 0.0, 0.0, 1.0])  # Añadimos tangentes por defecto (4 componentes por tangente)
	
	# Definir las caras del hipercubo
	var faces = [
		[0, 1, 2, 3], [4, 5, 6, 7], [0, 1, 5, 4], [2, 3, 7, 6],
		[0, 3, 7, 4], [1, 2, 6, 5], [8, 9, 10, 11], [12, 13, 14, 15],
		[8, 9, 13, 12], [10, 11, 15, 14], [8, 11, 15, 12], [9, 10, 14, 13],
		[0, 1, 9, 8], [2, 3, 11, 10], [4, 5, 13, 12], [6, 7, 15, 14],
		[0, 3, 11, 8], [1, 2, 10, 9], [4, 7, 15, 12], [5, 6, 14, 13]
	]
	
	for face in faces:
		indices.append(face[0])
		indices.append(face[1])
		indices.append(face[2])
		indices.append(face[2])
		indices.append(face[3])
		indices.append(face[0])
	
	var arrays = []
	arrays.resize(Mesh.ARRAY_MAX)
	arrays[Mesh.ARRAY_VERTEX] = vertices
	arrays[Mesh.ARRAY_INDEX] = indices
	arrays[Mesh.ARRAY_TEX_UV] = uvs
	arrays[Mesh.ARRAY_TANGENT] = tangents
	
	array_mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)
	
	mesh_instance.mesh = array_mesh
	
	if should_print_debug:
		print("NĂșmero de vĂ©rtices proyectados: ", projected_vertices.size())
		print("NĂșmero de Ă­ndices: ", indices.size())
		var num_aristas = float(indices.size()) / 2.0
		print("NĂșmero de aristas proyectadas: ", num_aristas)
		if indices.size() % 2 != 0:
			print("Error: La cantidad de Ă­ndices no es mĂșltiplo de 2")
		else:
			print("Los Ă­ndices son correctos")

:eyes: excuse me if I forgot to make a translation in some words, is spanish, sorry! I will be able to help to translate where you don’t understand

WHAT WAS MY INSPIRATION OR IDEA?
Well, I just wanted to make a puzzle game that has some 4D visual tricks on simple 3D shapes, like 4D toys, or even more just like Miegakure


And finally, I took a little of the code in this video just for make my 4D hypercube in GDscript

And a last question: how can I upload the winrar of my project if you don’t mind? :thinking:

2 Likes

You can upload the archive to a file sharing site and post the link here.

4D cubes simply “do my head in”. Have you ever seen the sphere packing problem, only solved for 8 and 24 dimensions. The packing density drops to below 20%! That is 80% empty space! All other dimensions are still a mystery (above 4). As I said, it absolutely breaks my brain!

PS I would love to try out your code!

I recommend you make yourself a GitHub account, upload your project there and share it that way. It will be available forever(-ish), and is mush more likely to be around as long as this thread is.

P.S. I played the game made in the video on itch.io and it was really cool. 4D Explorer by Jelle Vermandere

1 Like

I already have a Github account, but I don’t use it very often (àȠ∀àČ  hehe :sweat_smile:) the only project that is stored right there is an exercise for a frontend bootcamp and I upload it as private
 and was overwhelming to upload it with Git via cdm for first time for me àČ _àČ 

but thanks for the advice, I will try to do it, I will come back with news about where to find the whole file about the 4D shapes :+1:

PD: may I have to translate all the variables and strings, right? àȠ∀àČ 

2 Likes

Given the specifics of GitHub, it might make sense to use GitLab.

1 Like
  1. Install Git: Git - Downloads
  2. Navigate to the directory where your project is stored in Windows.
  3. Right-click and select Open Git Bash Here from the menu. (If you’re not in Windows just open a terminal window in the directory.)
  4. Log into your GitHub account.
  5. Create a new repository. (Do NOT select to add any of the optional starting files like a README or LICENSE.)
  6. Follow the onscreen instructions for initializing a new repo.

That’s it!

Or you can try GitLab instead. It’s good too.

Finally! :face_with_crossed_out_eyes: here is it on Gitlab, you must see the winrar of the project
https://gitlab.com/jeff-apple/4d-shapes-godot-engine

PD: sorry for not make it better, I’m too newbie for upload repositories

2 Likes

I already uploaded it, sorry if is the ugliest repository you have seen

1 Like

thank you and you all for your patience :handshake:

1 Like

404: Page not found

:pouting_cat: