Like many others before me I was experimenting with creating a flight/space-sim type camera, and ran into a number of issues with camera positioning and rotation. After searching the forums and Internet I found some workarounds, but no real solutions. On investigating I have found a number of bugs in the CCameraSceneNode code that are responsible for these problems.
The code provided by arras in http://irrlicht.sourceforge.net/phpBB2/ ... ht&start=0 works specifically because it compensates for these bugs, and is mostly what I've used to update CCameraSceneNode.
The bugs.
First, there are two issues in setTarget. Here is the original code.
Code: Select all
//! sets the look at target of the camera
//! \param pos: Look at target of the camera.
void CCameraSceneNode::setTarget(const core::vector3df& pos)
{
Target = pos;
if(TargetAndRotationAreBound)
{
const core::vector3df toTarget = Target - getAbsolutePosition();
ISceneNode::setRotation(toTarget.getHorizontalAngle());
}
}
Secondly, changing the target changes the camera's up vector, but no account is made for this.
Next, there are similar problems in setRotation. Original code:
Code: Select all
//! Sets the rotation of the node.
void CCameraSceneNode::setRotation(const core::vector3df& rotation)
{
if(TargetAndRotationAreBound)
Target = getAbsolutePosition() + rotation.rotationToDirection();
ISceneNode::setRotation(rotation);
}
The second problem here is again the up vector. Changing the rotation changes the up vector, but this is not handled.
A related issue exists in setUpVector:
Code: Select all
void CCameraSceneNode::setUpVector(const core::vector3df& pos)
{
UpVector = pos;
}
Another, minor issue exists in the code for OnRegisterSceneNode:
Code: Select all
void CCameraSceneNode::OnRegisterSceneNode()
{
<snip>
// if upvector and vector to the target are the same, we have a
// problem. so solve this problem:
core::vector3df up = UpVector;
up.normalize();
f32 dp = tgtv.dotProduct(up);
if ( core::equals(fabsf(dp), 1.f) )
{
up.X += 0.5f;
}
<snip>
Using the code from arras as referenced above I have updated the code for CCameraSceneNode::setRotation() and CCamerSceneNode::setTarget(). In addition I have added two new member functions, moveCamera() and rotateCamera(), that move and rotate the camera by a relative amount.
These changes make implementing a flight-sim type camera intuitive and easy. My new code will be posted in the next couple posts, along with a demo program.
Steve