Happy coding
![Smile :)](./images/smilies/icon_smile.gif)
[edit 17.02.2007]
code below was updated and should work with Irrlicht 1.2 now.
You may also look at example code (small demo) on second page of this post.
Code: Select all
//--- set camera to behave as cockpit camera of ship ---
void makeCockpit(irr::scene::ICameraSceneNode *camera, //camera
irr::scene::ISceneNode *node, //scene node (ship)
irr::core::vector3df offset) //relative position of camera to node (ship)
{
// get transformation matrix of node
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
// transform forward vector of camera
irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
m.transformVect(frv);
// transform upvector of camera
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
// transform camera offset (thanks to Zeuss for finding it was missing)
m.transformVect(offset);
// set camera
camera->setPosition(node->getPosition() + offset); //position camera in front of the ship
camera->setUpVector(upv); //set up vector of camera
camera->setTarget(node->getPosition() + frv); //set target of camera (look at point) (thx Zeuss for correcting it)
// update absolute position
camera->updateAbsolutePosition();
}
Code: Select all
//--- rotate node relative to its current rotation -used in turn,pitch,roll ---
void rotate(irr::scene::ISceneNode *node, irr::core::vector3df rot)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
irr::core::matrix4 n;
n.setRotationDegrees(rot);
m *= n;
node->setRotation( m.getRotationDegrees() );
node->updateAbsolutePosition();
}
//--- turn ship left or right ---
void turn(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, rot, 0.0f) );
}
//--- pitch ship up or down ---
void pitch(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(rot, 0.0f, 0.0f) );
}
//--- roll ship left or right ---
void roll(irr::scene::ISceneNode *node, irr::f32 rot)
{
rotate(node, irr::core::vector3df(0.0f, 0.0f, rot) );
}
Code: Select all
//--- move ship acording to its rotation ---
void move(irr::scene::ISceneNode *node, //node to move
irr::core::vector3df vel) //velocity vector
// for example to move node 10 units forward use vector3df(0,0,10)
{
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vel);
node->setPosition(node->getPosition() + vel);
node->updateAbsolutePosition();
}
Code: Select all
//--- move ship acording to its rotation ---
void move(irr::scene::ISceneNode *node, //node to move
irr::core::vector3df vel) //velocity vector
// for example to move node 10 units forward use vector3df(0,0,10)
{
//Zeuss - Has to be getRotation and not getRelativeTransformation
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vel);
node->setPosition(node->getPosition() + vel);
}
Code: Select all
//--- set camera to behave as cockpit camera of ship ---
void makeCockpit(irr::scene::ICameraSceneNode *camera, //camera
irr::scene::ISceneNode *node, //scene node (ship)
irr::core::vector3df offset) //relative position of camera to node (ship)
{
//get rotation matrix of node - Zeuss must be getRotation not getRelativeTransformation
irr::core::matrix4 m;
m.setRotationDegrees(m_node->getRotation());
// transform forward vector of camera
irr::core::vector3df frv = irr::core::vector3df (0.0f, 0.0f, 1.0f);
m.transformVect(frv);
// transform upvector of camera
irr::core::vector3df upv = irr::core::vector3df (0.0f, 1.0f, 0.0f);
m.transformVect(upv);
// transform camera offset (thanks to Zeuss for finding it was missing)
m.transformVect(offset);
// set camera
camera->setPosition(node->getPosition() + offset); //position camera in front of the ship
camera->setUpVector(upv); //set up vector of camera >> Zeuss - tested with +node->getPostion() and it didnt work, but this works fine.
camera->setTarget(node->getPosition() + frv); //set target of camera (look at point) >> Zeuss - Dont forget to add the node positiob
}
well actualy it is not. Rotation you get is in Euler angles, called afther one mathematican. Try to search internet about it, since its quit inportant, that you understand hove they work.In the form (pitch,turn,roll)
Code: Select all
irr::core::vector3df vector = irr::core::vector3df(0,0,1);
irr::core::matrix4 m;
m.setRotationDegrees(node->getRotation());
m.transformVect(vector);
Code: Select all
using System;
namespace SpaceFlight {
public class Controls {
public Controls() {
}
//--- move ship acording to its rotation ---
public static void Move(Irrlicht.Scene.ISceneNode node, Irrlicht.Core.Vector3D vel) {
Irrlicht.Core.Matrix4 m = new Irrlicht.Core.Matrix4();
m.SetRotationDegrees( node.Rotation );
m.TransformVect( ref vel );
Irrlicht.Core.Vector3D Pos = node.Position;
Pos.X += vel.X;
Pos.Y += vel.Y;
Pos.Z += vel.Z;
node.Position = Pos;
}
//--- rotate node relative to its current rotation -used in turn,pitch,roll ---
public static void Rotate(Irrlicht.Scene.ISceneNode node, Irrlicht.Core.Vector3D rot) {
Irrlicht.Core.Matrix4 m = node.RelativeTransformation;
Irrlicht.Core.Matrix4 n = new Irrlicht.Core.Matrix4();
n.SetRotationDegrees(rot);
m *= n;
node.Rotation = getRotationDegrees( m );
}
//--- turn ship left or right ---
public static void Turn(Irrlicht.Scene.ISceneNode node, float rot) {
Rotate(node, new Irrlicht.Core.Vector3D(0.0f, rot, 0.0f) );
}
//--- pitch ship up or down ---
public static void Pitch(Irrlicht.Scene.ISceneNode node, float rot) {
Rotate(node, new Irrlicht.Core.Vector3D(rot, 0.0f, 0.0f) );
}
//--- roll ship left or right ---
public static void Roll(Irrlicht.Scene.ISceneNode node, float rot) {
Rotate(node, new Irrlicht.Core.Vector3D(0.0f, 0.0f, rot) );
}
// Missing getRotationDegrees Function for Matrix4 in .NET
private static Irrlicht.Core.Vector3D getRotationDegrees( Irrlicht.Core.Matrix4 mat ) {
const double GRAD_PI = 180.000f/Math.PI;
double Y = - Math.Asin( mat.get_M(2,0) );
double D = Y;
double C = Math.Cos(Y);
Y *= GRAD_PI;
double rotx, roty, X, Z;
if ( Math.Abs(C) >0.0005f) {
rotx = mat.get_M(2,2) / C;
roty = mat.get_M(2,1) / C;
X = Math.Atan2( roty, rotx ) * GRAD_PI;
rotx = mat.get_M(0,0) / C;
roty = mat.get_M(1,0) / C;
Z = Math.Atan2( roty, rotx ) * GRAD_PI;
} else {
X = 0.0f;
rotx = mat.get_M(1,1);
roty = -mat.get_M(0,1);
Z = Math.Atan2( roty, rotx ) * (float)GRAD_PI;
}
if (X < 0.00) X += 360.00;
if (Y < 0.00) Y += 360.00;
if (Z < 0.00) Z += 360.00;
return new Irrlicht.Core.Vector3D((float)X,(float)Y,(float)Z);
}
}
}