I assume there is probably something like this built into Irrlicht somewhere. Which is why I had such a hard time putting it together from the board. Or it could just be that this is insanely easy for most here, but I'm sure somewhere there's a poor sap like me struggling to learn.
Code: Select all
irr::core::vector3df getMouseIntersect2( const irr::core::vector3df &ptCurPos, const irr::core::vector3df &ptAxis, const irr::core::position2di ptMouse )
{
// Is mouse in the window?
if ( 0 > ptMouse.X || 0 > ptMouse.Y )
return ptCurPos;
// Get mouse line
irr::core::line3df ml =
m_pSm->getSceneCollisionManager()->
getRayFromScreenCoordinates( ptMouse, GetCamera() );
irr::core::vector3df normal, ptNew;
// Pick an axis we don't need as the normal for the plane
if ( !ptAxis.X ) normal.X = 1;
else if ( !ptAxis.Y ) normal.Y = 1;
else if ( !ptAxis.Z ) normal.Z = 1;
// Plane faces camera for all axis
else normal = ml.getVector().normalize();
// Get mouse line / plane intersection
if ( !irr::core::plane3df( ptCurPos, normal ).
getIntersectionWithLine( ml.getMiddle(), ml.getVector(), ptNew ) )
return ptCurPos;
// Revert the axis we don't want to change
if ( !ptAxis.X ) ptNew.X = ptCurPos.X;
if ( !ptAxis.Y ) ptNew.Y = ptCurPos.Y;
if ( !ptAxis.Z ) ptNew.Z = ptCurPos.Z;
return ptNew;
}
Code: Select all
new_pos = getMouseIntersect( current_position, vector3df( 1, 0, 0 ), mouse_pos );
Code: Select all
new_pos = getMouseIntersect( current_position, vector3df( 0, 1, 1 ), mouse_pos );
Code: Select all
void setAbsolutePosition( irr::scene::ISceneNode *pNode, irr::core::vector3df pos )
{ if ( !pNode ) return;
irr::scene::ISceneNode *pParent = pNode->getParent();
if ( !pParent ) pNode->setPosition( pos );
else
{ irr::core::vector3df v( pos - pParent->getAbsolutePosition() );
pParent->getAbsoluteTransformation().inverseRotateVect( v );
pNode->setPosition( v );
} // end else
pNode->updateAbsolutePosition();
}
Code: Select all
setAbsolutePosition( pNode,
getMouseIntersect( pNode->getAbsolutePosition(), vector3df( 1, 0, 0 ), mouse_pos ) );
