Advertisement
Guest User

Untitled

a guest
Mar 9th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.68 KB | None | 0 0
  1. package main;
  2.  
  3. import gfx.SpriteSheet;
  4.  
  5. import java.awt.BorderLayout;
  6. import java.awt.Canvas;
  7. import java.awt.Color;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.geom.AffineTransform;
  11. import java.awt.image.AffineTransformOp;
  12. import java.awt.image.BufferStrategy;
  13. import java.awt.image.BufferedImage;
  14. import java.awt.image.DataBufferInt;
  15. import java.io.IOException;
  16.  
  17. import javax.imageio.ImageIO;
  18. import javax.swing.JFrame;
  19.  
  20. public class trigTester extends Canvas implements Runnable {
  21. AffineTransformOp op;
  22. AffineTransform tx;
  23. AffineTransform tx1;
  24. double rotationRequired;
  25. double locationX;
  26. double locationY;
  27. public boolean stopMovingMiss;
  28. public int once = 0;
  29.  
  30. public float angle = 0;
  31. public float targAngle;
  32. public float angleStep = 0.1f;
  33. public int animInc;
  34.  
  35. public boolean running;
  36. public float targetX;
  37. public float targetY;
  38.  
  39. public float targXVel;
  40. public float targYVel;
  41.  
  42. public float missileX;
  43. public float missileY;
  44. public float missSpeed;
  45.  
  46. public float a;
  47. public float b;
  48. public float c;
  49. public float disc;
  50. public float t1;
  51. public float t2;
  52. public float t;
  53. public float aimX;
  54. public float aimY;
  55.  
  56. public float yDiff;
  57. public float xDiff;
  58.  
  59. public float yNorm;
  60. public float xNorm;
  61. public float hyp;
  62.  
  63. public float a1;
  64. public float b1;
  65. public float c1;
  66. public float disc1;
  67. public float towT1;
  68. public float towT2;
  69. public float towT;
  70. public float aimX1;
  71. public float aimY1;
  72. public float yTowDiff;
  73. public float xTowDiff;
  74.  
  75. public float yNorm1;
  76. public float xNorm1;
  77. public float hyp1;
  78.  
  79. public static final int WIDTH=200;//480;
  80. public static final int HEIGHT = 200;//480;//WIDTH/ 12 * 9;
  81. public static final int SCALE = 5;
  82. public static final String name= "tester";
  83.  
  84. public JFrame frame;
  85.  
  86.  
  87.  
  88. public BufferedImage tower = null;
  89. private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  90. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  91. private int[] towPixels;
  92.  
  93. public trigTester()
  94. {
  95. try
  96. {
  97. tower = ImageIO.read(SpriteSheet.class.getResource("/spriteSheet/testTower.png"));
  98. }
  99. catch(IOException e)
  100. {
  101. e.printStackTrace();
  102. }
  103. //towPixels = ((DataBufferInt)tower.getRaster().getDataBuffer()).getData();
  104. towPixels = image.getRGB(0, 0, tower.getWidth(), tower.getHeight(), null, 0, tower.getWidth());
  105. rotationRequired = Math.toRadians(45);
  106. locationX = tower.getWidth() / 2;
  107. locationY = tower.getHeight() / 2;
  108. tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
  109. tx1 = new AffineTransform();
  110. op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
  111.  
  112.  
  113. stopMovingMiss = false;
  114. running= false;
  115. targetX = 150;
  116. targetY = 5;
  117. targXVel = -1;
  118. targYVel = 0;
  119.  
  120. missileX = 70;
  121. missileY = 60;
  122. missSpeed = 2;
  123.  
  124. Dimension dimension= new Dimension(WIDTH*SCALE, HEIGHT*SCALE);
  125. setMinimumSize(dimension);
  126. setMaximumSize(dimension);
  127. setPreferredSize(dimension);
  128.  
  129. frame = new JFrame(name);
  130. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  131. frame.setLayout(new BorderLayout());
  132. frame.add(this, BorderLayout.CENTER);
  133. frame.setResizable(false);
  134.  
  135. frame.pack();
  136. frame.setLocationRelativeTo(null);
  137. frame.setVisible(true);
  138.  
  139. start();
  140. }
  141.  
  142.  
  143.  
  144.  
  145.  
  146. public void run()
  147. {
  148.  
  149. long lastTime = System.nanoTime();
  150. long nowTime;
  151. long delta;
  152. long incrementTime = 900000000;
  153. long lastTimer = System.nanoTime();
  154.  
  155. while(running)
  156. {
  157.  
  158. nowTime = System.nanoTime();
  159. delta = nowTime = lastTime;
  160. if (delta >= incrementTime)
  161. {
  162. tick();
  163. lastTime = nowTime;
  164. }
  165. render();
  166.  
  167. }
  168.  
  169. }
  170.  
  171.  
  172.  
  173.  
  174. public void start()
  175. {
  176. Thread start = new Thread(this);
  177. running = true;
  178. start.run();
  179.  
  180. }
  181.  
  182. public void tick()
  183. {
  184.  
  185.  
  186. if (animInc== 100)
  187. {
  188.  
  189. targetX+=targXVel;
  190. if (once == 0)
  191. {
  192. a = (targXVel* targXVel) + (targYVel * targYVel) - (missSpeed * missSpeed);
  193. b = 2 *(targXVel * (targetX - missileX) + targYVel * (targetY - missileY));
  194. c = (targetX - missileX) * (targetX - missileX) + (targetY - missileY)* (targetY - missileY);
  195.  
  196. //System.out.println("a:" + a + ", b: "+ b+ "c: " + c);
  197. disc= b*b - 4 * a* c;
  198. //System.out.println(disc);
  199.  
  200.  
  201. t1 = (-b + (float)Math.sqrt(disc))/ (2*a);
  202. t2 = (-b- (float)Math.sqrt(disc))/ (2*a);
  203. if (t1 > 0 && t1 < t2)t = t1;
  204. else if (t2 > 0 && t2 < t1) t = t2;
  205. else if (t1 <0 && t2 >0) t = t2;
  206. else if (t2 < 0 && t1 > 0)t = t1;
  207.  
  208.  
  209. //System.out.println("t:" + t);
  210.  
  211.  
  212.  
  213.  
  214.  
  215.  
  216. aimX = t * targXVel + targetX;
  217. aimY = t * targYVel + targetY;
  218. // System.out.println("t:" + t);
  219. //System.out.println(aimX + ":" + aimY);
  220.  
  221.  
  222. xDiff = aimX - missileX;
  223. yDiff = aimY - missileY;
  224.  
  225. hyp = (float)Math.sqrt(xDiff*xDiff + yDiff* yDiff);
  226. //System.out.println("hyp: "+ hyp);
  227. xNorm = xDiff/ hyp;
  228. yNorm = yDiff/hyp;
  229.  
  230. //System.out.println("yNorm:" + yNorm);
  231. once++;
  232.  
  233. }
  234. animInc =0;
  235.  
  236.  
  237.  
  238. if ((int)(targetX - missileX) !=0 && (int)(targetY - missileY)!=0)
  239. {
  240. missileX += missSpeed*xNorm;
  241. missileY += missSpeed*yNorm;
  242. // System.out.println("hello!");
  243. //missileX = (float)targetX;
  244. //missileY = (float)targetY;
  245.  
  246. }
  247.  
  248.  
  249. a1 = (targXVel* targXVel) + (targYVel * targYVel) - (missSpeed * missSpeed);
  250. b1 = 2 *(targXVel * (targetX - 70) + targYVel * (targetY - 60));
  251. c1 = (targetX - 70) * (targetX - 70) + (targetY - 60)* (targetY - 60);
  252.  
  253.  
  254. disc1= b1*b1 - 4 * a1* c1;
  255.  
  256.  
  257.  
  258.  
  259. towT1 = (-b1 + (float)Math.sqrt(disc1))/ (2*a1);
  260. towT2 = (-b1- (float)Math.sqrt(disc1))/ (2*a1);
  261. if (towT1 > 0 && towT1 < towT2)towT = towT1;
  262. else if (towT2 > 0 && towT2 < towT1) towT = towT2;
  263. else if (towT1 <0 && towT2 >0) towT = towT2;
  264. else if (towT2 < 0 && towT1 > 0)towT = towT1;
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.  
  272.  
  273. aimX1 = towT * targXVel + targetX;
  274. aimY1 = towT * targYVel + targetY;
  275. if (aimX1 >= WIDTH || aimY1 >= HEIGHT)
  276. {
  277. aimX1 = 0;
  278. aimY1 = 0;
  279. }
  280.  
  281.  
  282. xTowDiff = aimX1 -70;
  283. yTowDiff = aimY1 - 60;
  284. hyp1 = (float)Math.sqrt(xTowDiff*xTowDiff + yTowDiff* yTowDiff);
  285.  
  286. //rotationRequired = Math.asin(yTowDiff/hyp1);
  287. //System.out.println(rotationRequired);
  288.  
  289.  
  290.  
  291. rotationRequired = Math.asin(xTowDiff/hyp1);
  292.  
  293.  
  294.  
  295.  
  296. //rotationRequired+=.1;
  297. if (rotationRequired >= 3.14 *2) rotationRequired =0;
  298.  
  299. tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
  300. op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);
  301. //tx1 = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY);
  302. //op = new AffineTransformOp(tx1, AffineTransformOp.TYPE_BILINEAR);
  303. //tower = op.filter(tower, null);
  304. //towPixels = tower.getRGB(0, 0, tower.getWidth(), tower.getHeight(), null, 0, tower.getWidth());
  305.  
  306.  
  307.  
  308.  
  309. }
  310. else
  311. animInc++;
  312.  
  313.  
  314.  
  315. }
  316.  
  317. /*
  318. xDiff = targetX- missileX;
  319. yDiff = targetY- missileY;
  320. if ((int)xDiff!=0 && (int)yDiff!=0)
  321. {
  322.  
  323. hyp = (float)Math.sqrt(xDiff * xDiff + yDiff * yDiff);
  324. xNorm = xDiff/hyp;
  325. yNorm = yDiff/hyp;
  326.  
  327.  
  328.  
  329.  
  330. missileX += 3*xNorm;
  331. missileY += 3*yNorm;
  332.  
  333.  
  334. }
  335.  
  336. targetX++;
  337. animInc = 0;
  338.  
  339.  
  340.  
  341. }
  342. else animInc++;
  343. }
  344. */
  345.  
  346. public void clear()
  347. {
  348.  
  349. for (int i = 0; i < WIDTH*HEIGHT; i++)
  350. {
  351. pixels[i]= 0;
  352. }
  353. }
  354. public void render()
  355. {
  356. BufferStrategy bs = getBufferStrategy();
  357. if (bs == null)
  358. {
  359. createBufferStrategy(3);
  360. return;
  361. }
  362. clear();
  363.  
  364. Graphics g = bs.getDrawGraphics();
  365. Color c = Color.blue;
  366. int col = c.getRGB();
  367. pixels[(int)missileX + (int)missileY * WIDTH] = col;
  368. pixels[(int)targetX + (int)targetY * WIDTH] = col;
  369. //image.setRGB(targetX, 50, col);
  370. //System.out.println("missileX: " + missileX + "missileY: " + missileY);
  371. //image.setRGB((int)missileX, (int)missileY, col);
  372. //image.setRGB((int)targetX , (int)targetY, col);
  373. image.setRGB(70, 60, col);
  374.  
  375. g.drawImage(image, 0 , 0 ,getWidth(), getHeight(), null);
  376.  
  377. //g.drawImage(tower, 100, 100, 30, 30,null);
  378. g.drawRect(70* SCALE, 60* SCALE, 9, 9);
  379. /*
  380. for (int i = 0; i < 30; i++){
  381. for (int j = 0; j < 30; j++){
  382. System.out.println("i: "+ (i+ 70) + " j: " + (j+ 60));
  383. pixels[j + i*WIDTH] = towPixels[j + i *tower.getWidth()];
  384.  
  385. }
  386.  
  387. }
  388. */
  389.  
  390.  
  391. g.drawImage(op.filter(tower, null), 70*SCALE-15, 60*SCALE-15, null);
  392.  
  393.  
  394. g.dispose();
  395. bs.show();
  396.  
  397.  
  398.  
  399. }
  400.  
  401. public static void main(String args[])
  402. {
  403. trigTester test = new trigTester();
  404.  
  405. }
  406. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement