Custom Fps camera
Custom Fps camera
i'm trying to make a custom FPS camera, because i'm using some odd controls (including several mice)
thing is , i only want ONE mouse to effect the P.o.V so the standard built in irrlicht fps camera doesn't work properly, and i'm NOT using a keyboard to control vertical movements (back, forward, left , right and jumping), but rather a different device
so how do i create a custom FPS camera in irrlictht that uses whatever inputs i give it? (even if they're windows or directx specific)
where is the event handler location for the fps camera , and if i find it could i modify it to suit my purposes?
thing is , i only want ONE mouse to effect the P.o.V so the standard built in irrlicht fps camera doesn't work properly, and i'm NOT using a keyboard to control vertical movements (back, forward, left , right and jumping), but rather a different device
so how do i create a custom FPS camera in irrlictht that uses whatever inputs i give it? (even if they're windows or directx specific)
where is the event handler location for the fps camera , and if i find it could i modify it to suit my purposes?
-
omar shaaban
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
well simply u create a custom camera scene node
that will create a camera in irrlicht then just assign an eveny handler to move it like
and for rotation u would make something like
crot is the camera rotation
mouse is the mouse postion
Code: Select all
smgr->addCameraSceneNode();
Code: Select all
if(up)
{
vector3df * cpos =camera->getpostion();
cpos.z+=1.0f;
}
camera->setpostion(cpos)
crot is the camera rotation
mouse is the mouse postion
Code: Select all
if (mouse.x >800)
{
crot.y+=1;
}
camera->setrotation(crot)
i'm trying to make the camera move around like an fps cam, (the code omar gave me above didn't work);
tell me, what is wrong with this code;
ms->pt.x
and ms->pt.y
are the x, y positions of a mouse cursor on screen... regardless i think if it's a fps camera, then i shouldn't need to use the edges of the screen to move the camera right? any mouse movement should move the camera, am i correct?
tell me, what is wrong with this code;
ms->pt.x
and ms->pt.y
are the x, y positions of a mouse cursor on screen... regardless i think if it's a fps camera, then i shouldn't need to use the edges of the screen to move the camera right? any mouse movement should move the camera, am i correct?
RvX=ms->pt.x;
RvY=ms->pt.y;
core::vector3df rot;
rot=camera->getRotation();
core::vector3df pos;
pos=camera->getPosition();
core::vector3df target;
target=camera->getTarget();
if (ms->pt.x>=windowWidth-50)
{
target.X=(int)(target.X+500);
}
if (ms->pt.x<=50)
{
target.X=target.X-500;
//if (rot.Z<0) rot.Z+=360;
}
if (ms->pt.x>=windowWidth-50)
{
rot.rotateXZBy(30,camera->getPosition());//(int)(rot.Y+30)%360;
}
if (ms->pt.x<=50)
{
rot.rotateXZBy(-30,camera->getPosition());
//rot.Y=rot.Y-30;
//if (rot.Y<0) rot.Y+=360;
}
camera->setRotation(rot);
camera->setTarget(target);
}
-
omar shaaban
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
well the problem with your code that u are moving the target only and u forgot to move the postion of the camera so at the end of this code write this
NOTE:this should be written after this line:
Code: Select all
target.Y-=20;
target.Z-=10;
camera->setPosition(target);
Code: Select all
camera->setTarget(target);
No, don't........
what the... Omar, stop trying to help if you're not sure, you'll only confuse ppl more.
If he does that his camera will ALWAYS look in the same direction.
anyway, look for bitplane's movement code in this section.
It teaches how to rotate a vector based on orientation.
Based on that, do your rotation bit then get your rotation matrix and place your target (the same way he makes the movement).
(get a simple vector : vector3df(0,0,1), multiply by rotation matrix, add to camera position.)
Somewhere else use also his movement script to move the camera
what the... Omar, stop trying to help if you're not sure, you'll only confuse ppl more.
If he does that his camera will ALWAYS look in the same direction.
anyway, look for bitplane's movement code in this section.
It teaches how to rotate a vector based on orientation.
Based on that, do your rotation bit then get your rotation matrix and place your target (the same way he makes the movement).
(get a simple vector : vector3df(0,0,1), multiply by rotation matrix, add to camera position.)
Somewhere else use also his movement script to move the camera
-
omar shaaban
- Posts: 616
- Joined: Wed Nov 01, 2006 6:26 pm
- Location: Cairo,Egypt
- Contact:
um, actually, i'm quite confused by what you just saidDorth wrote:No, don't........
what the... Omar, stop trying to help if you're not sure, you'll only confuse ppl more.
If he does that his camera will ALWAYS look in the same direction.
anyway, look for this section.
It teaches how to rotate a vector based on orientation.
Based on that, do your rotation bit then get your rotation matrix and place your target (the same way he makes the movement).
(get a simple vector : vector3df(0,0,1), multiply by rotation matrix, add to camera position.)
Somewhere else use also his movement script to move the camera
so basially i'm going to create a vector depending on whcih axis i want to rotate on (x, y or z) and then multiply that by a rotation matrix, and then the product (which should be in vector format) is going to be added to the camera position?
so it would be something like :
if that's correct then the question is, how do i multiply it by a rotation matrix? and i've done several searches on the forums for bitplane's movement code and couldn't find it, could you possibly give me the link?Current=(0,0,1);
RotVector=current*rotation matrix;
Final =camera->getPosition()+RotVector;
camera->setPosition(Final);
thanks again in advance.
edit:
actually i've been looking around and i found this in teh FPS camera, and i was wondering if this was the part i needed and if so can someone please explain the parts in bold?
if the above IS what i was looking for then my question is, what if i want to use one of SEVERAL mice? i know how to split them, but would the same exact code work under an event handler for one of the mice alone, or would it still effect all the rest?void CCameraFPSSceneNode::animate( u32 timeMs )
{
const u32 camIsMe = SceneManager->getActiveCamera() == this;
if (firstUpdate)
{
if (CursorControl && camIsMe)
{
CursorControl->setPosition(0.5f, 0.5f);
CenterCursor = CursorControl->getRelativePosition();
}
LastAnimationTime = os::Timer::getTime();
firstUpdate = false;
}
// get time. only operate on valid camera
f32 timeDiff = 0.f;
if ( camIsMe )
{
timeDiff = (f32) ( timeMs - LastAnimationTime );
LastAnimationTime = timeMs;
}
// update position
core::vector3df pos = getPosition();
// Update rotation
// if (InputReceiverEnabled)
{
Target.set(0,0,1);
if (CursorControl && InputReceiverEnabled && camIsMe )
{
core::position2d<f32> cursorpos = CursorControl->getRelativePosition();
if (!core::equals(cursorpos.X, CenterCursor.X) ||
!core::equals(cursorpos.Y, CenterCursor.Y))
{
RelativeRotation.X *= -1.0f;
RelativeRotation.Y *= -1.0f;
RelativeRotation.Y += (0.5f - cursorpos.X) * RotateSpeed;
RelativeRotation.X = core::clamp ( RelativeRotation.X + (0.5f - cursorpos.Y) * RotateSpeed,
-MAX_VERTICAL_ANGLE,
+MAX_VERTICAL_ANGLE
);
RelativeRotation.X *= -1.0f;
RelativeRotation.Y *= -1.0f;
CursorControl->setPosition(0.5f, 0.5f);
CenterCursor = CursorControl->getRelativePosition();
}
}
// set target
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df( RelativeRotation.X, RelativeRotation.Y, 0));
mat.transformVect(Target);
core::vector3df movedir = Target;
if (NoVerticalMovement)
movedir.Y = 0.f;
movedir.normalize();
if (InputReceiverEnabled && camIsMe)
{
if (CursorKeys[0])
pos += movedir * timeDiff * MoveSpeed;
if (CursorKeys[1])
pos -= movedir * timeDiff * MoveSpeed;
// strafing
core::vector3df strafevect = Target;
strafevect = strafevect.crossProduct(UpVector);
if (NoVerticalMovement)
strafevect.Y = 0.0f;
strafevect.normalize();
if (CursorKeys[2])
pos += strafevect * timeDiff * MoveSpeed;
if (CursorKeys[3])
pos -= strafevect * timeDiff * MoveSpeed;
// jumping ( need's a gravity , else it's a fly to the World-UpVector )
if (CursorKeys[4])
{
pos += UpVector * timeDiff * JumpSpeed;
}
}
// write translation
setPosition(pos);
}
// write right target
TargetVector = Target;
Target += pos;
}
i tried modding that code, and placing it under my mouse movement event handler as follows :
Global Declarations to make my life easier
i've also tried to use this as the strafing code, and it does strafe after an initial jump to look at the wrong direction:
can someone PPPPPLLLLEASSSSSSSSEEEEE help me?
edit:
i accidently discovered a way to make the camera rotate left and right (kinda jerky but it works) while trying to get strafing to work (now i need to find out how strafing DOES work properly)
however i cannot get this code to rotate the camera up and down
Global Declarations to make my life easier
Under the mouse movement function:const f32 MAX_VERTICAL_ANGLE = 88.0f;
const f32 RotateSpeed=100;
const f32 MoveSpeed=100;
it basically did nothing, although there were no compilation errors, but it would run the demo, and if i moved the mouse, it would crash.const u32 camIsMe = smgr->getActiveCamera() == camera;
bool firstUpdate=true;
gui::ICursorControl* CursorControl;
bool CursorControlflag=true;
core::position2df CenterCursor;
f32 LastAnimationTime;
f32 timeDiff;
core::vector3df Target;
core::vector3df RelativeRotation=camera->getRotation();
bool NoVerticalMovement=false;
core::vector3df TargetVector;
if (firstUpdate)
{
if (CursorControlflag && camIsMe)
{
CursorControl->setPosition(0.5f, 0.5f);
CenterCursor = CursorControl->getRelativePosition();
}
//LastAnimationTime = ITimer::Timer::getTime();
firstUpdate = false;
}
// get time. only operate on valid camera
timeDiff = 0.f;
//if ( camIsMe )
//{
//timeDiff = (f32) ( timeMs - LastAnimationTime );
//LastAnimationTime = timeMs;
//}
// update position
core::vector3df pos = camera->getPosition();
// Update rotation
// if (InputReceiverEnabled)
{
Target.set(0,0,1);
if (CursorControl && camIsMe )
{
core::position2d<f32> cursorpos = CursorControl->getRelativePosition();
if (!core::equals(cursorpos.X, CenterCursor.X) ||
!core::equals(cursorpos.Y, CenterCursor.Y))
{
RelativeRotation.X *= -1.0f;
RelativeRotation.Y *= -1.0f;
RelativeRotation.Y += (0.5f - cursorpos.X) * RotateSpeed;
RelativeRotation.X = core::clamp ( RelativeRotation.X + (0.5f - cursorpos.Y) * RotateSpeed,
-MAX_VERTICAL_ANGLE,
+MAX_VERTICAL_ANGLE
);
RelativeRotation.X *= -1.0f;
RelativeRotation.Y *= -1.0f;
CursorControl->setPosition(0.5f, 0.5f);
CenterCursor = CursorControl->getRelativePosition();
}
}
// set target
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df( RelativeRotation.X, RelativeRotation.Y, 0));
mat.transformVect(Target);
// write right target
TargetVector = Target;
Target += pos;
}
i've also tried to use this as the strafing code, and it does strafe after an initial jump to look at the wrong direction:
what am i doing wrong? i'm going insane here, i've tried everything i can think of and nothing works, and it's depressing, especially with a dead line VERY VERY soon.
else if (evtArg == 1)
{
//Rady note strafing trial:
core::vector3df Target=camera->getTarget();
core::vector3df RelativeRotation=camera->getRotation();
core::vector3df pos=camera->getPosition();
core::vector3df UpVector=camera->getUpVector();
core::vector3df TargetVector;
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df( RelativeRotation.X, RelativeRotation.Y, RelativeRotation.Z));
mat.transformVect(Target);
core::vector3df movedir = Target;
movedir.normalize();
core::vector3df strafevect = Target;
strafevect = strafevect.crossProduct(UpVector);
strafevect.normalize();
pos += strafevect * MoveSpeed;
camera->setPosition(pos);
TargetVector = Target;
Target += pos;
camera->setTarget(Target);
//End of Strafing Trial
}
can someone PPPPPLLLLEASSSSSSSSEEEEE help me?
edit:
i accidently discovered a way to make the camera rotate left and right (kinda jerky but it works) while trying to get strafing to work (now i need to find out how strafing DOES work properly)
however i cannot get this code to rotate the camera up and down
any ideas?//This is put under the mouse movement function.
/////////////// Camera movement trial///////////////////////
core::vector3df Target=camera->getTarget();
core::vector3df RelativeRotation=camera->getRotation();
core::vector3df pos=camera->getPosition();
core::vector3df UpVector=camera->getUpVector();
core::vector3df TargetVector;
//Rady note: ACCIDENTAL SUCCESS! Camera Move left
if (ms->pt.x<=50)
{
pos.normalize();
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df( 0, -1,0));
mat.transformVect(Target);
core::vector3df movedir = Target;
movedir.normalize();
core::vector3df strafevect = Target;
strafevect = strafevect.crossProduct(UpVector);
strafevect.normalize();
pos += strafevect * MoveSpeed;
camera->setPosition(pos);
TargetVector = Target;
Target += pos;
camera->setTarget(Target);
//End of camera movement Trial
}
//Rady Note :Camera move Right
if (ms->pt.x>=windowWidth-50)
{
pos.normalize();
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df(0, 1, 0));
mat.transformVect(Target);
core::vector3df movedir = Target;
movedir.normalize();
core::vector3df strafevect = Target;
strafevect = strafevect.crossProduct(UpVector);
strafevect.normalize();
pos -= strafevect * MoveSpeed;
camera->setPosition(pos);
TargetVector = Target;
Target += pos;
camera->setTarget(Target);
//End of camera movement Trial
}
//End Camera move right
//Rady Note :Camera move Up
if (ms->pt.y>=windowHeight-50)
{
pos.normalize();
core::matrix4 mat;
mat.setRotationDegrees(core::vector3df( -1, 0, 0));
mat.transformVect(Target);
core::vector3df movedir = Target;
movedir.normalize();
core::vector3df strafevect = Target;
strafevect = strafevect.crossProduct(UpVector);
strafevect.normalize();
pos -= strafevect * MoveSpeed;
camera->setPosition(pos);
TargetVector = Target;
Target += pos;
camera->setTarget(Target);
}
//End Camera move Up
////////////////End of camera movement Trial//////////////////
-
christianclavet
- Posts: 1638
- Joined: Mon Apr 30, 2007 3:24 am
- Location: Montreal, CANADA
- Contact:
