Trait collider::HbProfile
[−]
[src]
pub trait HbProfile: Copy { fn id(&self) -> HbId; fn can_interact(&self, other: &Self) -> bool; fn cell_width() -> f64; fn padding() -> f64; fn group(&self) -> Option<HbGroup> { ... } fn interact_groups(&self) -> &'static [HbGroup] { ... } }
A trait that holds metadata for describing a hitbox.
A user of Collider
will need to implement an HbProfile that best suites their needs in a game.
The most basic HbProfile will just contain an integer ID for the hitbox,
but a user may define additional metadata for identfying the hitbox and describing interactivity.
An HbProfile must implement the Copy
trait and should not take up much memory.
Required Methods
fn id(&self) -> HbId
A unique identifier for the hitbox.
Trying to have multiple hitboxes with the same id
to a Collider
instance simultaneously
will result in a panic.
fn can_interact(&self, other: &Self) -> bool
Returns true if the pair of hitboxes should be checked for collisions.
This method should be commutative.
This method should be consistent with group
and interact_groups
,
although possibly more restrictive.
fn cell_width() -> f64
The width of the cells used in the Collider grid.
To reduce the number of overlaps that are tested,
hitboxes are placed in a sparse grid structure behind the scenes.
cell_width
is the width of the cells used in that grid.
If your game has a similar grid concept, then it is usually a good choice
to use the same cell width as that grid.
Otherwise, a good choice is to use a width that is slightly larger
than most of the hitboxes.
fn padding() -> f64
The minimum distance before two hitboxes are considered separated.
Collider generates both Collide
and Separate
events.
However, due to numerical error, it is important that two hitboxes
be a certain small distance apart from each other after a collision
before they are considered separated.
Otherwise, false separation events may occur if, for example,
a sprite runs into a wall and stops, still touching the wall.
padding
is used to describe what this minimum separation distance is.
This should typically be something that is not visible to the
user, perhaps a fraction of a "pixel."
Another restriction introduced by padding
is that hitboxes are not
allowed to have a width or height smaller than padding
.
Provided Methods
fn group(&self) -> Option<HbGroup>
Returns the group id associated with the hitbox.
Default is Some(0)
.
If None
is returned, then no collisions will be reported
for this hitbox at all.
fn interact_groups(&self) -> &'static [HbGroup]
Returns a list of groups that this hitbox can interact with.
Default is [0]
.
Using large lists of groups may be inefficient.