Welcome, Guest. Please login or register.
Did you miss your activation email?
Your Ad Here
Pages: [1]   Go Down
  Print  
Author Topic: Java 2d collision detection  (Read 5093 times)
0 Members and 1 Guest are viewing this topic.
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2552
Referrals: 12
Activity
14%

WWW Awards
« on: August 14, 2008, 09:59:39 PM »

[Update]
Watch the video here: http://www.engineeringser...collision-detection-demo/



A Java 2d collision detection demo that I wrote earlier today for a game that i'm planning to make.

Screenshot


Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. SpriteDemoJFrame class
  3.  *****************
  4. By: deAppel http://www.engineeringserver.com - http://www.javaforums.net
  5. Contact: info [@] engineeringserver.com
  6. Version: 15/August/2008
  7. Last updated: 15/August/2008
  8.  
  9.  
  10.  *****************
  11. Note: a demo that shows collision detection using rectangles.
  12. //----------------------------------------------------------------------*/
  13. import java.awt.Color;
  14. import java.awt.event.MouseEvent;
  15. import java.awt.event.MouseMotionListener;
  16.  
  17. import javax.swing.BorderFactory;
  18. import javax.swing.JFrame;
  19.  
  20. public class SpriteDemoJFrame extends JFrame  implements MouseMotionListener, Runnable{
  21. SpriteDemo SD = new SpriteDemo();
  22. int sizeX = 500;
  23. int sizeY = 500;
  24. public SpriteDemoJFrame(){
  25. setLayout(null);
  26. SD.setPlayingField(sizeX,sizeY);
  27. SD.setBorder(BorderFactory.createLineBorder(Color.BLACK));
  28. SD.setSize(sizeX,sizeY);
  29. add(SD);
  30.  
  31. setTitle("Collision detection demo");
  32. setDefaultCloseOperation(3);
  33. setSize(800,600);
  34. setVisible(true);
  35. }
  36.  
  37.  
  38. public static void main(String[] args) {
  39. SpriteDemoJFrame SDF = new SpriteDemoJFrame();
  40. SDF.run();
  41. }
  42.  
  43. public void mouseDragged(MouseEvent e) {
  44. }
  45.  
  46. public void run() {
  47. while(true){
  48. Thread t = new Thread();
  49. try {
  50. t.sleep(50);
  51. SD.moveRectangle();
  52. } catch (InterruptedException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }
  57.  
  58. public void mouseMoved(MouseEvent e) { }
  59.  
  60. }
  61.  
Created by GeSHI 1.0.7.20

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. SpriteDemoJFrame class
  3. *****************
  4. By: deAppel http://www.engineeringserver.com - http://www.javaforums.net
  5. Contact: info [@] engineeringserver.com
  6. Version: 15/August/2008
  7. Last updated: 15/August/2008
  8.  
  9.  
  10. *****************
  11. Note: a demo that shows collision detection using rectangles.
  12. //----------------------------------------------------------------------*/
  13. import java.awt.Color;
  14. import java.awt.Font;
  15. import java.awt.Graphics;
  16. import java.awt.Point;
  17. import java.awt.Rectangle;
  18. import java.awt.event.MouseEvent;
  19. import java.awt.event.MouseMotionListener;
  20.  
  21. import javax.swing.JPanel;
  22.  
  23.  
  24. public class SpriteDemo extends JPanel  implements MouseMotionListener{
  25. int positionX = 0;
  26. int positionY = 0;
  27. int positionMovingX = 0;
  28. int positionMovingY = 0;
  29. boolean flip = false;
  30. boolean gotHit = false;
  31. boolean debug = false;
  32. int playFieldX = 0;
  33. int playFieldY = 0;
  34. int movement = 0;
  35. Point x;
  36. int speed;
  37. int size = 10;
  38. int hit = 0;
  39. Rectangle rect = new Rectangle(positionMovingX,positionMovingY,size,size);
  40. Rectangle rect2 = new Rectangle(100,100,40,40);
  41.  
  42. public SpriteDemo(){
  43. addMouseMotionListener(this);
  44. }
  45.  
  46. public void paintComponent(Graphics g){
  47. super.paintComponent(g);
  48. if(debug){
  49. System.out.println(positionMovingX +  "   " + positionMovingY +  "  " + hit);
  50. }
  51. rect2.setLocation(positionX, positionY);
  52.  
  53. g.setColor(Color.GREEN);
  54. g.fillRect(rect.x,rect.y,rect.width,rect.height);
  55.  
  56. g.setColor(Color.WHITE);
  57. g.fillRect(positionX, positionY,rect2.width,rect2.width);
  58.  
  59. g.setColor(Color.BLACK);
  60. g.setFont(new Font("Arial",Font.BOLD,16));
  61.  
  62. g.drawString("You got hit " + hit + " times", 20, 20);
  63.  
  64. }
  65. public static void main(String[] args) {
  66. }
  67.  
  68. public void mouseDragged(MouseEvent e) {
  69. positionX = e.getX()-20;
  70. positionY = e.getY()-20;
  71. repaint();
  72. }
  73.  
  74. public void mouseMoved(MouseEvent e) {
  75. }
  76.  
  77. public void moveRectangle(){
  78. speed = (int) (5+Math.random()*20);
  79.  
  80. if(flip == false){
  81. positionMovingX = positionMovingX+speed;
  82. positionMovingY = positionMovingY+speed;
  83. rect.setLocation(positionMovingX, positionMovingY);
  84. }
  85. if(positionMovingX+rect.height  >= playFieldX || positionMovingY+rect.width >= playFieldY ){
  86. flip = true;
  87. }
  88.  
  89. if(flip == true){
  90. speed = (int) (1+Math.random()*50);
  91. size = (int) (10+Math.random()*100);
  92.  
  93. positionMovingX = positionMovingX-speed;
  94. positionMovingY = positionMovingY-speed;
  95.  
  96. rect.setLocation(positionMovingX, positionMovingY);
  97. }
  98. if(positionMovingX <=0 || positionMovingY<=0 && flip == true){
  99. rect.setSize(size,size);
  100. flip = false;
  101.  
  102. }
  103.  
  104. if(rect.intersects(rect2) && gotHit == false){
  105. hit = hit+1;
  106. gotHit = true;
  107. }
  108.  
  109. if(!rect.intersects(rect2) && gotHit == true){
  110. gotHit = false;
  111. }
  112. repaint();
  113. }
  114.  
  115. public void setPlayingField(int x, int y){
  116. this.playFieldX = x;
  117. this.playFieldY = y;
  118. }
  119. }
  120.  
Created by GeSHI 1.0.7.20
« Last Edit: August 15, 2008, 02:06:47 PM by HappyFace » Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net/forum :: A community for software developers
« on: August 14, 2008, 09:59:39 PM »

Your Ad Here
 Logged
HappyFace
Javaforums.net admin
Senior Member
*

Reputation: 16
Developer @ Engineeringserver network
Offline Offline
Posts: 2552
Referrals: 12
Activity
14%

WWW Awards
« Reply #1 on: August 17, 2008, 03:42:16 PM »

Here's the sourcecode that was shown compiled in the youtube video i've uploaded.

No changes were made in the SpriteDemoJFrame.java file so you can copy the one in the first post and compile it with this one below.

Code
GeSHi (java):
  1. /*--------------------------------------------------------------------------
  2. SpriteDemoJFrame class
  3.  *****************
  4. By: HappyFace http://www.engineeringserver.com - http://www.javaforums.net
  5. Contact: info [@] engineeringserver.com
  6. Version: 15/August/2008
  7. Last updated: 17/August/2008
  8.  *****************
  9. Note: a demo that shows collision detection using rectangles.
  10. //----------------------------------------------------------------------*/
  11. import java.awt.Color;
  12. import java.awt.Font;
  13. import java.awt.Graphics;
  14. import java.awt.Graphics2D;
  15. import java.awt.Rectangle;
  16. import java.awt.event.MouseEvent;
  17. import java.awt.event.MouseMotionListener;
  18.  
  19. import javax.swing.JPanel;
  20.  
  21. public class SpriteDemo extends JPanel  implements MouseMotionListener{
  22. int positionX = 0, positionY = 0;
  23. int positionMovingX = 0, positionMovingY = 0;
  24. int playFieldX = 0, playFieldY = 0;
  25. int movement = 0;
  26. boolean flip = false, gotHit = false, debug = false, fliphit  = false;
  27. int speed = 0;
  28. int size = 10;
  29. int hit = 0;
  30. int points = 0;
  31.  
  32. Rectangle rect = new Rectangle(positionMovingX,positionMovingY,size,size);
  33. Rectangle rect2 = new Rectangle(100,100,40,40);
  34.  
  35. public SpriteDemo(){
  36. addMouseMotionListener(this);
  37. }
  38.  
  39. public void paintComponent(Graphics g){
  40. super.paintComponent(g);
  41. if(debug){
  42. System.out.println(positionMovingX +  "   " + positionMovingY +  "  " + hit);
  43. }
  44. rect2.setLocation(positionX, positionY);
  45.  
  46. g2.setColor(Color.GREEN);
  47. g2.fillRect(rect.x,rect.y,rect.width,rect.height);
  48.  
  49. g2.setColor(Color.WHITE);
  50. g2.fillRect(positionX, positionY,rect2.width,rect2.width);
  51.  
  52. g2.setColor(Color.BLACK);
  53. g2.setFont(new Font("Arial",Font.BOLD,16));
  54.  
  55. g2.drawString("You got hit " + hit + " times, you got " + points + " points", 20, 20);
  56.  
  57. }
  58. public static void main(String[] args) {
  59. }
  60.  
  61. public void mouseDragged(MouseEvent e) {
  62. positionX = e.getX()-20;
  63. positionY = e.getY()-20;
  64. repaint();
  65. }
  66.  
  67. public void mouseMoved(MouseEvent e) {
  68. }
  69.  
  70. public void moveRectangle(){
  71. speed = (int) (5+Math.random()*20);
  72. if(flip == false){
  73. positionMovingX = positionMovingX+speed;
  74. positionMovingY = positionMovingY+speed;
  75. rect.setLocation(positionMovingX, positionMovingY);
  76. }
  77. if(positionMovingX+rect.height  >= playFieldX || positionMovingY+rect.width >= playFieldY ){
  78. flip = true;
  79. }
  80.  
  81. if(flip == true){
  82. speed = (int) (1+Math.random()*50);
  83. size = (int) (10+Math.random()*100);
  84.  
  85. positionMovingX = positionMovingX-speed;
  86. positionMovingY = positionMovingY-speed;
  87.  
  88. rect.setLocation(positionMovingX, positionMovingY);
  89. }
  90. if(positionMovingX <=0 || positionMovingY<=0 && flip == true){
  91. rect.setSize(size,size);
  92. flip = false;
  93. }
  94.  
  95. if(rect.intersects(rect2) && gotHit == false){
  96. hit = hit+1;
  97. gotHit = true;
  98. }
  99.  
  100. if(!rect.intersects(rect2) && gotHit == true){
  101. gotHit = false;
  102. }
  103.  
  104. if(rect2.x <=0 && rect2.y <=0 && fliphit == false){
  105. points = points+1;
  106. fliphit = true;
  107. }
  108.  
  109. if(rect2.x+size/2 >= playFieldX && rect2.y+size/2 >=playFieldY  && fliphit == true){
  110. points = points+1;
  111. fliphit = false;
  112. }
  113. repaint();
  114. }
  115.  
  116. public void setPlayingField(int x, int y){
  117. this.playFieldX = x;
  118. this.playFieldY = y;
  119. }
  120.  
  121.  
  122. }
  123.  
Created by GeSHI 1.0.7.20
Logged

It doesn't matter how hard you've studied; the material won't be on the exam anyway.
Admin @ www.Engineeringserver.com General Admin @ www.Javaforums.net and it's subsites Forum admin @ www.Javaforums.net Global moderator @ www.Engineeringserver.com network Java / .NET blogger @ www.Engineeringserver.com/blog
XMA performer since 2006 @ http://www.engineeringserver.com/XMA-NaN/ Fan of http://www.retardedweblogger.com
Oh man, too much stuff to do in so little time.

http://www.javaforums.net...lery.html;sa=media;id=170
http://img222.imageshack....707/arkietomatoesmall.jpg
http://img208.imageshack.us/img208/7141/smalli.png
Blizzcon 2k9 Grubby and Cassandra Ng engaged ! <3
Triple D, eerste Denken Dan Doen
Javaforums.net/forum :: A community for software developers
   

Your Ad Here
 Logged
Pages: [1]   Go Up
  Print  
 
Jump to: