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:

here is it, IDK what is happened, maybe is a problem on your side (like VPN or something)
:thinking::thinking::thinking::thinking:

take that again…
https://gitlab.com/jeff-apple/4d-shapes-godot-engine/-/blob/main/ALL_THE_TESERACTS.rar?ref_type=heads

:thinking: have you try again to open the website?

I open this site almost every day — I have a work placed on it. :man_construction_worker: Other projects open without problems.

When I try to open the link from a browser where I am not connected to GitHab, I am prompted to register. But from my main, working browser, it doesn’t find the page.

Yeah I got prompted to register, registered, and then got a 404. Make sure your project is public. I can’t even see your user account.

1 Like

Excuse me for asking, but… why can’t projects be shared from here and do they have to be done in repositories?

I mean, I’m referring to the margin of benefits of uploading to a repository. I mean… By any chance, this could have been done here before, but some trolling individuals sabotaged it? Or… was this never created here in the first place?

I didn’t want to do it through my GitHub account, but I think I’ll have to figure out how to do it…

:weary_face: Ahh… if only using the terminal wasn’t so annoying. I remember uploading that project from that JavaScript bootcamp caused me to fill the terminal with letters like git --init, git this, git that, and I also had to manually search for the folder…

Just for that JS project. That’s why I don’t like that repositories force me to upload files in such a tedious way, and that it wasn’t as easy as uploading a file to Dropbox

I don’t know if the little information I put on my profile on those sites is detrimental to me. I’m really sorry, but I don’t know how to properly upload projects to repositories yet… What I can do is upload screenshots of the nodes and paste the code that generates the tesseract like I did before. I know it’s not the most formal or professional solution, but it’s what I know. :confused:

If it helps, I should clarify that the projects and files present the last time I worked on it are not many, many of the scenes are disposable, some nodes and scripts too; the green hypercube is just another instance of Godot’s default cube that I only put green for clarity, and it only depends on a single script to work; the other shiny tesseracts are basically the same.

There are no special or out of place modifications inside or outside the engine.

I’m not as familiar with GitLab - it’s been years since I’ve used it. But those command line commands work with either.

As part of an online course I started, I wrote step-by-step instructions on how to get GitHub working in Godot. It teaches you how to install all the software, make the accounts and the repository, and then use the GitHub plugin for Godot to upload your stuff. Why don’t you see if that makes it less painful?

1 Like