• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Saloon Keeper
Posts: 15630
366
  • 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
Saloon Keeper
Posts: 15630
366
  • 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: 10804
86
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
Saloon Keeper
Posts: 15630
366
  • 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!
 
Bartender
Posts: 5478
212
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic