useflashpunk / FlashPunk

A free ActionScript 3 library designed for developing 2D Flash games. It provides you with a fast, clean framework to prototype and develop your games in. This means that most of the dirty work (timestep, animation, input, and collision to name a few) is already coded for you and ready to go, giving you more time and energy to concentrate on the design and testing of your game.
http://useflashpunk.net
MIT License
390 stars 134 forks source link

Circular hitboxes #13

Open Draknek opened 13 years ago

Draknek commented 13 years ago

This is a very common requirement, it should be built-in to FlashPunk.

robertgoss commented 13 years ago

As all the current hitboxes are based on width and height it may be easier to add oval hitboxes.

Draknek commented 13 years ago

Detecting intersection of circles is simpler than ellipses, so I'd probably restrict it to circles and have width=height=diameter.

robertgoss commented 13 years ago

The code isnt that much more complex (about 10 more line in total as i implemented it) its the same test but with a scaled distance function.

Draknek commented 13 years ago

Can you post your code please? I don't believe it is as simple as that. An example of a problematic setup: http://www.draknek.org/misc/ellipses.svg

robertgoss commented 13 years ago

Yes your right I made a stupid assumption about ellipses.

I do have the code to work this but it is quiet a bit longer (Mostly forming a particular quartic then fiddling the discriminate around). Probably best left out for now.

On another note might it be a better idea to separate the notion of a bounding box out of the hitbox and make the hitbox just a mask (defaulting to the bounding box)? That would allow more flexibility for the shape (for example to allow for rotation). But it would be a big api change so may not be best way of going forward.

robertgoss commented 13 years ago

Have a current version here https://github.com/cipher05/FlashPunk/commit/bca9118df7aa1b9f8e317bb225edc883d51785cc

Does it look ok?

Draknek commented 13 years ago

That won't correctly handle some collisions, e.g. circle-vs-pixelmask.

As far as implementations go, I was imagining a CircleMask class rather than adding a circular property to Entity. Not actually sure which would be better though. If circles were just another type of mask, it means a lot of code will just automatically work. But if there's support at the entity level then circle-circle collisions can be really efficient.

I certainly don't think setHitbox should be extended in that way. If we want a convenience function for setting a circular hitbox, it should be a separate function.

IkonOne commented 12 years ago

This handles circle-circle collisions properly, but that is it. Everything else it treats as a square hitbox. Also, the renderDebug is not quite right. But it's a start.

http://pastebin.com/8e7d1JHh

This is untested against anything other than other circles and hitboxes.