# What is 4 Dimension and how program it =)

Let's start simple.

## 1st dimension:

Imagine a corridor.

It's simply a line, you can look into 2 directions: forwards or backwards.

In a program this would be a 1-dimensional array, i.e.

## 2nd dimension:

Imagine a corridor that has doors to rooms.

The line we had before can now have many more lines (or corridors / rooms) along the walls of the corridor.

You see can now look into 4 directions: forwards, backwards, left, right.

In a program this would be a 2-dimensional array (an array of arrays):

For rotating we use one defining angle and sin and cos:

- Sin = first and second direction (forwards, backwards)
- Cos = third and fourth direction (left, right)

In different angles we move into different directions:

- angle 90°: x+
- angle 270°: x-
- angle 0°: z+
- angle 180°: z-

## 3rd dimension:

Imagine a building, which has floors, which have corridors with rooms and doors to them.

You can look into 6 directions: forwards, backwards, left, right, top and down.

In a program this would be a 3-dimensional array:

For rotating we use 2 defining angles:

Sinus for up and down and

Cos for the XZ plane.

## Dimension Plane

Every new dimension adds a new Plane, which has 2 sides.

We only see a particular direction, if we look into it's angle.

To show the effect, we give those directions (sides) different colors.

If you look diagonal in 3D by 2 angles, we can see 3 sides:

Green Y, Blue Z, Red X:

If we look diagonal with only 1 angle, we can only see 2 sides:

Y Angle = 0;

This is how it looks like, when we see 2 X sides, 1 Z side and 2 Y sides:

Every new dimension adds 2 planar sides on one positive dimension and on one negative dimension (+/-).

They may have any color or texture.

Every new plane after 2 planes adds a new rotation axis, which is able to render it's own 2 sides and the 2 previous planars.

In 3D the angle 0 and 180 look along the Y-axis, and 90 and 270 along the XZ plane.

## 4th dimension

Imagine a group of buildings.

You can look into 8 directions: forwards, backwards, left, right, top, down, D+ and D-.

We call the new dimension D.

In a program that is a 4-dimensional array:

So we now have 2 new planes on which we can travel or look along.

If it's possible to cast a ray in 1d, 2d and 3d, it should be able to move in 4d!

And it is!

If we have a 4x4 matrix we can easy calculate Ray position with it,

but how to render it?

Let's use raycasting.

Every pixel casts a ray, which sets the color for a pixel to the color of the obstacle it collides with.

The maths:

To cast a ray, we have to properly normalize it:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| length = sqrt(x * x + y * y) invlen = 1 / length x *= length y *= length
length = sqrt(x * x + y * y + z * z) invlen = 1 / length x *= length y *= length z *= length
length = sqrt(x * x + y * y + z * z + d * d) invlen = 1 / length x *= length y *= length z *= length d *= length |

Moves In 3D it looks like here we have same XZ Position but We moves Top or bottom on Y plane

And Words Changed.

4D matrix where

In one picture we don't have a floor and in the second we have it.

We now moved in the D plane, but the XYZ position is still the same.

And Words Changed.

It's even more interesting to see how the image will be if we make the D angle diagonal and see all 4 planes:

Purpl Side is 4D plane color

It can be explain like

Rays that colis First World Out Of Draw Range Have Black color (1 world don’t have floor)

Rays that colis second World Have Grean color(2 world have floor)

Rays that colis Plane Side have purple color.

(purple D plane can have same Hole like Any other Plane)

I Have img this =)

Ray Progression 2D

Cube in 4D have 4 Planes every Plane with own color.

## Ray Progression 4D

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| y=0, z=2, x=2, d=1 y=0, z=3, x=2, d=1 y=0, z=3, x=3, d=1 y=-1, z=3, x=3, d=1 y=1, z=2, x=3, d=1 y=1, z=3, x=3, d=1 y=0, z=3, x=3, d=1 y=0, z=3, x=4, d=1 y=0, z=3, x=4, d=2 y=1, z=2, x=3, d=1 y=1, z=3, x=3, d=1 y=1, z=3, x=4, d=1 y=1, z=3, x=4, d=2 y=0, z=3, x=4, d=2 |

Every new plane add Sin self and Cos prev Planes

Camera ray Projection 3D

1 2 3 4
| ray.y += Dist * sin_Y; ray.x += Dist * sin_X * cos_Y; ray.z += -Dist * cos_X * cos_Y; |

Camera ray Projection 4D

1 2 3 4 5
| pos.d += Dist * sin_D; pos.y += Dist * sin_Y * cos_D; pos.x += Dist * sin_X * cos_Y * cos_D; pos.z += -Dist * cos_X * cos_Y * cos_D; |

## 4D matrix Img

We have int[2][2][2][2]

So We have two 3D Matrix

4th Dimension give ability to Ray travel between this two 3D matrix =)

If we in int[X][Y][Z][0] we in first 3D matrix

If we in int[X][Y][Z][1] we in second 3D matrix

All simple )

If we have matrix array like in minecraft in 4D it will be 4D cube

Every Cube In First 3D matrix have Side To second 3D matrix,

So we have 2 * 2 * 2 = 8 sides(Tunnels) between two 3D matrix =)

Same as with 3D we can travel only on Free place block (Air)

So Even in 4D you only can move through the Air {Air == 0, Block == 1}

[1][2][3] D[2] = 0

[1][2][3] D[1] = 0

[1][2][3] D[0] = 1

We in [1][2][3] D[1]

Ray can move to [1][2][3] D[2] ,

but can't move to [1][2][3] D[0] we will see wall, it can be One Color Or Texture like side in Cube

Try it yourself and make a raycasting renderer and add a 4th vector component to the ray.

Is the 4 the maximum number of possible dimensions?

No, it could be any number of dimensions. 5D, 6D or even 111D =)

Bin Link

http://www.2shared.com/complete/oWYgVUbR/4d_z.htmlMore img