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

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello thanks for reading this
is there another method for detecting a collision between two objects on the screen other than using co-ordinate tracking as below
if (flyer[1].x<=flyer[0].x+25 && flyer[1].x>flyer[0].x-25){
if (flyer[1].y<=flyer[0].y+15 && flyer[1].y>flyer[0].y-15){
//ACTION
}
}
many thanks simon
 
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You certainly need consider z-order too. See here. http://members.tripod.com/rosezhang/Sprites/Sprite.html
 
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To what ends? What are you trying to achieve?
Are you meaning you want to have some other collision detection than a rectangle? I guess that's what you mean, as I don't see how you could do it without knowing the object's coordinates..
You can have collision detection for circles easily enough with a little math, but it gets tricky if you want to do per-pixel collisions - it can take a while to calculate, at least.
You might try using the Shape class - set up a Shape similar to the sprite you're using, then you can use intersects() or contains() to check collisions (it's a shame there isn't an intersects( Shape ), but it's still handy nonetheless).
Is that what you mean?
 
simcel jones
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello grant
thanks for replying
this is what i meant, sorry if it wasn't too clear
i am unfamiliar with the Shape class
how do the contains() and intersects() methods work?
thanks for your time in this
simon
 
Grant Crofton
Ranch Hand
Posts: 154
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To be honest, I've never tried this with the Shape class, but it seemed like a good idea..
Shape is actually an interface, so you'd probably want to use Polygon.
You set up a Polygon with an array of points - actually two arrays, one for x points and one for y (and a number - how many points there are). For example, to set up a square, it would be something like
int[] xPoints = new int{0, 10, 10, 0};
int[] yPoints = new int{10, 10, 0, 0};
Polygon theSquare = new Polygon( xPoints, yPoints, 4 );
NOTE: I haven't tried this, so it might not work. You might have to connect up the points on the polygon, for example.
So if you set up a polygon with the shape of the sprite you're using, this can help you with collision detection (you'd just have to experiment to find the right shape, or draw it on squared paper or something).
The trouble is, there is no method to find if part of a Shape is inside a Shape - you can find if a Point is inside a Shape. So I don't think there is an easy way to do it if both objects are using aShape for collision detection.
What you could do is get each point of the shape and test if that's inside the other Shape - if you have a lot of space between points this won't work, but if the sprites are fairly small it should work OK.
Got to go now, lunch break over, but shout if u need more help.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic