Skip to content

Commit

Permalink
more math
Browse files Browse the repository at this point in the history
  • Loading branch information
justinchang1124 authored Sep 12, 2020
1 parent c644fb6 commit 2cdd547
Show file tree
Hide file tree
Showing 4 changed files with 441 additions and 0 deletions.
44 changes: 44 additions & 0 deletions Arguments.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import java.awt.Color;

/**
* A list of all entities to be added to the engine upon starting the program.
*
* @author Justin Chang
*/
public class Arguments
{
/**
* Instructions for user input.
*/
public static final String INSTRUCTIONS =

"Instructions: " + "\n" +
"Controls for Movement: WASD, Space, Shift" + "\n" +
"Controls for Camera: Mouse Movement" + "\n" +
"Controls for Near Plane: Mouse Wheel" + "\n" +
"Controls for Zoom: O/P" + "\n" +
"Controls for Rotation: FGHJKL" + "\n" +
"E = Toggle Heads-Up Display" + "\n" +
"Left Click = Reset All" + "\n" +
"Right Click = Toggle Shadows" + "\n" +
"Left Control = Toggle Pause";

/**
* Adds all intended entities.
*/
public static void initialize()
{
Model.pipeline.add(new RubiksCube(new Vec3(0, -2000, 0), 400));
AnimatedModel.pipeline.add(new Sphere(new Vec3(0, 2000, 0), true, 200, Color.magenta, 5));

AnimatedModel.pipeline.add(new AxialStar(
new Vec3(-2000, 0, 0), true, 100));

AnimatedModel.pipeline.add(new AxialStar(
new Vec3(2000, 0, 0), false, 100));

AnimatedModel.pipeline.add(new Pinwheel(
new Vec3(0, 0, 1000), false, 1200, 800, 225, 12, 3.75,
new Color[] { Color.red, Color.green, Color.blue }));
}
}
111 changes: 111 additions & 0 deletions Movement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.awt.event.KeyEvent;

/**
* A set of static methods that govern the movement of a user avatar
* through the traditional WASD keyboard setup. SPACE and SHIFT are
* used to ascend and descend.
*
* @author Justin Chang
*/
public class Movement
{
/**
* The absolute magnitude of the user's velocity.
*/
public static final double vel = 10;

/**
* The player's current position.
*/
public static Vec3 pos;

/**
* A set of private integers used to help with a smooth keyboard navigation
* system.
*/
private static int x1, x2, y1, y2, z1, z2;

// Don't let anyone instantiate this class.
private Movement(){

}

/**
* Resets the player's position to the origin.
*/
public static void reset()
{
pos = new Vec3();
}

/**
* Standard movement controls. (Example: A 2D side scroller.)
*/
public static void tick()
{
tick(0);
}

/**
* Moves the player in a direction based on the camera. (Ex: A 3D landscape).
*
* @param h
* The player's clockwise orientation from the x-axis in radians.
*/
public static void tick(double h)
{
int netX = x1 - x2;
int netY = y1 - y2;
int netZ = z1 - z2;

// Constant used to minimize calculations.
double h_n = h + Math.PI / 2;

double newX = pos.v[0][0] + (netX * Math.cos(h) + netY * Math.cos(h_n)) * vel;
double newY = pos.v[1][0] + (netX * Math.sin(h) + netY * Math.sin(h_n)) * vel;
double newZ = pos.v[2][0] + netZ * vel;

// The intended destination of the player.
Vec3 end = new Vec3(newX, newY, newZ);

pos = end;
}

/**
* Adjusts fields if the player presses a key.
*/
public static void pressKeys(int keyCode)
{
if (keyCode == KeyEvent.VK_SHIFT)
z2 = 1;
if (keyCode == KeyEvent.VK_W)
x2 = 1;
if (keyCode == KeyEvent.VK_A)
y2 = 1;
if (keyCode == KeyEvent.VK_S)
x1 = 1;
if (keyCode == KeyEvent.VK_D)
y1 = 1;
if (keyCode == KeyEvent.VK_SPACE)
z1 = 1;
}

/**
* Adjusts fields if the player releases a key.
*/
public static void releaseKeys(int keyCode)
{
if (keyCode == KeyEvent.VK_SHIFT)
z2 = 0;
if (keyCode == KeyEvent.VK_W)
x2 = 0;
if (keyCode == KeyEvent.VK_A)
y2 = 0;
if (keyCode == KeyEvent.VK_S)
x1 = 0;
if (keyCode == KeyEvent.VK_D)
y1 = 0;
if (keyCode == KeyEvent.VK_SPACE)
z1 = 0;
}
}
157 changes: 157 additions & 0 deletions Rotation.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
import java.awt.event.KeyEvent;

