Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.Color;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseMotionListener;
- import java.awt.image.BufferedImage;
- import javax.swing.JFrame;
- import javax.swing.JPanel;
- import javax.swing.Timer;
- public class QuickProof extends JPanel{
- public int delay = 50;
- public Point3d[] nodes = new Point3d[8];
- public Point3d camPos = new Point3d(-3,-3,-15);
- public double[] camRot = {0,0,0};//y x spin_not_used
- public double fov = Math.PI/2;
- public static int Width = 1000;
- public static int Height = 800;
- public double speed = .1;
- public static JFrameKeys frame;
- public QuickProof() {
- nodes[0] = new Point3d(-1,-1,1);
- nodes[1] = new Point3d(-1,1,1);
- nodes[2] = new Point3d(1,1,1);
- nodes[3] = new Point3d(1,-1,1);
- nodes[4] = new Point3d(-1,-1,-1);
- nodes[5] = new Point3d(-1,1,-1);
- nodes[6] = new Point3d(1,1,-1);
- nodes[7] = new Point3d(1,-1,-1);
- repaint();
- ActionListener counter = new ActionListener() {
- public void actionPerformed(ActionEvent evt) {
- repaint();
- camRot[0] += frame.getMX()*Height/fov/20000;
- camRot[1] -= frame.getMY()*Width/fov/20000;
- if(camRot[1] < -1){
- camRot[1] = -1;
- }
- else if(camRot[1] > 1)
- camRot[1] = 1;
- if(camRot[0] > Math.PI)
- camRot[0] = -Math.PI;
- else if(camRot[0] < -Math.PI){
- camRot[0] = Math.PI;
- }
- frame.recenterMouse();
- if(frame.last != ""){
- //System.out.println(frame.last);
- if(frame.last.indexOf("w") >= 0)
- go("forward");
- if(frame.last.indexOf("a") >= 0)
- go("left");
- if(frame.last.indexOf("s") >= 0)
- go("back");
- if(frame.last.indexOf("d") >= 0)
- go("right");
- if(frame.last.indexOf("q") >= 0)
- go("up");
- if(frame.last.indexOf("e") >= 0)
- go("down");
- if(frame.last.indexOf("x") >= 0){
- setVisible(false);
- frame.dispose(); //Destroy the JFrame
- System.exit(0); //bye
- }
- if(frame.last.indexOf("t") >= 0)
- System.out.println("0: " + camRot[0] + " 1:" + camRot[1] + " 2:" + camRot[2]);
- }
- }
- };
- new Timer(1, counter).start();
- }
- public void go(String string) {
- double xDist,yDist,zDist;
- //Ok this is my own algorithm lets try it\
- //first calculate Y
- double y = this.speed * Math.sin(camRot[1]*Math.PI);
- //from that we can calculate x and z
- double x = Math.sin(camRot[1]*4) * this.speed * Math.cos(camRot[0]*Math.PI/2);
- double z = Math.cos(camRot[1]*4) * this.speed * Math.cos(camRot[0]*Math.PI/2);
- y*=-1;
- if(string == "forward"){
- if(camRot[0] > 0)
- camPos.x += x;
- else
- camPos.x -= x;
- camPos.y += y;
- camPos.z += z;
- }
- if(string == "back"){
- if(camRot[0] > 0)
- camPos.x -= x;
- else
- camPos.x += x;
- if(camRot[1] > 0)
- camPos.y -= y;
- else
- camPos.y += y;
- camPos.z -= z;
- }
- }
- public void paintComponent(Graphics g) {
- super.paintComponent(g);
- paintbackGround(g);
- paintNodes(g);
- }
- private void paintbackGround(Graphics g) {
- for(int i = 0; i<15;i++){
- for(int n = 0; n<15;n++){
- g.setColor(Color.GRAY);
- g.fillRect(i*100, n*100, 50, 50);
- g.fillRect(i*100+50, n*100+50, 50, 50);
- g.setColor(Color.white);
- g.fillRect(i*100+50, n*100, 50, 50);
- g.fillRect(i*100, n*100+50, 50, 50);
- }
- }
- g.setColor(Color.black);
- }
- private void paintNodes(Graphics g) {
- Point[] points = new Point[8];
- boolean a = false;
- for(int i = 0; i<nodes.length; i++){
- Point3d p = projection(nodes[i]);
- //g.fillOval(projection.x, projection.y, 5, 5);
- //g.drawString(i + "", projection.x, projection.y);
- Point projection = new Point();
- projection.x = (int) p.x;
- projection.y = (int) p.y;
- points[i] = projection;
- if(p.x > 0 && p.x < 1000 && p.y > 0 && p.y < 800 && p.z>0){
- a = true;
- }
- }
- if(!a){return;}
- int[] xPoints1 = new int[4];
- int[] yPoints1 = new int[4];
- int[] xPoints2 = new int[4];
- int[] yPoints2 = new int[4];
- int[] xPoints3 = new int[4];
- int[] yPoints3 = new int[4];
- g.setColor(Color.CYAN);
- xPoints1[0] = points[5].x;
- yPoints1[0] = points[5].y;
- xPoints1[1] = points[1].x;
- yPoints1[1] = points[1].y;
- xPoints1[2] = points[2].x;
- yPoints1[2] = points[2].y;
- xPoints1[3] = points[6].x;
- yPoints1[3] = points[6].y;
- g.fillPolygon(xPoints1, yPoints1, 4);
- g.setColor(Color.BLUE);
- xPoints2[0] = points[0].x;
- yPoints2[0] = points[0].y;
- xPoints2[1] = points[1].x;
- yPoints2[1] = points[1].y;
- xPoints2[2] = points[2].x;
- yPoints2[2] = points[2].y;
- xPoints2[3] = points[3].x;
- yPoints2[3] = points[3].y;
- g.fillPolygon(xPoints2, yPoints2, 4);
- g.setColor(Color.GREEN);
- xPoints3[0] = points[7].x;
- yPoints3[0] = points[7].y;
- xPoints3[1] = points[6].x;
- yPoints3[1] = points[6].y;
- xPoints3[2] = points[2].x;
- yPoints3[2] = points[2].y;
- xPoints3[3] = points[3].x;
- yPoints3[3] = points[3].y;
- g.fillPolygon(xPoints3, yPoints3, 4);
- g.setColor(Color.black);
- g.drawLine(points[0].x, points[0].y, points[4].x, points[4].y);
- g.drawLine(points[0].x, points[0].y, points[3].x, points[3].y);
- g.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
- g.drawLine(points[4].x, points[4].y, points[7].x, points[7].y);
- g.drawLine(points[6].x, points[6].y, points[5].x, points[5].y);
- g.drawLine(points[7].x, points[7].y, points[6].x, points[6].y);
- g.drawLine(points[4].x, points[4].y, points[5].x, points[5].y);
- g.drawLine(points[3].x, points[3].y, points[2].x, points[2].y);
- g.drawLine(points[2].x, points[2].y, points[1].x, points[1].y);
- g.drawLine(points[7].x, points[7].y, points[3].x, points[3].y);
- g.drawLine(points[6].x, points[6].y, points[2].x, points[2].y);
- g.drawLine(points[5].x, points[5].y, points[1].x, points[1].y);
- }
- private Point3d projection(Point3d point) {
- Point3d point3d = new Point3d(point.x,point.y,point.z);
- //first, apply cam rotation to the point
- //double temp = point3d.z;
- //point3d.z = -point3d.x * Math.sin(camRot[2]) - point3d.z * Math.cos(camRot[2]);
- //point3d.x = -point3d.x * Math.cos(camRot[2]) + temp * Math.sin(camRot[2]);
- //point3d.z = -point3d.y * Math.sin(camRot[0]) + camRot[1] * Math.cos(camRot[0]);
- //point3d.y = point3d.y * Math.cos(camRot[0]) + temp * Math.sin(camRot[0]);
- //temp = point3d.x;
- //point3d.x = point3d.x * Math.cos(camRot[1]) - point3d.y * Math.sin(camRot[1]);
- //point3d.y = point3d.y * Math.cos(camRot[1]) + temp * Math.sin(camRot[1]);
- // Apply camera translation after the rotation, so we are actually just rotating the object
- //point3d.x -= camPos.x;
- //point3d.y -= camPos.y;
- //point3d.z -= camPos.z;
- Point3d d = new Point3d();
- d.x = Math.cos(camRot[0]) * (Math.sin(camRot[2]) * (point3d.y-camPos.y) + Math.cos(camRot[2]) * (point3d.x-camPos.x)) - Math.sin(camRot[0]) * (point3d.z-camPos.z);
- d.y = Math.sin(camRot[1]) * (Math.cos(camRot[0]) * (point3d.z-camPos.z) + Math.sin(camRot[0]) * (Math.sin(camRot[2]) * (point3d.y - camPos.y) + Math.cos(camRot[2]) * (point3d.x-camPos.x))) + Math.cos(camRot[1])*(Math.cos(camRot[2]) * (point3d.y - camPos.y) - Math.sin(camRot[2]) * (point3d.x-camPos.x));
- d.z = Math.cos(camRot[1]) * (Math.cos(camRot[0]) * (point3d.z-camPos.z) + Math.sin(camRot[0]) * (Math.sin(camRot[2]) * (point3d.y - camPos.y) + Math.cos(camRot[2]) * (point3d.x-camPos.x))) - Math.sin(camRot[1])*(Math.cos(camRot[2]) * (point3d.y - camPos.y) - Math.sin(camRot[2]) * (point3d.x-camPos.x));
- // projecting from 3D to 2D
- Point3d point2d = new Point3d();
- //point2d.x = (int) ((RatioConst * (point3d.x)) / point3d.z) + 100;
- //point2d.y = (int) ((RatioConst * (point3d.y)) / point3d.z) + 100;\
- double ez = 1 / Math.tan(fov/2);
- double x = (d.x-.5)*(ez/d.z);
- double y = (d.y-.5)*(ez/d.z);
- //System.out.println(x);
- point2d.x = (int) (x * Width);
- point2d.y = (int) (y*Height);
- point2d.z = d.z;
- //System.out.println(point2d);
- return point2d;
- }
- public static void main(String[] args){
- frame = new JFrameKeys("Proof Of Concept");
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.add(new QuickProof());
- frame.setSize(Width, Height);
- frame.setVisible(true);
- frame.setCursor(frame.getToolkit().createCustomCursor(
- new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB), new Point(0, 0),
- "null"));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement