Pick object in 3D world

View previous topic View next topic Go down

Pick object in 3D world

Post  sw on Tue Nov 25, 2008 1:24 am

i have take off the code from bullet engine example to show how to pick a object in 3D world :


Code:


btVector3 GetRayTo(int x,int y, float nearPlane, float farPlane, btVector3 cameraUp, btVector3 CameraPosition, btVector3 CameraTargetPosition)
{
   float top = 1.f;
   float bottom = -1.f;
   float tanFov = (top - bottom) * 0.5f / nearPlane;
   float fov = 2.0 * atanf (tanFov);
   
   btVector3   rayFrom = CameraPosition;
   btVector3 rayForward = (CameraTargetPosition - CameraPosition);
   rayForward.normalize();
   //float fPlane = 10000.f;
   rayForward *= farPlane;
   
   btVector3 rightOffset;
   btVector3 vertical = cameraUp;
   
   btVector3 hor;
   
   hor = rayForward.cross(vertical);
   hor.normalize();
   vertical = hor.cross(rayForward);
   vertical.normalize();
   
   float tanfov = tanf(0.5f*fov);
   
   float aspect = (float)320 / (float)480;
   
   hor *= 2.f * farPlane * tanfov;
   vertical *= 2.f * farPlane * tanfov;
   
   if (aspect<1)
   {
      hor/=aspect;
   } else
   {
      vertical*=aspect;
   }
   
   btVector3 rayToCenter = rayFrom + rayForward;
   btVector3 dHor = hor * 1.f/float(480);
   btVector3 dVert = vertical * 1.f/float(320);
   
   btVector3 rayTo = rayToCenter - 0.5f * hor + 0.5f * vertical;
   rayTo += x * dHor;
   rayTo -= y * dVert;
   return rayTo;
}


btVector3 PicRay;

btVector3 m_cameraUp(0,0,1); // Z Axis is UP
SIO2object * Pickobject()
{
   //float rele = m_ele * 0.01745329251994329547;// rads per deg
   //float razi = m_azi * 0.01745329251994329547;// rads per deg
   
   
   //btQuaternion rot(m_cameraUp,razi);
   btVector3 m_cameraPosition (sio2->_SIO2camera->pos->x ,sio2->_SIO2camera->pos->y, sio2->_SIO2camera->pos->z ) ;
   btVector3 m_cameraTargetPosition (sio2->_SIO2camera->tar->x , sio2->_SIO2camera->tar->y ,sio2->_SIO2camera->tar->z );
   

   PicRay = GetRayTo(sio2->_SIO2window->touch[ 0 ]->x, sio2->_SIO2window->touch[ 0 ]->y,
              1.0f, 1000.0f,m_cameraUp, m_cameraPosition,
              m_cameraTargetPosition);
   btCollisionWorld::ClosestRayResultCallback rayCallback(m_cameraPosition, PicRay);   
   sio2->_SIO2physic->_btSoftRigidDynamicsWorld->getCollisionWorld()->rayTest(m_cameraPosition, PicRay, rayCallback);
   if (rayCallback.hasHit())
   {
      vec3 HitObjectPos;
      HitObjectPos.x = rayCallback.m_hitPointWorld.x();
      HitObjectPos.y = rayCallback.m_hitPointWorld.y();
      HitObjectPos.z = rayCallback.m_hitPointWorld.z();

      for ( int i =0 ;i <6 ;i++)
      {
         if ( sio2Distance( &HitObjectPos , objects[i].kkkobject->pos )<1 )
            return tobjects[i].kkkobject ;
      
      }
      
   }
   
   return NULL;

}




the code works, but it can't pick the correct object .
anyone can check the code and tell me why ?
avatar
sw

Posts : 73
Join date : 2008-10-12

View user profile

Back to top Go down

Re: Pick object in 3D world

Post  sio2interactive on Tue Nov 25, 2008 3:06 am

Check out the post of Matt, he write one based on the setUserPointer property of the _btRigidBody to return the _SIO2object handle...
avatar
sio2interactive

Posts : 1526
Join date : 2008-08-26
Age : 38
Location : Shanghai

View user profile http://sio2interactive.com

Back to top Go down

Re: Pick object in 3D world

Post  sw on Tue Nov 25, 2008 6:14 am

sio2interactive wrote:Check out the post of Matt, he write one based on the setUserPointer property of the _btRigidBody to return the _SIO2object handle...

OK , that's good Smile
avatar
sw

Posts : 73
Join date : 2008-10-12

View user profile

Back to top Go down

Re: Pick object in 3D world

Post  Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum