Advertisement
Guest User

Quick Proof, this is the panel and the main

a guest
Apr 24th, 2015
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.74 KB | None | 0 0
  1. import java.awt.Color;
  2. import java.awt.Graphics;
  3. import java.awt.Point;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.awt.event.MouseEvent;
  7. import java.awt.event.MouseMotionListener;
  8.  
  9.  
  10.  
  11.  
  12. import java.awt.image.BufferedImage;
  13.  
  14. import javax.swing.JFrame;
  15. import javax.swing.JPanel;
  16. import javax.swing.Timer;
  17. public class QuickProof extends JPanel{
  18. public int delay = 50;
  19. public Point3d[] nodes = new Point3d[8];
  20.  
  21. public Point3d camPos = new Point3d(-3,-3,-15);
  22. public double[] camRot = {0,0,0};//y x spin_not_used
  23. public double fov = Math.PI/2;
  24.  
  25. public static int Width = 1000;
  26. public static int Height = 800;
  27. public double speed = .1;
  28.  
  29. public static JFrameKeys frame;
  30.  
  31. public QuickProof() {
  32.  
  33. nodes[0] = new Point3d(-1,-1,1);
  34. nodes[1] = new Point3d(-1,1,1);
  35. nodes[2] = new Point3d(1,1,1);
  36. nodes[3] = new Point3d(1,-1,1);
  37. nodes[4] = new Point3d(-1,-1,-1);
  38. nodes[5] = new Point3d(-1,1,-1);
  39. nodes[6] = new Point3d(1,1,-1);
  40. nodes[7] = new Point3d(1,-1,-1);
  41.  
  42.  
  43.  
  44.  
  45. repaint();
  46. ActionListener counter = new ActionListener() {
  47. public void actionPerformed(ActionEvent evt) {
  48. repaint();
  49. camRot[0] += frame.getMX()*Height/fov/20000;
  50. camRot[1] -= frame.getMY()*Width/fov/20000;
  51. if(camRot[1] < -1){
  52. camRot[1] = -1;
  53. }
  54. else if(camRot[1] > 1)
  55. camRot[1] = 1;
  56.  
  57. if(camRot[0] > Math.PI)
  58. camRot[0] = -Math.PI;
  59. else if(camRot[0] < -Math.PI){
  60. camRot[0] = Math.PI;
  61. }
  62. frame.recenterMouse();
  63. if(frame.last != ""){
  64. //System.out.println(frame.last);
  65.  
  66. if(frame.last.indexOf("w") >= 0)
  67. go("forward");
  68. if(frame.last.indexOf("a") >= 0)
  69. go("left");
  70. if(frame.last.indexOf("s") >= 0)
  71. go("back");
  72. if(frame.last.indexOf("d") >= 0)
  73. go("right");
  74. if(frame.last.indexOf("q") >= 0)
  75. go("up");
  76. if(frame.last.indexOf("e") >= 0)
  77. go("down");
  78. if(frame.last.indexOf("x") >= 0){
  79. setVisible(false);
  80. frame.dispose(); //Destroy the JFrame
  81. System.exit(0); //bye
  82. }
  83. if(frame.last.indexOf("t") >= 0)
  84. System.out.println("0: " + camRot[0] + " 1:" + camRot[1] + " 2:" + camRot[2]);
  85.  
  86. }
  87.  
  88.  
  89.  
  90.  
  91. }
  92.  
  93.  
  94. };
  95. new Timer(1, counter).start();
  96. }
  97.  
  98. public void go(String string) {
  99. double xDist,yDist,zDist;
  100.  
  101. //Ok this is my own algorithm lets try it\
  102. //first calculate Y
  103. double y = this.speed * Math.sin(camRot[1]*Math.PI);
  104. //from that we can calculate x and z
  105. double x = Math.sin(camRot[1]*4) * this.speed * Math.cos(camRot[0]*Math.PI/2);
  106. double z = Math.cos(camRot[1]*4) * this.speed * Math.cos(camRot[0]*Math.PI/2);
  107. y*=-1;
  108.  
  109.  
  110. if(string == "forward"){
  111. if(camRot[0] > 0)
  112. camPos.x += x;
  113. else
  114. camPos.x -= x;
  115.  
  116. camPos.y += y;
  117.  
  118. camPos.z += z;
  119. }
  120.  
  121. if(string == "back"){
  122. if(camRot[0] > 0)
  123. camPos.x -= x;
  124. else
  125. camPos.x += x;
  126. if(camRot[1] > 0)
  127. camPos.y -= y;
  128. else
  129. camPos.y += y;
  130. camPos.z -= z;
  131. }
  132.  
  133. }
  134.  
  135. public void paintComponent(Graphics g) {
  136. super.paintComponent(g);
  137. paintbackGround(g);
  138. paintNodes(g);
  139.  
  140.  
  141. }
  142. private void paintbackGround(Graphics g) {
  143. for(int i = 0; i<15;i++){
  144. for(int n = 0; n<15;n++){
  145. g.setColor(Color.GRAY);
  146. g.fillRect(i*100, n*100, 50, 50);
  147. g.fillRect(i*100+50, n*100+50, 50, 50);
  148. g.setColor(Color.white);
  149. g.fillRect(i*100+50, n*100, 50, 50);
  150. g.fillRect(i*100, n*100+50, 50, 50);
  151. }
  152. }
  153. g.setColor(Color.black);
  154.  
  155. }
  156.  
  157. private void paintNodes(Graphics g) {
  158. Point[] points = new Point[8];
  159. boolean a = false;
  160. for(int i = 0; i<nodes.length; i++){
  161. Point3d p = projection(nodes[i]);
  162. //g.fillOval(projection.x, projection.y, 5, 5);
  163. //g.drawString(i + "", projection.x, projection.y);
  164. Point projection = new Point();
  165. projection.x = (int) p.x;
  166. projection.y = (int) p.y;
  167. points[i] = projection;
  168.  
  169. if(p.x > 0 && p.x < 1000 && p.y > 0 && p.y < 800 && p.z>0){
  170. a = true;
  171. }
  172.  
  173. }
  174.  
  175. if(!a){return;}
  176.  
  177. int[] xPoints1 = new int[4];
  178. int[] yPoints1 = new int[4];
  179. int[] xPoints2 = new int[4];
  180. int[] yPoints2 = new int[4];
  181. int[] xPoints3 = new int[4];
  182. int[] yPoints3 = new int[4];
  183. g.setColor(Color.CYAN);
  184. xPoints1[0] = points[5].x;
  185. yPoints1[0] = points[5].y;
  186. xPoints1[1] = points[1].x;
  187. yPoints1[1] = points[1].y;
  188. xPoints1[2] = points[2].x;
  189. yPoints1[2] = points[2].y;
  190. xPoints1[3] = points[6].x;
  191. yPoints1[3] = points[6].y;
  192. g.fillPolygon(xPoints1, yPoints1, 4);
  193. g.setColor(Color.BLUE);
  194. xPoints2[0] = points[0].x;
  195. yPoints2[0] = points[0].y;
  196. xPoints2[1] = points[1].x;
  197. yPoints2[1] = points[1].y;
  198. xPoints2[2] = points[2].x;
  199. yPoints2[2] = points[2].y;
  200. xPoints2[3] = points[3].x;
  201. yPoints2[3] = points[3].y;
  202. g.fillPolygon(xPoints2, yPoints2, 4);
  203. g.setColor(Color.GREEN);
  204. xPoints3[0] = points[7].x;
  205. yPoints3[0] = points[7].y;
  206. xPoints3[1] = points[6].x;
  207. yPoints3[1] = points[6].y;
  208. xPoints3[2] = points[2].x;
  209. yPoints3[2] = points[2].y;
  210. xPoints3[3] = points[3].x;
  211. yPoints3[3] = points[3].y;
  212. g.fillPolygon(xPoints3, yPoints3, 4);
  213. g.setColor(Color.black);
  214. g.drawLine(points[0].x, points[0].y, points[4].x, points[4].y);
  215. g.drawLine(points[0].x, points[0].y, points[3].x, points[3].y);
  216. g.drawLine(points[0].x, points[0].y, points[1].x, points[1].y);
  217. g.drawLine(points[4].x, points[4].y, points[7].x, points[7].y);
  218. g.drawLine(points[6].x, points[6].y, points[5].x, points[5].y);
  219. g.drawLine(points[7].x, points[7].y, points[6].x, points[6].y);
  220. g.drawLine(points[4].x, points[4].y, points[5].x, points[5].y);
  221. g.drawLine(points[3].x, points[3].y, points[2].x, points[2].y);
  222. g.drawLine(points[2].x, points[2].y, points[1].x, points[1].y);
  223. g.drawLine(points[7].x, points[7].y, points[3].x, points[3].y);
  224. g.drawLine(points[6].x, points[6].y, points[2].x, points[2].y);
  225. g.drawLine(points[5].x, points[5].y, points[1].x, points[1].y);
  226.  
  227.  
  228. }
  229. private Point3d projection(Point3d point) {
  230. Point3d point3d = new Point3d(point.x,point.y,point.z);
  231.  
  232. //first, apply cam rotation to the point
  233.  
  234. //double temp = point3d.z;
  235. //point3d.z = -point3d.x * Math.sin(camRot[2]) - point3d.z * Math.cos(camRot[2]);
  236. //point3d.x = -point3d.x * Math.cos(camRot[2]) + temp * Math.sin(camRot[2]);
  237. //point3d.z = -point3d.y * Math.sin(camRot[0]) + camRot[1] * Math.cos(camRot[0]);
  238. //point3d.y = point3d.y * Math.cos(camRot[0]) + temp * Math.sin(camRot[0]);
  239.  
  240. //temp = point3d.x;
  241. //point3d.x = point3d.x * Math.cos(camRot[1]) - point3d.y * Math.sin(camRot[1]);
  242. //point3d.y = point3d.y * Math.cos(camRot[1]) + temp * Math.sin(camRot[1]);
  243.  
  244. // Apply camera translation after the rotation, so we are actually just rotating the object
  245. //point3d.x -= camPos.x;
  246. //point3d.y -= camPos.y;
  247. //point3d.z -= camPos.z;
  248. Point3d d = new Point3d();
  249. 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);
  250. 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));
  251. 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));
  252. // projecting from 3D to 2D
  253. Point3d point2d = new Point3d();
  254. //point2d.x = (int) ((RatioConst * (point3d.x)) / point3d.z) + 100;
  255. //point2d.y = (int) ((RatioConst * (point3d.y)) / point3d.z) + 100;\
  256. double ez = 1 / Math.tan(fov/2);
  257. double x = (d.x-.5)*(ez/d.z);
  258. double y = (d.y-.5)*(ez/d.z);
  259.  
  260. //System.out.println(x);
  261. point2d.x = (int) (x * Width);
  262. point2d.y = (int) (y*Height);
  263. point2d.z = d.z;
  264.  
  265. //System.out.println(point2d);
  266. return point2d;
  267.  
  268. }
  269. public static void main(String[] args){
  270. frame = new JFrameKeys("Proof Of Concept");
  271. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  272. frame.add(new QuickProof());
  273. frame.setSize(Width, Height);
  274. frame.setVisible(true);
  275.  
  276. frame.setCursor(frame.getToolkit().createCustomCursor(
  277. new BufferedImage(3, 3, BufferedImage.TYPE_INT_ARGB), new Point(0, 0),
  278. "null"));
  279.  
  280.  
  281. }
  282.  
  283.  
  284.  
  285.  
  286.  
  287.  
  288.  
  289.  
  290.  
  291.  
  292. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement