Page 1 of 1

Move node to given screen coords, same distance to cam.

Posted: Tue May 22, 2007 8:11 pm
by cederron
Hello all,
I want to move a node specyfing screen x, y coordinates, the dificult thing, for me, is that it has to conservate the distance to the camera.

I have tried a few variations of the following code, without succes:

Code: Select all

	line3d<f32> line = pDevice->getSceneManager()->getSceneCollisionManager()->
		getRayFromScreenCoordinates(position2d<s32>(ScreenX, ScreenY));
	vector3df lineStartPos = line.start;
	vector3df camPos = pDevice->getSceneManager()->getActiveCamera()->getPosition();
	vector3df nodePos = pNode->getPosition();
	vector3df nodeDist = nodePos - camPos;
	vector3df lineVec = line.getVector().normalize();
	vector3df sumVec = nodeDist*lineVec;
	vector3df destPos = lineStartPos + sumVec;
	pNode->setPosition(destPos);

I'm really bad at math so any help is apreciated, thank you.

Posted: Wed May 23, 2007 10:05 pm
by raven_coda
Try this

Code: Select all

line3df line = pDevice->getSceneManager()->getSceneCollisionManager()->
	getRayFromScreenCoordinates(position2d<s32>(ScreenX, ScreenY)); 
	vector3df camPos = pDevice->getSceneManager()->getActiveCamera()->getPosition(); 
    vector3df nodePos = pNode->getPosition(); 
    vector3df nodeDist = nodePos - camPos; 
    vector3df destPos=line.getVector();
    destPos.normalize();
    destPos*=nodeDist.getLength();
	pNode->setPosition(destPos+camPos); 

Posted: Thu May 24, 2007 9:41 am
by cederron
raven_coda wrote:Try this

It Works! Amazing man, thank you!