Ken-Soft

Software and the World


3D Cube Engine - Java

Posted under Game Development, Java, Programming, Software Development by Kenny on Saturday 27 June 2009 at 10:35 am

Bookmark and Share

This is a recent project of mine for building 3D puzzle games, (like Rubik’s Cubes).

While the code could be optimized quite a bit, this is mainly for those who wish to better understand concepts of 3D programming. For example, one way to create, store, and manipulate a 3D polygon, whether it be a Cube, or an ellipsoid. This project contains code and algorithm’s to rotate 3D polygons, around the origin, and themselves. Ability to draw 3D objects in order of increasing Z-values. Draw wireframe or solid color polygons. Keyboard and Mouse controls to move and alter the Polygons. It is a work in progress and not intended for use in graphic intensive games.

Note: later I will add rotateAroundVector() for more general rotation
Note: Ellipse3D is still very primitive but demonstrates one method creating an ellipsoid
Note: Not all Jar files contain the same versions of code. Each Jar file contains code specific to what it is supposed to do.

All source code can be found in the JAR files
Downloadable Jars (simple to complex):
1. RenderWiredCubes3D-demo.jar: this code rotates 4 rotating cubes about the origin.
2. RenderPolygons3D-demo.jar: this code demonstrates various ellipsoids and cubes rotating around unique axises.
3. Cubes3D-demo.jar: this code demonstrates more of the functionality of the 3D Cube engine by creating a Rubik’s cube like look and feel.
Controls

  • Mouse: click and drag the cube
  • A - reset
  • S - toggle between solid and non-solid mode. (i.e. the inside is filled with cubes or not)
  • D - randomly select an internally defined color scheme
  • F - toggle between random rotate mode. Just try it :)
  • Q - Decrease space between pieces
  • W - Increase space between pieces
  • E - Decrease size of cubes
  • R - Increase size of cubes
  • T - Decrease dimensions. i.e. 4×4x4 -> 3×3x3
  • Y - Increase dimensions. i.e. 4×4x4 -> 5×5x5 (not that if solid mode is true, then it will render slower
  • Arrows - Translate the cubes across the screen

If you’re only interested in the Transformation algorithms, check the below link:
Graph4D - demonstrates methods and actual source code for rotating a 4D vector.
Graph3D - demonstrates methods and actual source code for rotating a 3D vector.

Share on Facebook

4D Maze - (Java)

Posted under General Development, Java, Mathematics, Software Development by Kenny on Wednesday 14 January 2009 at 12:23 am

Bookmark and Share

This is a simple implementation of a 4D maze written in Java. As of now it is entirely text based however, a simple GUI is currently under development. Either way, this simple implementation should be useful to modify and/or build off of.
The algorithm used to generate the map is quite simple and always generates a perfect maze.

1. Randomly choose a starting room
2. Add all surrounding rooms that have not been visited, or to a list
3. While their are rooms in the list randomly select one
4. Set the room to visited, and start again from step 2 using the current room

Note: the Java version used in this implementation uses recursion to do this.
This code has not been thoroughly checked for possible bugs, and I am sure there is a more efficient method to write a 4D maze. But, at the very least, it is flexible and each room of the maze is of class type Room4D and should be easy to modify.
The Jar file can be downloaded here: Maze4D.jar
To run from the command line:

java -jar Maze4D.java dimXYZU

Ex:

java -jar Maze4D.java 5

This initializes a 5×5x5×5 maze. Start = (0,0,0,0), Finish = (5,5,5,5)
that’s 5^4 rooms, 625 rooms, that’s a lot of rooms to navigate in 4D. :)

java -jar Maze4D.java dimX dimY dimZ dimU

Ex:

java -jar Maze4D.java 5 5 3 2

This initializes a 5×5x3×2 maze. Start = (0,0,0,0), Finish = (5,5,3,2)
5×5x3×2 = 150 rooms.

java -jar Maze4D.java

creates a default maze of dimensions 4×4x4×4

Note: be careful not to load a 4D maze that’s too big or Java will run out of memory, depending on your Java settings. For example creating a room that is 10×10x10×10, 10000 rooms, will likely cause an error caused by using too much memory. besides, a 10000 room is very, very complicated, especially in 4D.
4D maze java

Rect4D.java

Point4D.java

Room4D.java

Maze4D.java

Share on Facebook

4D Rotation Matrix - Graph 4D

Posted under General Development, Java, Mathematics, Programming by Kenny on Thursday 8 January 2009 at 3:52 pm

Bookmark and Share

This program rotates points about the XY, YZ, XZ, XU, YU, and ZU axises. I then projects each 4D vector to the 2D canvas.
The Jar file can be downloaded here: Graph4D.jar

4D rotation matrix java 4D rotation matrix java

Before looking at the source, let’s take a look at some of the fundamental mathematics behind the software.
If you are uncomfortable with the thought of 4D matrix rotations, then I recommend reading Wikipedia, or checking out my article about 3D graphing, which can be found here. In this example, I will only show the 4D rotation matrices. Note that for each rotation matrix, 2 axises are held still while the vector is rotated around the other two axises. This may be hard to visualize at first, but It will become clear after a while.
rotXY = delim{[}{matrix{4}{4}{{cos(theta)} {sin(theta)} 0 0 {-sin(theta)} {cos(theta)} 0 0 0 0 1 0 0 0 0 1}}{]}rotYZ = delim{[}{matrix{4}{4}{1 0 0 0 0 {cos(theta)} {sin(theta)} 0 0 {-sin(theta)} {cos(theta)} 0 0 0 0 1}}{]}rotXZ = delim{[}{matrix{4}{4}{{cos(theta)} 0 {-sin(theta)} 0 0 1 0 0 {sin(theta)} 0 {cos(theta)} 0 0 0 0 1}}{]}rotXU = delim{[}{matrix{4}{4}{{cos(theta)} 0 0 {sin(theta)} 0 1 0 0 0 0 1 0 {-sin(theta)} 0 0 {cos(theta)}}}{]}rotYU = delim{[}{matrix{4}{4}{1 0 0 0 0 {cos(theta)} 0 {-sin(theta)} 0 0 1 0  0 {sin(theta)} 0 {cos(theta)}}}{]}rotZU = delim{[}{matrix{4}{4}{1 0 0 0 0 1 0 0 0 0 {cos(theta)} {-sin(theta)} 0 0 {sin(theta)} {cos(theta)}}}{]}

The source code can be found below as well as being bundled into the Jar file.

Transform4D.java - contains method for rotating a 4D vector

Transform4D.java

Point4D.java

Graph4D.java

Share on Facebook

Next Page »

Copyright © 2009 www.Ken-Soft.com