Your code seems sound up to the last line. You appear to be setting the rotation to a value that only changes as the mouse position changes. If I understand you correctly, you want the node to continue to rotate right as long as the mouse is near the right edge of the window.
I haven't tested this yet, but I'm pretty sure it does what you want. You need to calculate the elapsed frame time elapsedSeconds, and you may need to invert the Y offset.
#if 0
// this block will continue to rotate node when mouse is outside window
core::position2df relativePosition (cursor->getRelativePosition());
// the values returned by getRelativePosition() are supposed to be
// clamped to (0, 0) and (1, 1), but they aren't. do that here.
relativePosition.X = core::clamp (relativePosition.X, 0.f, 1.f);
relativePosition.Y = core::clamp (relativePosition.Y, 0.f, 1.f);
{
#else
// this block will not rotate node when mouse is outside window
const core::position2di mousePosition = cursor->getPosition();
const core::dimension2di windowSize = driver->getCurrentRenderTargetSize();
if ((0 < mousePosition.X && mousePosition.X < windowSize.Width) &&
(0 < mousePosition.Y && mousePosition.Y < windowSize.Height))
{
// fractional screen position, upper left is (0, 0) and lower right is (1, 1)
const core::vector2df relativePosition (1.f * mousePosition.X / windowSize.Width,
1.f * mousePosition.Y / windowSize.Height);
#endif
// use a vector2d to simplify some of the code below
const core::vector2df rotateFactor0to1 (relativePosition.X, 1.f - relativePosition.Y);
// lerp from [0-1] to [-1, 1]
core::vector2df rotateFactorM1toP1 ((rotateFactor0to1 * 2.f) - 1.f);
// apply a factor to accelerate rotation near edges of screen slightly
rotateFactorM1toP1.X = powf (rotateFactorM1toP1.X, 3);
rotateFactorM1toP1.Y = powf (rotateFactorM1toP1.Y, 3);
// clamp that to .01 so rotation stops near center of screen
if (fabs (rotateFactorM1toP1.X) < .01f)
rotateFactorM1toP1.X = 0.f;
if (fabs (rotateFactorM1toP1.Y) < .01f)
rotateFactorM1toP1.Y = 0.f;
const core::vector2df rotateSpeed (180.f, 180.f);
const core::vector2df rotateDelta (rotateFactorM1toP1 * (rotateSpeed * elapsedSeconds));
// now apply the rotation delta
core::vector3df rotation = node->getRotation ();
// not a typo. up/down in screen coordinates [the Y axis]
// is a rotation on the X axis.
rotation.X -= rotateDelta.Y;
rotation.Y += rotateDelta.X;
// keep angles between -180 and 180 to make it readable for debugging
while (180.f < rotation.X)
rotation.X -= 360.f;
while (rotation.X < 180.f)
rotation.X += 360.f;
while (180.f < rotation.Y)
rotation.Y -= 360.f;
while (rotation.Y < 180.f)
rotation.Y += 360.f;
node->setRotation (rotation);
}
Travis
Last edited by vitek on Mon Jan 21, 2008 7:51 pm, edited 2 times in total.
vitek wrote:Your code seems sound up to the last line. You appear to be setting the rotation to a value that only changes as the mouse position changes. If I understand you correctly, you want the node to continue to rotate right as long as the mouse is near the right edge of the window.
If you multiple a negative by a negative (pow 2) you get a positive and thus no left or up.
The ship rotates up and down on Z not X .
Those were the only problems with the code (except for a few misnamed variables).
The control works with a fixed camera now.
The major problem is getting a chase camera working in this arrangement. (parenting it to the back of the ship does not work well with this control scheme. The angles do not go back and thus they cause a slight problem. I have no y but it goes up and down. Also when it goes up or down a certain amount the ship flips and the y is inverted. )
Thanks for you help with getting me to this point anyway.
If you multiple a negative by a negative (pow 2) you get a positive and thus no left or up.
Whoops! I knew I started out with a cube in there for a reason...
The ship rotates up and down on Z not X.
Usually the Z axis points forward, the Y axis up, and the X axis to the side. Up and down on the screen would be the Y axis on screen, and a rotation on the X axis in 3D. Left and right is the X axis on screen, but the Y axis in 3D. I've now tested this, and it does work correctly.
I have no y but it goes up and down. Also when it goes up or down a certain amount the ship flips and the y is inverted. )
The Y axis is the yaw rotation. With no Y, you will have X and Z, so your ship will pitch and roll.
If you multiple a negative by a negative (pow 2) you get a positive and thus no left or up.
Whoops! I knew I started out with a cube in there for a reason...
The ship rotates up and down on Z not X.
Usually the Z axis points forward, the Y axis up, and the X axis to the side. Up and down on the screen would be the Y axis on screen, and a rotation on the X axis in 3D. Left and right is the X axis on screen, but the Y axis in 3D. I've now tested this, and it does work correctly.
My ship is the reverse with respect to Z and X.
vitek wrote:
I have no y but it goes up and down. Also when it goes up or down a certain amount the ship flips and the y is inverted. )
The Y axis is the yaw rotation. With no Y, you will have X and Z, so your ship will pitch and roll.
I've updated the code above for those who care.
Travis
I have managed to get a camera by itself working except the Y axis inverts at 90 and 270 degrees. (trying to figure out to invert the screen)