Eiffel Media API
Overview Cluster Class Classes Index      Previous Next      Top Features

em.video.collision_detection

Class EM_COLLISION_DETECTOR



Creation

Features

Invariants

indexing

description

This (2D) collision detector checks the added objects for collisions and generates a
list of all collisions (collisions: DS_LINKED_LIST[EM_COLLISION [G]]).
this is void, if no collision was found.

usage:
first specify, how many sets of collidables you would like to have added
A set of collidables is a list of collidable objects, that should not be checked for collisions with each
other, this may be useful, if for example there are objects, that do not move, compared to
each other, like compositions of collidables, or non-moving objects.
The number of sets may be increased and decreased later on.

date

$Date: 2005/10/23 10:49:44 $

revision

$Revision: 1.18 $

class

EM_COLLISION_DETECTOR [G->EM_COLLIDABLE]

create

make (nr_of_sets_of_collidables: INTEGER)

-- creates an array of sets of collidables
-- note: arrays start from index 1

require
nr_valid: nr_of_sets_of_collidables >= 0
ensure
count_correct: count = nr_of_sets_of_collidables

feature -- Access

collides_with_position (pos: EM_VECTOR_2D): G

-- returns a collidable that collides with a certain position.
-- this can be useful, for getting a single collision with the mouse
-- the last in the list will be returned

count: INTEGER

-- counts the number of sets

last_collisions: DS_LINKED_LIST [EM_COLLISION_COMPOSITION [G]]

-- a list of the collisions generated by check_for_collisions

movement_check: INTEGER

-- if this is enabled (>0), the whole path from last_position to the current position
-- will be checked for a collision. This computations takes a lot of time, so if it
-- is not really necessary (if there are no such quick movements for example), don't
-- use it. An example of its usage is, that it prevents objects from flying through
-- walls or other thin items.
-- Performance: O(2^n)

search_depth: INTEGER

-- the depth of bisection for search of intersection points of two collidable objects.
-- default is 0 (meaning just the final position of the collidables)
-- search_depth has to be greater than 0 in order to do movement_checks

sets_of_collidables: ARRAY[DS_LINKED_LIST[G]]

-- all sets of collidables in the scene

feature -- Drawing

draw (a_surface: EM_SURFACE)

-- draws all contained collidables on the screen, with clipping and zoom_factor

feature -- Creation

make (nr_of_sets_of_collidables: INTEGER)

-- creates an array of sets of collidables
-- note: arrays start from index 1

require
nr_valid: nr_of_sets_of_collidables >= 0
ensure
count_correct: count = nr_of_sets_of_collidables

feature -- Element Change

add (a_collidable: G; add_to_set: INTEGER)

-- Add a_collidable to a certain set (takes the collidable and index as arguments)

require
a_collidable_not_void: a_collidable /= void
add_to_set_not_void: add_to_set <= count and add_to_set > 0
ensure
collidable_added: sets_of_collidables.item (add_to_set).last = a_collidable
add_list (collidable_list: DS_LINKED_LIST [G]; add_to_set: INTEGER)

-- adds all elements of collidable_list to the set

require
collidable_list_not_void: collidable_list /= void
add_to_set_not_void: add_to_set <= count and add_to_set > 0
empty_set (set_index: INTEGER)

-- empties the set with index set_index

remove (a_collidable: G)

-- Removes a_collidable from any set containing it

require
a_collidable_not_void: a_collidable /= void
remove_from_set (a_collidable: G; set_index: INTEGER)

-- removes a_collidable from a certain set

set_draw_circumcircle (v: BOOLEAN)

-- also draws the circumcircle of the collidable

ensure
draw_circumcircle_set: draw_circumcircle = v
set_draw_circumcircle_color (c: EM_COLOR)

-- sets the circumcircle color

require
color_not_void: c /= Void
ensure
circumcircle_color_set: draw_circumcircle_color = c
set_draw_clipping (top_left_corner: EM_VECTOR_2D; a_zoom_factor: DOUBLE)

-- This sets the clipping for the part to draw
-- The first argument is the top left corner of the screen.
-- for example if we want an object, that is situated at (5000, 1000), the
-- clipping should be moved to an appropriate position e.g. (4800, 800)
-- All objects outside of the clipping will not be displayed

require
corner_not_void: top_left_corner /= void
factor_valid: a_zoom_factor > 0
set_draw_color (a_color: EM_COLOR)

-- sets the color of all collidables, drawn in draw

require
a_color_not_void: a_color /= Void
set_draw_filled (v: BOOLEAN)

-- sets the draw mode saved in filled

ensure
filled_set: filled = v
set_movement_check (v: INTEGER)

-- sets movement_check, see movement_check for more details
-- Performance for movement check: O(2^n)

ensure
movement_check_set: movement_check = v
set_search_depth (depth: INTEGER)

-- sets search_depth, see search_depth for more detail

require
depth_valid: depth >= 0
ensure
search_depth_set: search_depth = depth

feature -- Computation

check_for_collision

-- Checks if any element collides with an other from another set
-- Saves the resulting collisions in the variable collisions
-- worst-case performance: O(n^2)
-- resets the cursors on sets_of_collidables

feature -- Iteration

do_all (action: PROCEDURE [ANY, TUPLE [G]])

-- Apply action to every item in the collision detector.
-- Semantics not guaranteed if action changes the structure;
-- in such a case, apply iterator to clone of structure instead.

for_all (test: FUNCTION [ANY, TUPLE [G], BOOLEAN]): BOOLEAN

-- Is test true for all items?

feature -- Status Report

has (a_collidable: G): BOOLEAN

-- returns true, if the collidable is in the detector
-- Performance: O(n)

require
a_collidable_not_void: a_collidable /= Void

invariant

count_non_negative: count >= 0

-- From ANY
reflexive_equality: standard_is_equal (Current)
reflexive_conformance: conforms_to (Current)

end