• 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: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm making a commander keen type game at the moment. Can anyone suggest the best way to implement collision detection (with enemies, landscape and the like) into the game?

Thanks,
Mark.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
give everything a bounding box.
Keep track of the coordinates of those bounding boxes and calculate when two or more of them intersect.
 
Ranch Hand
Posts: 815
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm a big fan of bounding circles... you need alot less comparisons. Just check if the distance between the two centers is less than the sum of the radii.
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
same principle but often less accurate unless you have multiple overlapping bounding circles for an object (or stretch the circles into ellipses in which case you're probably cheaper off using rectangles).
 
Mark Lockery
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the ideas
 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do we need to have multiple overlapping bounding circles for an object to make it as accurate as bounding rectangle?

Thank
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
wait- how do you give an image a bounding circle?
And how do you incorporate the walls of the drawing panel into the collision? Do you need a collision class or something?
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The same way you give it a bounding rectangle, I would guess: choose a center and radius that completely encloses the sprite. This is just your first level detection. If two circles intersect, then you can use the accurate yet more expensive collision detection. If they don't intersect, no need to do a more accurate test.

Just to be clear, bounding regions give you a "they might intersect or they absolutely do not intersect" test.

Also, if you're trying to scrape every last bit of performance out, are using circles, and are doing a lot of first-level collision detection, you don't need to take the square root of the distance between two points. You can instead square all the distances at which a collision takes place and compare against them.

How do you use these for collisions with the map versus other moving objects? I would guess you use something entirely different, but I didn't read that far into the game programming books I got.
 
I would challenge you to a battle of wits, but I see you are unarmed - shakespear. Unarmed tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic