• 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:

Collision sensors.

 
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is no code for this one, just a quick question.
Is the something like a collision sensor. For example:

If X touches Y a sound will be played. Or in even more detail,
If your character touches a star a pling sound will be played.

Thanks

 
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This completely depends on what types X and Y are represented by, and what kind of application you are writing.
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:This completely depends on what types X and Y are represented by, and what kind of application you are writing.


X represents your character. I can't go to much into detail as I don't know what my character is yet. Y is the star that will add 1 to your point score.
As your character (X) touches / picks up the star I want it to make a pling sound. I have the sound effect and the code for the API. I don't have the star made but I don't need it yet. I just need to know if there is code for as such 'collision'.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Okay, so you're making a graphical application. What technology are you using to render your graphics?
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hmmm.. I don't really understand what you mean by 'what technology are you using to render your graphics?'.I am just making the game ColourSwitch (Use google images if you don't know what it is). I am using a JPanel with a few shapes on it just stars at the moment and as I said every time you touch the stars it makes a pling.
 
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
Depending on how precise it has to be, you could have a bounding rectangle or a bounding circle for your shapes and look for a collision of those bounds. Much quicker and easier than looking for pixel collisions on odd shapes.
 
Rancher
Posts: 4801
50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Java by itself doesn't have that sort of collision detection.

Well, unless you mean "click on the star with the mouse", in which case you'd probably have a mouse listener attached to the panel (assuming you've overidden paintComponent to do all your drawing).
Then you'd handle the click event and read the coordinates and see if they match any of your stars.
 
Stephan van Hulst
Bartender
Posts: 15741
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you're using JPanel, then the technology I was asking about would be Swing.

If you're using Swing, then you can use the various java.awt.Shape subtypes to represent your geometry. For instance, you could represent your stars with java.awt.Polygon.

When you want to do collision detection, construct a java.awt.geom.Area out of your shapes, intersect() them, and see if the resulting area isEmpty().

The upside of this approach is that besides performing collision detection, you can also directly render shapes on your panel.

[edit]

Dave Tolls wrote:Java by itself doesn't have that sort of collision detection.


Untrue. Check out java.awt.geom.Area.intersect().
 
Bod McLeon
Ranch Hand
Posts: 146
Mac OS X IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Guys!
 
Saloon Keeper
Posts: 5614
214
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But using only Shapes has some drawbacks. It is much work getting all those Color Switch things into a Shape, redrawing is slow, the collision detection ditto, to name a few.
 
reply
    Bookmark Topic Watch Topic
  • New Topic