• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Checking intersection of two shapes of type Graphics java AWT

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi. I am making a game where there is a character that the snake (player controls this) and an enemy. What I am trying to achieve is that the program should detect collisions between the snake and the enemy . However, I have drawn both the snake and the enemy using fillRect() and fillOval() respectively (using Graphics library java AWT) and my current collision detection code is not functioning.

I am wondering if there is a way to check for collisions between two java AWT Graphics objects. I will show my code which includes the current state of collision detection in my program. I have also included a screenshot image of the current state of collision detection in my program.

Code:


I have taken out some misc code to attempt to shorten the post

the image below shows how collision detection is currently not working between the snake and the enemy (green)
Capture.JPG
[Thumbnail for Capture.JPG]
 
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I have taken out some misc code to attempt to shorten the post


Could you fix the code so that it compiles and executes for testing?

Where are the locations of the two objects stored?
 
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Would this work?

 
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I am wondering if there is a way to check for collisions between two java AWT Graphics objects.



Don't paint graphics objects.

Instead paint Shape objects using the Graphics2D.fill( Shape ) method.

Then you can use the Shape.intersects(...) method to check for collisions.

Check out Playhing With Shapes for some general information on using a Shape object.

An example of playing with shapes that you can create and drag around a panel:


Note, the above code converts the Shape objects to a GeneralPath object to make the dragging logic easier. In your case you can just keep the objects a Shape objects.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

I have taken out some misc code to attempt to shorten the post


Could you fix the code so that it compiles and executes for testing?

Where are the locations of the two objects stored?



If you want to test the code, you can use the following link which links to my github project and the code for the main class and panel class is there: https://github.com/zkhan122/snakeboost
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you post code that demonstrates the problem.  The code from github does not show any collisions when it executes without any interaction from the user.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:Can you post code that demonstrates the problem.  The code from github does not show any collisions when it executes without any interaction from the user.




The code on my github repository is the current state of the program. You mentioned "does not show any collisions when it executes without any interaction from the user" and that is the problem I am facing; where when the snake collides with the enemy, there is no feedback which ultimately means that my program is not detecting collisions properly.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:Would this work?



What exactly would I iterate over? Because iterating over the variable bodyParts would mean that I am iterating over the snake body but the enemy and snake do not have the same body length. Can you clarify further?
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When I execute the code, I do not see any collisions between the two moving bodies.
What do I need to do to make the moving bodies collide?
Can you change the code so that the two bodies collide very soon after the program starts?

How are you debugging the code to see what the x,y values are for both of the moving bodies so you can see what the values are when a collision is missed?   I suggest adding some print statements that print the x,y values for each moving body.
 
Carey Brown
Saloon Keeper
Posts: 11054
88
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code needs lots of debugging to find all the bugs before worrying about the collision logic.
For example,
the enemy body does not reverse when touching all 4 boundaries.
Current code does not use the enemyX and enemyY values for displaying its location.
And more
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:The code needs lots of debugging to find all the bugs before worrying about the collision logic.
For example,
the enemy body does not reverse when touching all 4 boundaries.
Current code does not use the enemyX and enemyY values for displaying its location.
And more



The enemy body is programmed to just reverse vertically when it touches top border and bottom border (not horizontal borders as you mentioned "4 borders"). Is that not being displayed for you?
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There are no comments in  the code saying what directions are valid for the enemy body. Is it only 'U' and D?
Also there are no comments regarding what value of x the enemy should have.  The code appears to use the center column.
 

What about my other requests for the code?


When I execute the code, I do not see any collisions between the two moving bodies.
What do I need to do to make the moving bodies collide?
Can you change the code so that the two bodies collide very soon after the program starts?

How are you debugging the code to see what the x,y values are for both of the moving bodies so you can see what the values are when a collision is missed?   I suggest adding some print statements that print the x,y values for each moving body.


 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:There are no comments in  the code saying what directions are valid for the enemy body. Is it only 'U' and D?
Also there are no comments regarding what value of x the enemy should have.  The code appears to use the center column.
 

What about my other requests for the code?


When I execute the code, I do not see any collisions between the two moving bodies.
What do I need to do to make the moving bodies collide?
Can you change the code so that the two bodies collide very soon after the program starts?

How are you debugging the code to see what the x,y values are for both of the moving bodies so you can see what the values are when a collision is missed?   I suggest adding some print statements that print the x,y values for each moving body.




The body only moves up and down hence the 'U' and 'D'. X value is being treated as 0 as the center is being treated as Y axis (i believe) and it is moving up and down that vertical axis

Also with regards to your request of wanting the "two bodies to collide very soon after the program starts" my problem is the collision itself which I believe I have made clear; my collisions are not working so I need help with that. Others have suggested not to use Graphics g and instead use Rectangles and intersects() which is fine but I want to try my best to do it without intersects() first. If you can provide help with that, it is very much appreciated
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

my collisions are not working

How do you get a collision?
When I test the program, I would like to see a collision almost immediately after the program starts.
Currently when I execute the program the two bodies do not collide.  One just runs off the screen.

Can you change the code so that the two bodies start near each other and have paths that cross so that there is a collision very soon after the program starts?

How does the code use the enemyX array?  I don't see that it is used when the body is drawn.
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Make the game more challenging by having the enemy change directions:
At the class level:

In the move method:
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

my collisions are not working

How do you get a collision?
When I test the program, I would like to see a collision almost immediately after the program starts.
Currently when I execute the program the two bodies do not collide.  One just runs off the screen.

Can you change the code so that the two bodies start near each other and have paths that cross so that there is a collision very soon after the program starts?

How does the code use the enemyX array?  I don't see that it is used when the body is drawn.




When you say you would like to see a collision, are you referring to how you would like to see the two bodies crossing over each other (perpendicular intersection) as soon as the program starts? Because they can overlap but the program should stop and say "Game over" when it is a perpendicular intersection (any part of snake touching enemy)  or even if the snake head touches any part of enemy.

Also the array was originally being used to draw the enemy but since I wanted the enemy body to start going up and down in the CENTER that is why I replaced with . I will try to make the enemy direction randomized as you stated for further upgrade in difficulty.
 
Norm Radder
Rancher
Posts: 5114
38
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I replaced ...


Then the code that tests for collisions needs to know and use that so it does not use enemyX.

the program should stop and say "Game over" when ...

The collision never happens.
What does a tester need to do to make a collision happen?  I do not get a collision when I execute the code.
 
Zayaan Khan
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Norm Radder wrote:

I replaced ...


Then the code that tests for collisions needs to know and use that so it does not use enemyX.

the program should stop and say "Game over" when ...

The collision never happens.
What does a tester need to do to make a collision happen?  I do not get a collision when I execute the code.



Hi thanks for your help overall I actually managed to get the collision detection working such that when the head of the player snake touches the green enemy then the program detects this and ends the game. I managed to do this by drawing rectangles (as hit boxes) around both sprites and checking if rectangles intersect and also changed my logic for the nested for loop and how it was detecting if the coordinates of both sprites were the same ! (though this is only for the head so I need to do the same for the whole of the player snake body).
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic