- Home /

# Creating a Tesseract - a 4D cube.

Hi all! I've got a question which will probably raise more questions than answers, but I'd appreciate any advice I can get.

I'm looking to create a tesseract, which is a 4 dimensional cube. My plan is to create a custom mesh from a set of vertices, and redraw the mesh every time the rotation of the shape changes. I am not worried about precision as this is purely for visual effect(ie - issues such as Gimble Lock can be ignored), and I will only be concerned with rotation, not position.

So - is this theoretically sound? Would 4D rotations be calculated in a similar way to 3D rotations? Would the Vector4 be my friend here? Is anyone aware of any existing c# projects or 4D rotation math? Once again, sorry for being so vague, this is a very top level question! Thanks muchly in advance!

Look, there are no "4D objects". It's just an abstraction by mathematicians.

In different conventionalities, you can talk about the projection of 4D objects on to 3D. (Just as - depending on how you look at it - the "projection" of a sphere on to 2D could, in some situations, be said to be a circle.)

So all you're talking about is "rendering some 3D shape".

Yes, you can render absolutely any 3D shape or object, whatsoever, in Unity.

OK?

It would be like if you asked "I'm looking at drawing < some particular mathematical abstraction related to 3D objects > on a piece of paper, can I do that?"

Yes, you can draw any 2D drawing, whatsoever, on paper.

Yes, you can render any 3D object, whatsoever, in Unity.

For math discussion on 4D shapes, etc, math-stackexchange site

**Answer** by ataxkt
·
Apr 15, 2013 at 07:36 PM

Answering my own question, because I'm just that much of a dick. Results can be seen here: http://tomkail.com/tesseract/ If anyone is interested in the code, let me know! Thanks very much everybody for your support, I'll be using these forums more!

You should definitely answer your own question!

It would be great to get a description of how you did it...

