Code: Select all
void TestComponet::move(const Ogre::Vector2& currentMousePosition, const Ogre::Vector2& oldMousePosition)
{
Ogre::Ray ray = camera_->getCameraToViewportRay(currentMousePosition.x, currentMousePosition.y);
Ogre::Vector3 objectPosition = cvt(collisionObject_->getWorldTransform().getOrigin()); // cvt function convert bullet to ogre and ogre to bullet (vectors, quaternions)
Ogre::Real a = (objectPosition - ray.getOrigin()).length();
Ogre::Real b = (currentMousePosition - oldMousePosition).length();
Ogre::Real distance = sqrt(a*a + b*b);
Ogre::Vector3 newPosition = ray.getPoint(distance);
btTransform tr = collisionObject_->getWorldTransform();
tr.setOrigin(cvt(newPosition));
collisionObject_->setWorldTransform(tr);
entityNode_->setPosition(newPosition);
}
Code: Select all
void RenderWidget::_mouseRaycast(Ogre::Real x, Ogre::Real y)
{
Ogre::Ray ray = camera_->getCameraToViewportRay(x, y);
Ogre::Vector3 dest = ray.getPoint(FAR_CLIP_DISTANCE);
btVector3 rayFrom, rayTo;
rayFrom = cvt(ray.getOrigin());
rayTo = cvt(dest);
btCollisionWorld::ClosestRayResultCallback rayResultCallBack(rayFrom, rayTo);
collisionWorld_->rayTest(rayFrom, rayTo, rayResultCallBack);
if(rayResultCallBack.hasHit())
{
for(register int i = 0; i < SCROLLAREA_CONTENT_COUNT; i++)
{
if(components_[i]->isThisObject(rayResultCallBack.m_collisionObject))
{
if(activeComponent_ != components_[i])
{
prevActiveComponent_ = activeComponent_;
activeComponent_ = components_[i];
activeComponent_->setPickPosition(rayResultCallBack.m_hitPointWorld);
isActiveComponentChange_ = true;
}
else { isActiveComponentChange_ = false; activeComponent_->setPickPosition(rayResultCallBack.m_hitPointWorld); }
return;
}
}
}
else
{
prevActiveComponent_ = activeComponent_;
activeComponent_ = false;
isActiveComponentChange_ = true;
}
}