Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members | Related Pages

physics/Collider.cpp

Go to the documentation of this file.
00001 /****************************************************************************
00002   Copyright (C)2003 David Jung <opensim@pobox.com>
00003 
00004   This program/file is free software; you can redistribute it and/or modify
00005   it under the terms of the GNU General Public License as published by
00006   the Free Software Foundation; either version 2 of the License, or
00007   (at your option) any later version.
00008   
00009   This program is distributed in the hope that it will be useful,
00010   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012   GNU General Public License for more details. (http://www.gnu.org)
00013   
00014   You should have received a copy of the GNU General Public License
00015   along with this program; if not, write to the Free Software
00016   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00017   
00018   $Id: Collider.cpp 1031 2004-02-11 20:46:36Z jungd $
00019   $Revision: 1.3 $
00020   $Date: 2004-02-11 15:46:36 -0500 (Wed, 11 Feb 2004) $
00021   $Author: jungd $
00022  
00023 ****************************************************************************/
00024 
00025 #include <physics/Collider>
00026 
00027 using physics::Collider;
00028 
00029 using base::dynamic_cast_ref;
00030 using physics::Collidable;
00031 using physics::CollidableGroup;
00032 
00033 
00034 
00035 Collider::Collider()
00036  : enabled(true)
00037 {
00038 }
00039 
00040 
00041 Collider::~Collider()
00042 {
00043 }
00044 
00045 
00046 void Collider::collide(ref<const Collidable> collidable)
00047 {
00048   if (!isCollisionEnabled()) return;
00049 
00050   if (!instanceof(*collidable, const CollidableGroup))
00051     return; // can't collide one Collidable with anything - do nothing
00052 
00053   ref<const CollidableGroup> group(narrow_ref<const CollidableGroup>(collidable));
00054   
00055   if (!group->isChildIntercollisionEnabled()) return;
00056   
00057   CollidableGroup::const_iterator_const c1 = group->const_begin();
00058   CollidableGroup::const_iterator_const end = group->const_end();
00059   while (c1 != end) {
00060     ref<const Collidable> collidable1(*c1);
00061     
00062     CollidableGroup::const_iterator_const c2 = group->const_begin();
00063     while (c2 != end) {
00064       ref<const Collidable> collidable2(*c2);
00065       
00066       if (&(*collidable1) < &(*collidable2))   // i.e. don't collide if c1==c2 or c2,c1 if already did c1,c2 
00067         collide(collidable1, collidable2);
00068       
00069       ++c2;
00070     }
00071     
00072     ++c1;
00073   }
00074 }
00075 
00076 
00077 void Collider::notifyListeners(ref<const Collidable> collidable1, ref<const Collidable> collidable2)
00078 {
00079   if (!isCollisionEnabled()) return;
00080   
00081   ListenerList::const_iterator l = listeners.begin();
00082   ListenerList::const_iterator end = listeners.end();
00083   while (l != end) {
00084     (*l)->potentialCollision(collidable1, collidable2);
00085     ++l;
00086   }
00087 }
00088 
00089 
00090 void Collider::resetListeners()
00091 {
00092   if (!isCollisionEnabled()) return;
00093   
00094   ListenerList::const_iterator l = listeners.begin();
00095   ListenerList::const_iterator end = listeners.end();
00096   while (l != end) {
00097     (*l)->reset();
00098     ++l;
00099   }
00100 }
00101 

Generated on Thu Jul 29 15:56:22 2004 for OpenSim by doxygen 1.3.6