Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Collision sensors.  RSS feed

 
Bod McLeon
Ranch Hand
Posts: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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

 
Stephan van Hulst
Saloon Keeper
Posts: 7693
139
  • Mark post as helpful
  • send pies
  • 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: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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: 7693
139
  • Mark post as helpful
  • send pies
  • 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: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • 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.
 
Carey Brown
Saloon Keeper
Posts: 2832
41
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Dave Tolls
Ranch Hand
Posts: 2809
30
  • Mark post as helpful
  • send pies
  • 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: 7693
139
  • Mark post as helpful
  • send pies
  • 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: 65
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Guys!
 
Piet Souris
Rancher
Posts: 1941
66
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!