//--------------------------------------------------------------
ofxBulletRaycastData ofxBulletWorldSoft::raycastTest(float a_x, float a_y, short int a_filterMask) {
	
	ofVec3f castRay = _camera->screenToWorld( ofVec3f(a_x, a_y, 0) );
	castRay = castRay - _camera->getPosition();
	castRay.normalize();
	castRay *= 300;
	
	return raycastTest( _camera->getPosition(), castRay, a_filterMask);
}
//--------------------------------------------------------------
ofxBulletRaycastData ofxBulletWorldRigid::raycastTest(float a_x, float a_y, short int a_filterMask) {

    if(_camera == NULL) {
        ofLog( OF_LOG_ERROR, "ofxBulletWorldRigid :: raycastTest : must set the camera first!!");
        return ofxBulletRaycastData();
    }

    ofVec3f castRay = _camera->screenToWorld( ofVec3f(a_x, a_y, 0) );
    castRay = castRay - _camera->getPosition();
    castRay.normalize();
    castRay *= 1000;
    castRay += _camera->getPosition();

    return raycastTest( _camera->getPosition(), castRay, a_filterMask);
}
//--------------------------------------------------------------
// pulled from DemoApplication in the AllBulletDemos project included in the Bullet physics download //
void ofxBulletWorldSoft::checkMousePicking(float a_mousex, float a_mousey) {
	ofxBulletRaycastData data = raycastTest(a_mousex, a_mousey, _mouseFilterMask);
	if(data.bHasHit) {
		ofxBulletMousePickEvent cdata;
		cdata.setRaycastData(data);
		if (cdata.body != NULL) {
			btVector3 m_cameraPosition( _camera->getPosition().x, _camera->getPosition().y, _camera->getPosition().z );
			//other exclusions?
			if (!(cdata.body->isStaticObject() || cdata.body->isKinematicObject()) && bRegisterGrabbing) {
				_pickedBody = cdata.body; //btRigidBody //
				_pickedBody->setActivationState( DISABLE_DEACTIVATION );
				
				btTransform tr;
				tr.setIdentity();
				tr.setOrigin(btVector3(cdata.localPivotPos.x, cdata.localPivotPos.y, cdata.localPivotPos.z));
				btGeneric6DofConstraint* dof6 = new btGeneric6DofConstraint(*cdata.body, tr, false);
				dof6->setLinearLowerLimit(btVector3(0,0,0));
				dof6->setLinearUpperLimit(btVector3(0,0,0));
				dof6->setAngularLowerLimit(btVector3(0,0,0));
				dof6->setAngularUpperLimit(btVector3(0,0,0));
				
				world->addConstraint(dof6);
				_pickConstraint = dof6;
				
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,0);
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,1);
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,2);
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,3);
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,4);
				dof6->setParam(BT_CONSTRAINT_STOP_CFM,0.8,5);
				
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,0);
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,1);
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,2);
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,3);
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,4);
				dof6->setParam(BT_CONSTRAINT_STOP_ERP,0.1,5);
				
				gOldPickingDist  = ( btVector3(cdata.pickPosWorld.x, cdata.pickPosWorld.y, cdata.pickPosWorld.z) - m_cameraPosition).length();
				
				//cout << "ofxBulletWorldSoft :: checkMousePicking : adding a mouse constraint" << endl;
			}
			//cout << "ofxBulletWorldSoft :: checkMousePicking : selected a body!!!" << endl;
			ofNotifyEvent( MOUSE_PICK_EVENT, cdata, this );
		}
	}
}
//--------------------------------------------------------------
ofVec3f chinoWorld::getWorldPos() {
    ofxBulletRaycastData data = raycastTest(ofGetMouseX(), ofGetMouseY());
    return data.pickPosWorld;
}