00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include "spherecollisioncomponent.h"
00021
00022 #include "gameobject.h"
00023 #include "game.h"
00024 #include "scene.h"
00025
00026 #include <core/debughelper.h>
00027
00028 REGISTER_OBJECTTYPE( GluonEngine, SphereCollisionComponent )
00029
00030 using namespace GluonEngine;
00031
00032 class SphereCollisionComponent::SphereCollisionComponentPrivate
00033 {
00034 public:
00035 SphereCollisionComponentPrivate() :
00036 collisionGroup( 0 ),
00037 radius( 1.0f ),
00038 collides( 0 )
00039 {
00040 }
00041
00042 int collisionGroup;
00043 float radius;
00044 GameObject* collides;
00045
00046 int componentType;
00047 const char* typeName;
00048
00049 QVector<Component*> collisionComponents;
00050 };
00051
00052 SphereCollisionComponent::SphereCollisionComponent( QObject* parent )
00053 : Component( parent )
00054 , d( new SphereCollisionComponentPrivate )
00055 {
00056 d->componentType = qMetaTypeId<GluonEngine::SphereCollisionComponent>();
00057 d->typeName = staticMetaObject.className();
00058 }
00059
00060 SphereCollisionComponent::~SphereCollisionComponent()
00061 {
00062 delete d;
00063 }
00064
00065 QString SphereCollisionComponent::category() const
00066 {
00067 return QString( "Physics" );
00068 }
00069
00070 void SphereCollisionComponent::start()
00071 {
00072 d->collisionComponents = gameObject()->parentGameObject()->findComponentsInChildrenByType( d->componentType ).toVector();
00073
00074 foreach( Component * component, d->collisionComponents )
00075 {
00076 connect( component, SIGNAL( destroyed( QObject* ) ), SLOT( componentDestroyed( QObject* ) ) );
00077 static_cast<SphereCollisionComponent*>( component )->addComponent( this );
00078 }
00079 }
00080
00081 void SphereCollisionComponent::update( int elapsedMilliseconds )
00082 {
00083 Q_UNUSED( elapsedMilliseconds )
00084
00085 d->collides = 0;
00086
00087
00088 QVector3D position = gameObject()->position();
00089
00090 position.setZ( 0 );
00091
00092 float radius = d->radius * d->radius;
00093
00094
00095 const int componentCount = d->collisionComponents.count();
00096 Component** data = d->collisionComponents.data();
00097 for( int i = 0; i < componentCount; ++i )
00098 {
00099 Component* component = data[i];
00100
00101
00102 if( component->metaObject()->className() != d->typeName )
00103 continue;
00104
00105 SphereCollisionComponent* sphere = static_cast< SphereCollisionComponent* >( component );
00106 if( sphere && sphere != this )
00107 {
00108
00109 if( sphere->collisionGroup() == d->collisionGroup )
00110 {
00111
00112 QVector3D otherPosition = component->gameObject()->position();
00113
00114 position.setZ( 0 );
00115
00116
00117 float otherRadius = sphere->radius();
00118
00119
00120
00121 float dist = ( otherPosition - position ).lengthSquared();
00122
00123
00124
00125 if( dist < ( otherRadius + radius ) )
00126 {
00127 d->collides = component->gameObject();
00128 }
00129 }
00130 }
00131 }
00132 }
00133
00134 void SphereCollisionComponent::stop()
00135 {
00136 }
00137
00138 int SphereCollisionComponent::collisionGroup() const
00139 {
00140 return d->collisionGroup;
00141 }
00142
00143 float SphereCollisionComponent::radius() const
00144 {
00145 return d->radius;
00146 }
00147
00148 bool SphereCollisionComponent::isColliding() const
00149 {
00150 return d->collides != 0;
00151 }
00152
00153 QObject* SphereCollisionComponent::collidesWith() const
00154 {
00155 return d->collides;
00156 }
00157
00158 void SphereCollisionComponent::setCollisionGroup( int group )
00159 {
00160 d->collisionGroup = group;
00161 }
00162
00163 void SphereCollisionComponent::setRadius( float radius )
00164 {
00165 d->radius = radius;
00166 }
00167
00168 void SphereCollisionComponent::componentDestroyed( QObject* obj )
00169 {
00170 if( !obj )
00171 return;
00172
00173 Component* comp = static_cast<Component*>( obj );
00174 if( d->collisionComponents.contains( comp ) )
00175 d->collisionComponents.remove( d->collisionComponents.indexOf( comp ) );
00176 }
00177
00178 void SphereCollisionComponent::addComponent( SphereCollisionComponent* comp )
00179 {
00180 if( comp )
00181 {
00182 if( !d->collisionComponents.contains( comp ) )
00183 {
00184 d->collisionComponents.append( comp );
00185 connect( comp, SIGNAL( destroyed( QObject* ) ), this, SLOT( componentDestroyed( QObject* ) ) );
00186 }
00187 }
00188 }
00189
00190
00191 Q_EXPORT_PLUGIN2( gluon_component_spherecollision, GluonEngine::SphereCollisionComponent )
00192
00193 #include "spherecollisioncomponent.moc"