Pick object in 3D world
2 posters
Pick object in 3D world
i have take off the code from bullet engine example to show how to pick a object in 3D world :
the code works, but it can't pick the correct object .
anyone can check the code and tell me why ?
- 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 ?
sw- Posts : 73
Join date : 2008-10-12
Re: Pick object in 3D world
Check out the post of Matt, he write one based on the setUserPointer property of the _btRigidBody to return the _SIO2object handle...
Re: Pick object in 3D world
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
sw- Posts : 73
Join date : 2008-10-12
Similar topics
» Physics-based picking
» pick by color
» physics object inside another physics object
» Use object only for collision
» how to not rotate around object
» pick by color
» physics object inside another physics object
» Use object only for collision
» how to not rotate around object
Permissions in this forum:
You cannot reply to topics in this forum