Implementation of:
- Matrix multiplication
- 3D projection + a fix for points behind the camera
1 parent e32b437 commit f9dc322893cb98cf7afb0681d3a64265e489725a
@cory cory authored on 6 Nov 2022
Showing 6 changed files
View
10
src/Line.java
nextPix();
try {
img.setRGB(returnVal[0], returnVal[1], Color.HSBtoRGB(1, 1, 1));
}
catch (ArrayIndexOutOfBoundsException ref){
System.out.println(returnVal[0] + " " + returnVal[1]);
catch (ArrayIndexOutOfBoundsException ignored){
// System.out.println(returnVal[0] + " " + returnVal[1]);
}
}
} else {
for (int i = 0; i <= dy; i++){
nextPix();
try {
img.setRGB(returnVal[0], returnVal[1], Color.HSBtoRGB(.5f, 1, 1));
}
catch (ArrayIndexOutOfBoundsException ref){
System.out.println(returnVal[0] + " " + returnVal[1]);
catch (ArrayIndexOutOfBoundsException ignored){
// System.out.println(returnVal[0] + " " + returnVal[1]);
}
}
}
/*
return returnVal;
}
else {
is_initialised = false;
return null;
throw new RuntimeException("Accessed too many line pixels");
}
iteratorVal += 1;
return returnVal;
}
View
64
src/Matrix.java 0 → 100644
public class Matrix {
protected int x;
protected int y;
private double[][] items;
 
public Matrix(int _x, int _y){
x = _x; y= _y;
items = new double[y][x];
}
public double item(int _x, int _y){
return items[_y][_x];
}
public void setItem(int _x, int _y, double val){
items[_y][_x] = val;
}
public double[][] getItems() {
return items;
}
public void setItems(double[][] newItems){
items = newItems;
}
public Matrix multiply(Matrix mult) {
Matrix result = new Matrix(mult.x, this.y);
double newItem;
if(x==mult.y){
for(int rx = 0; rx< result.x; rx+=1){
for(int ry = 0; ry< result.y; ry+=1){
newItem = 0;
for(int i = 0; i<x; i+=1){
newItem += this.item(i, ry)*mult.item(rx, i);
}
result.setItem(rx, ry, newItem);
}}
} else {
throw new RuntimeException("wrong dimensions");
}
return result;
}
public Point3D multiplyPoint3(Point3D point) {
double px;double py; double pz;
if(x==3){
px = point.x * item(0,0) + point.y*item(1,0) + point.z*item(2,0);
py = point.x * item(0,1) + point.y*item(1,1) + point.z*item(2,1);
pz = point.x * item(0,2) + point.y*item(1,2) + point.z*item(2,2);
} else if(x == 4){
px = point.x * item(0,0) + point.y*item(1,0) + point.z*item(2,0) + item(3,0);
py = point.x * item(0,1) + point.y*item(1,1) + point.z*item(2,1) + item(3,1);
pz = point.x * item(0,2) + point.y*item(1,2) + point.z*item(2,2) + item(3,2);
} else {throw new RuntimeException("wrong-dimensions");}
return new Point3D(px, py, pz);
}
public Point2D multiplyPoint2(Point3D point) throws Exception {
double px;double py;
if(x==2){
px = point.x * item(0,0) + point.y*item(0,1);
py = point.x * item(1,0) + point.y*item(1,1);
} else if(x == 3){
px = point.x * item(0,0) + point.y*item(0,1) + item(0,2);
py = point.x * item(1,0) + point.y*item(1,1) + item(1,2);
} else {throw new Exception("wrong-dimensions");}
return new Point2D(px, py);
}
}
View
118
src/Player.java
import sun.awt.motif.X11CNS11643;
 
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
 
// image that represents the player's position on the board
private BufferedImage image;
// current position of the player on the board grid
private Point3D position;
private Point3D rotation;
 
public Matrix camMatrix;
public double Fpdis = 0.4;
private Point3D position = new Point3D(-30,0,0);
private Point3D rotation = new Point3D(0,Math.PI / 2, 0);
private Point3D direction = new Point3D(0,0,0);
public double mouseSensitivity = 0.005; // radians per pixel
// called when the player is initialised
public Player() {
}
 
g.drawImage(img, 0, 0, observer);
}
 
public void keyPressed(int key) {
if (key == KeyEvent.VK_UP) {
}
if (key == KeyEvent.VK_RIGHT) {
}
if (key == KeyEvent.VK_DOWN) {
}
if (key == KeyEvent.VK_LEFT) {
if (key == KeyEvent.VK_W) {
direction.x = 1;
} else if (key == KeyEvent.VK_S) {
direction.x = -1;
} else if (key == KeyEvent.VK_A) {
direction.y = -1;
} else if (key == KeyEvent.VK_D) {
direction.y = 1;
} else if (key == KeyEvent.VK_SPACE) {
direction.z = 1;
} else if (key == KeyEvent.VK_SHIFT) {
direction.z = -1;
}
}
public void keyReleased(int key){
 
if (key == KeyEvent.VK_W) {
direction.x = 0;
} else if (key == KeyEvent.VK_S) {
direction.x = 0;
} else if (key == KeyEvent.VK_A) {
direction.y = 0;
} else if (key == KeyEvent.VK_D) {
direction.y = 0;
} else if (key == KeyEvent.VK_SPACE) {
direction.z = 0;
} else if (key == KeyEvent.VK_SHIFT) {
direction.z = 0;
}
}
public void tick(Point2D mouseRel) {
// System.out.println(mouseRel.x + " " + mouseRel.y);
// gets called once every tick, before the repainting process happens.
// change rotation depending on mouse movement
rotation.z += mouseRel.x * mouseSensitivity;
rotation.y += mouseRel.y * mouseSensitivity;
// calculate direction vector to translate position by
Matrix zMat = new Matrix(3, 3);
zMat.setItems(new double[][]{
{Math.cos(rotation.z), Math.sin(rotation.z), 0},
{-Math.sin(rotation.z), Math.cos(rotation.z), 0},
{0, 0, 1}}
);
Matrix yMat = new Matrix(3, 3);
yMat.setItems(new double[][]{
{Math.cos(rotation.y), 0, -Math.sin(rotation.y)},
{0, 1, 0 },
{Math.sin(rotation.y), 0, Math.cos(rotation.y) }}
);
Matrix xMat = new Matrix(3, 3);
xMat.setItems(new double[][]{
{1, 0, 0 },
{0, Math.cos(rotation.x), Math.sin(rotation.x)},
{0, -Math.sin(rotation.x), Math.cos(rotation.x)}}
);
Matrix izMat = new Matrix(3, 3);
izMat.setItems(new double[][]{
{Math.cos(-rotation.z), Math.sin(-rotation.z), 0},
{-Math.sin(-rotation.z), Math.cos(-rotation.z), 0},
{0, 0, 1}}
);
// apply zmat to direction vector
try {
Point3D dirvec = izMat.multiplyPoint3(direction);
position.translate(dirvec);
} catch (Exception e) {
throw new RuntimeException(e);
}
Matrix traMat = new Matrix(4, 3);
traMat.setItems(new double[][]
{{1, 0, 0, -position.x},
{0, 1, 0, -position.y},
{0, 0, 1, -position.z}}
);
// multiply out camera matrix
try {
//camMatrix = ((zMat.multiply(yMat)).multiply(xMat)).multiply(traMat);
// camMatrix = zMat.multiply(yMat);
// camMatrix = camMatrix.multiply(xMat);
// camMatrix = camMatrix.multiply(traMat);
camMatrix = xMat.multiply(yMat);
camMatrix = camMatrix.multiply(zMat);
camMatrix = camMatrix.multiply(traMat);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
 
// returnValue Functions
public Point3D getPos() {
View
17
src/Point3D.java
public class Point3D {
private double x;
private double y;
private double z;
public double x;
public double y;
public double z;
 
public Point3D(double _x, double _y, double _z){
x = _x;
y = _y;
x = _new[0];
y = _new[1];
z = _new[2];
}
public void translate(Point3D trVec){
x += trVec.x;
y += trVec.y;
z += trVec.z;
}
public Point2D project(double fpdis, int scrX, int scrY){
return new Point2D(
(((fpdis*y)/z + .5)*scrX),
(((fpdis*x)/z + .5)*scrY)
);
}
public double[] get(){
return new double[]{x, y, z};
}
}
View
src/Screen.java
View
src/Triangle.java