# Collision Detection?

Show 40 post(s) from this thread on one page
Page 2 of 2 First 12
• 09-04-2011, 08:24 PM
Norm
Re: Collision Detection?
Your code does not compile and is missing a main method to be able to execute it.
• 09-04-2011, 08:40 PM
Norm
Re: Collision Detection?
Can you explain what is supposed to happen when the WASD keys are pressed and when the white square goes to the side of the black square?
• 09-04-2011, 09:04 PM
Alerhau
Re: Collision Detection?
I think I actually managed to solve it.
Thanks for all help,
Ah
• 09-04-2011, 09:05 PM
Norm
Re: Collision Detection?
Part of your problem was that the object is larger than the point at the top left corner.

Some suggestions:

Code:

```class ac extends JPanel {         public int speed = 1;         Rectangle[] rec = {new Rectangle(100, 100, 100, 100)}; //Rectangle         Rectangle pt = new Rectangle(120, 130, 5,5 ); //Current Character Position;         in the key listeners                 pt.y -= speed; and in the paint method:                 g.setColor(Color.BLACK);                 g.fillRect(rec[0].x, rec[0].y, rec[0].width, rec[0].height);                 //Level;                         g.setColor(Color.WHITE);                 g.fillRect(pt.x, pt.y, pt.width, pt.height);```
• 09-04-2011, 09:18 PM
Alerhau
Re: Collision Detection?
Now I encountered even one more problem...
My character is walking in the white areas, so he is walking into the cubes.
But when he are supposed to change cube, I mean when I add,

if(rec[0].contains(pt) *Here the thing I want to show starts* || rec[1].contains(pt))

he stays in the rec[0] area. :-/ How can I solve this?
• 09-04-2011, 09:19 PM
Alerhau
Re: Collision Detection?
Oh, did'nt see you wrote something. I'll take a look.
• 09-04-2011, 09:36 PM
Alerhau
Re: Collision Detection?
But he is still getting stuck in the walls.
• 09-04-2011, 09:39 PM
Norm
Re: Collision Detection?
What stops the moving of the small white square when you are pressing one of the WASD keys?
Do you need to move the small white square back inside of the black square.
• 09-05-2011, 09:20 PM
Alerhau
Re: Collision Detection?
He stops when hitting the walls. It's like he is going one pixel too much.
Is there any way to tell him that he can't walk that extra pixel?
• 09-05-2011, 09:24 PM
Norm
Re: Collision Detection?
Quote:

He stops when hitting the walls
What does your logic do when HE hits a wall? What happens to future presses of the key that moved HIM to hit the wall.

Quote:

Is there any way to tell him that he can't walk that extra pixel?
Test BEFORE changing the x,y if HE will hit a wall and don't move HIM if the new x,y hits a wall.

Have the "boundary" wall one or two pixels inside of the visual wall.
• 09-06-2011, 05:13 PM
Alerhau
Re: Collision Detection?
Hmm, what do you mean with a boundary wall? :o
• 09-06-2011, 05:23 PM
Norm
Re: Collision Detection?
A rectangle inside of the visual rectangle.
Visual at 10, 10, 100, 100
boundary at 11, 11, 98, 98

Not really necessary, your code should detect a wall and not go past or into it.
• 09-06-2011, 06:37 PM
Alerhau
Re: Collision Detection?
Are you sure there are no other word then constaints(); i can use in order to see the walls?
He is going one pixel too much every time.
• 09-06-2011, 06:44 PM
Norm
Re: Collision Detection?
Quote:

He is going one pixel too much every time.
Your code should detect that BEFORE the movement
Say the wall is at x = 100
and the object is at x = 99 and moving to the right (+x)
If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99
• 09-06-2011, 06:46 PM
Norm
Re: Collision Detection?
Quote:

no other word then constaints()
Do you mean methods?
Read the API doc to see if any other methods would be of use.
• 09-06-2011, 09:26 PM
Alerhau
Re: Collision Detection?
Quote:

Your code should detect that BEFORE the movement
Say the wall is at x = 100
and the object is at x = 99 and moving to the right (+x)
If the distance to move is 1, 99+1 is into the wall => Don't make the move, stay at x=99
Hmm, the code is testing it before the movement :-o
How do you want me to stay at 99? -.-
• 09-06-2011, 09:33 PM
Norm
Re: Collision Detection?
Basically: if the move will be into the wall do not make it. What order can you set, test and change the values that will prevent moving into the wall. Look at the order you are doing those things.

Post the lines of code you are using to keep the object from moving into a wall.
• 09-07-2011, 07:43 AM
Alerhau
Re: Collision Detection?
Can't I like test collision for only one side of the cube?
For an example when it hits the left wall the left movement stops.
That would make me to solve it.

Thanks,
Ah
• 09-07-2011, 01:17 PM
Norm
Re: Collision Detection?
Quote:

Can't I like test collision for only one side of the cube?
Why only one side? Is this just for testing some new code?

Post the lines of code you are using to keep the object from moving into a wall.
• 09-07-2011, 04:55 PM
Alerhau
Re: Collision Detection?
I think I actually managed to solve it now.
Made 2 different rectangles, one for left/up & one for right/down.
Works perfect!

Thanks!
Show 40 post(s) from this thread on one page
Page 2 of 2 First 12