Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 12-24-2007, 06:58 AM
Member
 
Join Date: Dec 2007
Posts: 10
quickfingers is on a distinguished road
Massive Hit Deletion Help please
Hello I am new here but could someone please guide be through how to make an array for my hit detection.

Below is the code I am focusing on for my hit detection.


// in my public void run()


Rectangle paddleRect = new Rectangle(paddlex,650,75,15);
Rectangle ballRect = new Rectangle(ballx,bally,25,25);

if(ballRect.intersects(paddleRect))
{
yChange=yChange*-1;
bally=bally-1;
}
if(ballRect.intersects(Rect))
{
yChange=yChange*-1;
}



As you can see I am using a very basic method for my hit detection for my 40 blocks in my break out game.

Here is how I have my game setup

I am using one array for the blocks x locations than I just create four different images of the blocks which I place at 4 different y locations.

I hope this is enough infomation. If you need more infomation please tell me so I can get this breakout game done before christmas
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-24-2007, 08:49 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.geom.*; import javax.swing.*; public class HitDetection extends JPanel implements Runnable { Rectangle[] blocks = { new Rectangle(100,100,30,30), new Rectangle(140,160,30,30), new Rectangle(240,270,30,30), new Rectangle(200,300,30,30) }; Ellipse2D.Double ball = new Ellipse2D.Double(200,10,20,20); int dx = 3; int dy = 2; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); for(int j = 0; j < blocks.length; j++) g2.draw(blocks[j]); g2.setPaint(Color.red); g2.fill(ball); } public void run() { boolean running = true; while(running) { try { Thread.sleep(50); } catch(InterruptedException e) { running = false; } checkForCollision(); checkBoundries(); double x = ball.x + dx; double y = ball.y + dy; ball.setFrame(x, y, ball.width, ball.height); repaint(); } } private void checkForCollision() { Ellipse2D.Double e = new Ellipse2D.Double(); e.setFrame(ball.x+dx, ball.y+dy, ball.width, ball.height); for(int j = 0; j < blocks.length; j++) { if(e.intersects(blocks[j])) { bounce(blocks[j], e); break; } } } private void bounce(Rectangle r, Ellipse2D.Double e) { double cx = e.getCenterX(); double cy = e.getCenterY(); int outcode = r.outcode(cx, cy); switch(outcode) { case Rectangle.OUT_TOP: case Rectangle2D.OUT_BOTTOM: dy *= -1; break; case Rectangle2D.OUT_TOP + Rectangle2D.OUT_LEFT: case Rectangle2D.OUT_LEFT + Rectangle2D.OUT_BOTTOM: case Rectangle2D.OUT_BOTTOM + Rectangle.OUT_RIGHT: case Rectangle2D.OUT_RIGHT + Rectangle2D.OUT_TOP: dx *= -1; dy *= -1; break; case Rectangle2D.OUT_LEFT: case Rectangle2D.OUT_RIGHT: dx *= -1; break; default: System.out.println("CENTER: " + outcode); } } private void checkBoundries() { if(ball.x + dx < 0 || ball.x + ball.width + dx > getWidth()) dx *= -1; if(ball.y + dy < 0 || ball.y + ball.height + dy > getHeight()) dy *= -1; } private void start() { while(!isVisible()) { try { Thread.sleep(25); } catch(InterruptedException e) { break; } } Thread thread = new Thread(this); thread.setPriority(Thread.NORM_PRIORITY); thread.start(); } public static void main(String[] args) { HitDetection test = new HitDetection(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); test.start(); } }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Deletion Servlet Java Tip Java Tips 0 12-25-2007 12:23 PM


All times are GMT +3. The time now is 07:17 PM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org