In short, I ported the code from the link at the bottom of the page. In long: A list of Vector4s is created, and filled with a value for each vertices of a tesseract, of which there are 16 (essentially two cubes of 8 verts, with the second cube's w axis inverted).

```
verts = new List<Vector4>(){
new Vector4(1,1,1,1),
new Vector4(1,1,1,-1),
new Vector4(1,1,-1,1),
new Vector4(1,1,-1,-1),
.....
```

Rotations are carried out over different axis and stored in a list of rotations (instead of using the GO's Transform, which is three-dimentional).

The rotations are calculated using a matrix (which can be found in Milosz code).

Lastly, I draw planes to fill all 24 faces of the tesseract using the vert array (where the 4D verts are treated as 3D verts).

And that's it! The code can be found at this link.

Hi, I know that this is old post, but I've got different method to create 4D Objects in unity, also it's supported to create another 4D Objects from any 3D Meshes. check it here.

and anyway, your tesseract is awesome too!, just want to see how did you calculate that.

Oh wow! That tool looks amazing. I don't suppose you'd consider sending me a copy of your tool? I'd love to have a play!

my code just simply duplicate two meshes together and then interpolating it with this code :

```
Vector3 DiffA = Vector3.Lerp (Vector3.zero,m_wOffset,m_wPivot);
Vector3 DiffB = Vector3.Lerp (m_wOffset,Vector3.zero,m_wPivot);
float FacA = Mathf.Lerp (1, m_wScale,m_wPivot);
float FacB = Mathf.Lerp (m_wScale,1, m_wPivot);
mesh.vertices =
System.Array.ConvertAll (oriVert, x => vRot (x * FacA + DiffA , m_wRotation, x)).Concat( // First Mesh
System.Array.ConvertAll (oriVert, x => vRot (x / FacB - DiffB , m_wRotation, x))).ToArray(); //Second Mesh
// OriVert -> Original Vertices
// vRot(Axis, Input, Pivot) -> Rotating Vector3 with Quaternion
mesh.RecalculateBounds ();
```

well actually I'm trying to upload the webplayer but I get limited budget (Dropbox need Pro ones) so I still looking for alternatives one.

**Answer** by Bunny83
·
Apr 15, 2013 at 08:12 AM

The easiest way to draw 4D objects in 3D space is to simply introduce another axis. So you would use forward, up and right of a transform as the first 3 axis. Just use another transform's forward axis as the 4th axis. To calculate a 4D point projected into our 3D space just do this:

```
var axis0 = transform1.forward;
var axis1 = transform1.up;
var axis2 = transform1.right;
var axis3 = transform2.forward;
var pointIn4D = new Vector4(xx,xx,xx,xx);
var projectedPoint = pointIn4D.x*axis0 + pointIn4D.y*axis1 + pointIn4D.z*axis2 + pointIn4D.w*axis3;
```

You could also setup an projection matrix from the 4 axis. I always get confused if Unity uses column or row multiplication, but in general it would look like:

```
a0x a1x a2x a3x
a0y a1y a2y a3y
a0z a1z a2z a3z
0 0 0 0
```

or

```
a0x a0y a0z 0
a1x a1y a1z 0
a2x a2y a2z 0
a3x a3y a3z 0
```

Like others already said there is not "right" way to draw a hypercube in 3D space since that's not possible. You can only draw an projection of it in a certain state. Like 3D can't be displayed on a 2D screen. It's only a projection of the current viewing angle.

Take a look at this:

Just as Bunny says ...

even what we think of as a "2D image" is just one mathematical approach, an abstraction, an idea, of a way to do it.

Sure, it's sort of roughly what Vermeer invented or (in some sense) our eyeballs do, but it has no real meaning.

Indeed note that, immediately, Unity presents two utterly different ways to map 3D to 2D - both of which are there simply because "those two particular choices happen to be common in video games"

Thanks for the advice everyone! You are of course correct about the 2d/3d reprentation analysis - I think Carl Segan called a 3d model of a 4d shape a 3d shadow - so I guess that makes this a 2d shadow of a 3d shadow of a 4d shape! I'll have a closer look at the matrices and the wiki link when I get home. This link http://hypersolid.milosz.ca/ best explains the effect I'm looking for. Looking through the code, it seems that the verts are being rotated using a simple looking 4D rotation matrix around 6 different axis, which is interesting. I because rotations are not stored in the GO's transform, I will need to change the stored rotation in relation the camera (unless I went for a custom camera setup), but I'll ignore that for now. Its probably not the best way to go about it, but I'll attempt to port it into Unity later on - I'll post a link if I have any success.

I LOVE Carl Sagan, man !

I have an old-school boxed-set of **Cosmos**, even.

I suggest everyone just takes an hour off an watches this!

http://www.youtube.com/watch?v=dADUBcoEEHw

What a great guy.

the new world's greatest TV moment. Hooray, Carl Sagan.

**Answer** by 1337GameDev
·
Apr 14, 2013 at 11:03 PM

I would look up general 4d representations in 3d space. It seems very odd / impractical or even impossible to render a 4d mesh in 3d space.

I would just simulate 4d using a cube inset in another cube. You could also do mesh deformation / small meshes for "pipes" that make up the cube. This video shows the apparent rotation. Try to simulate what this looks like / does.

Surely the existence of that video proves it possible? You are correct about it being difficult to render in 3D space though - I'm not sure how the shape would cope with camera rotation :/ The two cube idea would work if the hypercube did not rotate (two cubes being the "foundation" of the shape anyway), and the pipes idea would still rely on drawing from a set of 4D rotated verts as I proposed above.

It's actually being rendered in 2D space right, via the projection to the screen :) Could just go the whole way right?

**Answer** by whydoidoit
·
Apr 15, 2013 at 08:04 AM

Seriously though you are going to need to look at matrices with an extra dimension - so a 4x4 rotates/translates and scales a 3D object - I'm guessing that math will extend to rotating a 4th dimension with a combined isoclinic matrix or something :S. Then you'd need a 4D to 3D projection matrix...

This is a question best answered somewhere where there are proper mathematicians - I still have to read the manual to work out what all the buttons on my calculator do!

See this: http://en.wikipedia.org/wiki/Rotations_in_4-dimensional_Euclidean_space

Looks like a great (and really confusing) link. I'll take a look later! Thanks.

**Answer** by aliaksandryemialyanau
·
Nov 01, 2017 at 02:36 PM

Rotation in four-dimensional space.

https://youtu.be/vN9T8CHrGo8 The 5-cell is an analog of the tetrahedron.

https://youtu.be/z_KnvGGwpAo Tesseract is a four-dimensional hypercube - an analog of a cube.

https://youtu.be/HsecXtfd_xs The 16-cell is an analog of the octahedron.

https://youtu.be/1-oj34hmO1Q The 24-cell is one of the regular polytope.

https://youtu.be/w3-TqPXKlVk The hypersphere is an analog of the sphere.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

Procedurally Generated Cube Mesh 3 Answers

Sphere made of cubes algorithm 3 Answers

Highmap on a cube. 0 Answers

Custom Mesh UV Problem 1 Answer

Simple plane generation 1 Answer