# Collision Detection?

Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last
• 09-03-2011, 11:22 PM
Alerhau
Collision Detection?
Ohai everybody! Just started up a small new project,
creating a small game for school.

I'm using the draw(); method to draw stuff on the screen,
I managed to get the character to move around,
but when I add walls (In another color),
I just can't figure out how to add a collision detection that works.

Is it possible to make an if statement about what color it is?
I just want it as simple as possible,
going to add some rooms, a store & monsters later on.

Thanks,
Alexander Hållenius
• 09-03-2011, 11:59 PM
Norm
Yes you can detect what has been drawn on an image by looking at its pixels.
It might be easier for your app to define some kind of shape objects for the boundaries and use the contains method to detect collisions.
• 09-04-2011, 12:13 AM
Alerhau
• 09-04-2011, 12:15 AM
Norm
Quote:

Read the API doc for the Shape class.
• 09-04-2011, 12:28 AM
Alerhau
Okay, I will! (-:
• 09-04-2011, 12:47 AM
Alerhau
I have really no idea. Contains? Can't find it in the API. :-/
• 09-04-2011, 12:59 AM
Norm
Here is how you find a method if you don't know what class it is in.
Go to the API doc page. At the top there is a light blue strip with the word Index to the right.
Click on Index to go to the index page.
Then click on the first letter of the method in the row of letters. That would be C for contains.
On the page with all the methods starting with C, Find the method you want to read about.
• 09-04-2011, 07:18 AM
Alerhau
Okay, I found this out,

Quote:

public boolean contains(Point p)
Checks whether the specified point is within this object's bounds, where the point's x and y coordinates are defined to be relative to the coordinate system of the object.
Specified by:
contains in interface AccessibleComponent
Parameters:
p - the Point relative to the coordinate system of the object
Returns:
true if object contains Point; otherwise false
AccessibleComponent.getBounds()
...

How can I use this for the collision detecting?
I'm not using Java 2D, does it matters?
• 09-04-2011, 07:24 AM
Fubarable
Quote:

How can I use this for the collision detecting?
I'm not using Java 2D, does it matters?
Maybe you want to tell us what you are in fact using if not Java2D.
• 09-04-2011, 08:09 AM
Alerhau
Hmm, I might actually be using Java 2D? (I'm very new to graphics)

What I mean is that I have'nt been typing anything more than "super.paintComponents(g)" in my paint method.
I also have some stuff just drawn by g.fillRect();

So, then to the collision detection again I suppose if you don't need me to do something else before doing that.
• 09-04-2011, 02:02 PM
Norm
Re: Collision Detection?
If you define a Rectangle where there is a rectangular boundary that you want to detect a collision with and use a Point on the object that is moving then use the contains method to see if the point is in the rectangle.
Or if you have a rectangular shape that is moving you could use the intersects method to see if the moving rectangle's position intersects with the rectangular boundary.
The are several methods for shapes that could be useful for collision detection.
• 09-04-2011, 02:26 PM
Alerhau
Re: Collision Detection?
How exactly do I define the boundaries? I mean. the rectangle does'nt have a name :-/
• 09-04-2011, 02:33 PM
Norm
Re: Collision Detection?
Quote:

How exactly do I define the boundaries
Define a rectangle to align with one of the boundaries.
Where are the boundaries that you want to detect?
Say they are at upper left of 10, 10 to lower right of 50, 50
Then define a rectangle:
Rectangle bndry1 = new Rectangle(10, 10, 40, 40);
Then if you have a point:
Point pt = new Point(20, 20);

if (bndry1.contains(pt)) { //Test if the pt within the rectangle
....
}
• 09-04-2011, 04:00 PM
Alerhau
Re: Collision Detection?
Thankyou very much Norm! You made me solve it! :-)
I still have a lot to learn about it, but now I can make my first little adventure game for school!

Thanks,
Ah
• 09-04-2011, 07:28 PM
Alerhau
Re: Collision Detection?
Hmm, seems to work fine.
But there is one more thing that I have to ask.

How do I make my character not getting stuck in the wall each time he collides with it?
Any ideas?

Thanks,
Ah
• 09-04-2011, 07:29 PM
Norm
Re: Collision Detection?
Change its x,y and direction of motion if it is supposed to bounce
• 09-04-2011, 07:40 PM
Alerhau
Re: Collision Detection?
I don't really know what to change my X & Y to.
If I do that I suppose that my character will start to jump around and do double steps sometimes (Jump 4 pixels forward).
• 09-04-2011, 07:47 PM
Norm
Re: Collision Detection?
Quote:

getting stuck in the wall
I assume means the object's x,y values overlap with the wall's x,y values.

Quote:

I don't really know what to change my X & Y to.
That would depend on the visual effect you want. Try different changes to see what looks right.
• 09-04-2011, 08:21 PM
Alerhau
Re: Collision Detection?
Gaah, I can't get it :-/

Here is the code, tried to make some comments so that you can understand it better.

Code:

```import javax.swing.*; import java.awt.event.*; import java.awt.*; public class ac extends JPanel {         public int xp = 10, yp = 110, speed = 1;                 public ac(ab l) {                                 l.addKeyListener(new KeyAdapter() {                         public void keyPressed(KeyEvent ke) {                                 Rectangle[] rec = {new Rectangle(0, 100, 100, 100)}; //Rectangle;                                 Point pt = new Point(xp, yp); //Current Character Position;                                                                 if(ke.getKeyCode() == KeyEvent.VK_W) { //Upward                                                                                 if(rec[0].contains(pt)) {                                                 yp -= sp;                                                 repaint();                                         } else {                                                                                         }                                                                         }                                 if(ke.getKeyCode() == KeyEvent.VK_S) { //Downward                                                                                 if(rec[0].contains(pt)) {                                                 yp += speed;                                                 repaint();                                         } else {                                                                                         }                                                                         }                                 if(ke.getKeyCode() == KeyEvent.VK_A) { //Left                                         if(rec[0].contains(pt)) {                                                 xp -= speed;                                                 repaint();                                         } else {                                                                                         }                                 }                                 if(ke.getKeyCode() == KeyEvent.VK_D) { //Right                                         if(rec[0].contains(pt)) {                                                 xp += speed;                                                 repaint();                                         } else {                                                                                         }                                 }                                                         }                 });                         }                 public void paintComponent(Graphics g) {                 super.paintComponent(g);                                 //Level;                 g.setColor(Color.BLACK);                 g.fillRect(0, 100, 100, 100);                 //Level;                                 g.setColor(Color.WHITE);                 g.fillRect(xp, yp, 10, 10);                         } }```
• 09-04-2011, 08:22 PM
Alerhau
Re: Collision Detection?
The first thing that says "sp", in the upward movement are supposed to be "speed".
Show 40 post(s) from this thread on one page
Page 1 of 2 12 Last