diff options
Diffstat (limited to 'source/gameengine/Ketsji/KX_RaySensor.cpp')
-rw-r--r-- | source/gameengine/Ketsji/KX_RaySensor.cpp | 196 |
1 files changed, 92 insertions, 104 deletions
diff --git a/source/gameengine/Ketsji/KX_RaySensor.cpp b/source/gameengine/Ketsji/KX_RaySensor.cpp index c60b2171cab..fa5f5215aa4 100644 --- a/source/gameengine/Ketsji/KX_RaySensor.cpp +++ b/source/gameengine/Ketsji/KX_RaySensor.cpp @@ -41,8 +41,11 @@ #include "KX_GameObject.h" #include "KX_Scene.h" -#include "SumoPhysicsEnvironment.h" -#include "KX_SumoPhysicsController.h" +#include "KX_RayCast.h" +#include "PHY_IPhysicsEnvironment.h" +#include "PHY_IPhysicsController.h" +#include "KX_IPhysicsController.h" + #ifdef HAVE_CONFIG_H #include <config.h> @@ -101,7 +104,47 @@ bool KX_RaySensor::IsPositiveTrigger() return result; } +bool KX_RaySensor::RayHit(KX_ClientObjectInfo* info, MT_Point3& hit_point, MT_Vector3& hit_normal, void* const data) +{ + KX_GameObject* obj = (KX_GameObject*)GetParent(); + SCA_IObject *hitgameobj = info->m_gameobject; + if (hitgameobj == obj || info->m_type > KX_ClientObjectInfo::ACTOR) + { + // false hit + return false; + } + + bool bFound = false; + if (m_propertyname.Length() == 0) + { + bFound = true; + } + else + { + if (m_bFindMaterial) + { + if (info->m_auxilary_info) + { + bFound = (m_propertyname== ((char*)info->m_auxilary_info)); + } + } + else + { + bFound = hitgameobj->GetProperty(m_propertyname) != NULL; + } + } + if (bFound) + { + m_rayHit = true; + m_hitObject = hitgameobj; + m_hitPosition = hit_point; + m_hitNormal = hit_normal; + + } + + return true; +} bool KX_RaySensor::Evaluate(CValue* event) { @@ -168,115 +211,64 @@ bool KX_RaySensor::Evaluate(CValue* event) MT_Point3 topoint = frompoint + (m_distance) * todir; MT_Point3 resultpoint; MT_Vector3 resultnormal; - bool ready = false; - SumoPhysicsEnvironment *spe = dynamic_cast<SumoPhysicsEnvironment *>(m_scene->GetPhysicsEnvironment()); - if (!spe) + PHY_IPhysicsEnvironment* physics_environment = m_scene->GetPhysicsEnvironment(); + if (!physics_environment) { std::cout << "WARNING: Ray sensor " << GetName() << ": There is no physics environment!" << std::endl; std::cout << " Check universe for malfunction." << std::endl; return false; } - SM_Scene *scene = spe->GetSumoScene(); - KX_SumoPhysicsController *spc = dynamic_cast<KX_SumoPhysicsController *>(obj->GetPhysicsController()); + KX_IPhysicsController* physics_controller = obj->GetPhysicsController(); + + // Use the parent's physics controller if obj has no physics controller. KX_GameObject *parent = obj->GetParent(); - if (!spc && parent) - spc = dynamic_cast<KX_SumoPhysicsController *>(parent->GetPhysicsController()); + if (!physics_controller && parent) + physics_controller = parent->GetPhysicsController(); + if (parent) parent->Release(); - SM_Object *thisObj = spc?spc->GetSumoObject():NULL; - - do { - SM_Object* hitObj = scene->rayTest(thisObj, - frompoint, - topoint, - resultpoint, - resultnormal); - - if (hitObj) - { - - KX_ClientObjectInfo* info = static_cast<KX_ClientObjectInfo*>(hitObj->getClientObject()); - bool bFound = false; - - if (!info) - { - std::cout<< "WARNING: Ray sensor " << GetName() << " cannot sense SM_Object " << hitObj << " - no client info.\n" << std::endl; - ready = true; - break; - } - - SCA_IObject *hitgameobj = info->m_gameobject; - - if (hitgameobj == obj || info->m_type > KX_ClientObjectInfo::ACTOR) - { - // false hit - KX_SumoPhysicsController *hitspc = dynamic_cast<KX_SumoPhysicsController *> (static_cast<KX_GameObject*> (hitgameobj) ->GetPhysicsController()); - if (hitspc) - { - /* We add 0.01 of fudge, so that if the margin && radius == 0., we don't endless loop. */ - MT_Scalar marg = 0.01 + hitspc->GetSumoObject()->getMargin(); - if (hitspc->GetSumoObject()->getShapeProps()) - { - marg += 2*hitspc->GetSumoObject()->getShapeProps()->m_radius; - } - - /* Calculate the other side of this object */ - MT_Point3 hitObjPos; - hitspc->GetWorldPosition(hitObjPos); - MT_Vector3 hitvector = hitObjPos - resultpoint; - if (hitvector.dot(hitvector) > MT_EPSILON) - { - hitvector.normalize(); - marg *= 2.*todir.dot(hitvector); - } - frompoint = resultpoint + marg * todir; - } else { - ready = true; - } - } - else - { - ready = true; - if (m_propertyname.Length() == 0) - { - bFound = true; - } - else - { - if (m_bFindMaterial) - { - if (info->m_auxilary_info) - { - bFound = (m_propertyname== ((char*)info->m_auxilary_info)); - } - } - else - { - bFound = hitgameobj->GetProperty(m_propertyname) != NULL; - } - } - - if (bFound) - { - m_rayHit = true; - m_hitObject = hitgameobj; - m_hitPosition = resultpoint; - m_hitNormal = resultnormal; - - } - } - } - else - { - ready = true; - } - } - while (!ready); - + + KX_RayCast::RayTest(physics_controller, physics_environment, frompoint, topoint, resultpoint, resultnormal, KX_RayCast::Callback<KX_RaySensor>(this)); + +// do { +// PHY__Vector3 respos; +// PHY__Vector3 resnormal; +// +// PHY_IPhysicsController* hitCtrl = spe->rayTest(physCtrl, +// frompoint.x(),frompoint.y(),frompoint.z(), +// topoint.x(),topoint.y(),topoint.z(), +// respos[0],respos[1],respos[2], +// resnormal[0],resnormal[1],resnormal[2]); +// +// if (hitCtrl) +// { +// +// resultpoint = MT_Vector3(respos); +// resultnormal = MT_Vector3(resnormal); +// KX_ClientObjectInfo* info = static_cast<KX_ClientObjectInfo*>(hitCtrl->getNewClientInfo()); +// bool bFound = false; +// +// if (!info) +// { +// std::cout<< "WARNING: Ray sensor " << GetName() << " cannot sense PHY_IPhysicsController - no client info.\n" << std::endl; +// ready = true; +// break; +// } +// +// +// +// } +// else +// { +// ready = true; +// } +// } +// while (!ready); +// /* now pass this result to some controller */ - if (m_rayHit) + if (m_rayHit) { if (!m_bTriggered) { @@ -284,10 +276,6 @@ bool KX_RaySensor::Evaluate(CValue* event) result = true; m_bTriggered = true; } - else - { - - } } else { |