/**
* A series of static methods for drawing objects in 3D.
*
* @author Justin C
*/
public class Rotation
{
/**
* Camera speeds.
*/
public static final double V_CAM_SLOW = 0.02, V_CAM_FAST = 0.03, V_ZOOM = 5;

/**
* Rotational speed.
*/
public static final double V_ROTATE = 0.03;

/**
* near and far are the respective clipping planes.
*/
public static double near, far;

/**
* Heading and pitch govern player view.
*/
public static double heading, pitch;

/**
* Zoom is the zoom factor.
*/
public static double zoom = 1;

/**
* Stores the x, y, z radians of rotation for use by Models.
*/
public static double rX, rY, rZ;

/**
* Stores the x, y, z rotation speeds for use by Models.
*/
public static int vX, vY, vZ;

/**
* Don't let anyone instantiate this class.
*/
private Rotation()
{

};

/**
* Increments all rotational matrices by the appropriate amount.
*/
public static void tick()
{
rX += vX * V_ROTATE;
rY += vY * V_ROTATE;
rZ += vZ * V_ROTATE;
}

/**
* Manage the pitch and heading. left, right, up, down
*/
public static void slowL()
{
heading += V_CAM_SLOW;
}

public static void slowR()
{
heading -= V_CAM_SLOW;
}

public static void fastL()
{
heading += V_CAM_FAST;
}

public static void fastR()
{
heading -= V_CAM_FAST;
}

public static void slowU()
{
if (2*pitch < Math.PI)
pitch += V_CAM_SLOW;
}

public static void slowD()
{
if (2*pitch > -Math.PI)
pitch -= V_CAM_SLOW;
}

public static void fastU()
{
if (2*pitch < Math.PI)
pitch += V_CAM_FAST;
}

public static void fastD()
{
if (2*pitch > -Math.PI)
pitch -= V_CAM_FAST;
}

public static void scroll(int wheel)
{
if (near >= 0 || wheel >= 0)
near += V_ZOOM * wheel;
}

/**
* Resets constants.
*/
public static void reset()
{
near = 400;
far = 20000;
zoom = 1;
heading = 0;
pitch = 0;
rX = 0;
rY = 0;
rZ = 0;
vX = 0;
vY = 0;
vZ = 0;
}

/**
* Takes in a keyCode from a Virtual Keyboard key that has been released and
* adjusts the appropriate constants.
*/
public static void releaseKeys(int keyCode)
{
if (keyCode == KeyEvent.VK_O)
zoom /= 1.1;
if (keyCode == KeyEvent.VK_P)
zoom *= 1.1;
if (keyCode == KeyEvent.VK_F)
vZ++;
if (keyCode == KeyEvent.VK_G)
vZ--;
if (keyCode == KeyEvent.VK_H)
vY++;
if (keyCode == KeyEvent.VK_J)
vY--;
if (keyCode == KeyEvent.VK_K)
vX++;
if (keyCode == KeyEvent.VK_L)
vX--;
}
}
Loading

0 comments on commit 2cdd547

Please sign in to comment.