3rd Person Camera (Oh not another one!!!!)[SOLVED]

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

3rd Person Camera (Oh not another one!!!!)[SOLVED]

Post by g0bl1n »

Ok, well, I think of myself as a pretty experienced programmer. So I figured I would make a 3rd person camera easy...not... So I looked on the forums, searched many a topic, downloaded many broken links, and tried many examples with no success. So I am trying my way!

My first problem is is that I cannot use some of the simple commands like I thought you could(like so):

Code: Select all

ICameraSceneNode* camera=smgr->addCameraSceneNodeFPS();
camera->setRelativePosition(tris_node);
!!!!!tris_node is the player model!!!!!

So being fed up with that, I began to think of a way to move the player, regardless of not being able to actually implement it. So my question is, can someone help me translate what I have into Irrlicht code?

So, I'll just dive into it. I figured I'd have the player model face the cursor, or the middle of the screen. Then using cos and sin make him move based on the direction. My basic "code"(this is without middle stuff, and without mostly everything else, just the necessary code):

Code: Select all

float char_speed=4;
float char_direc=0;

if(keys[KEY_KEY_W])
{
tris_node->setPosition(tris_node->getPosition()+vector3df(cos(((char_direc)*pi/180)*char_speed,0,-sin(((char_direc)*pi/180)*char_speed));
}

if(keys[KEY_KEY_A])
{
tris_node->setPosition(tris_node->getPosition()+vector3df(cos(((char_direc+90)*pi/180)*char_speed,0,-sin(((char_direc+90)*pi/180)*char_speed));
}

if(keys[KEY_KEY_S])
{
tris_node->setPosition(tris_node->getPosition()+vector3df(cos(((char_direc+180)*pi/180)*char_speed,0,-sin(((char_direc+180)*pi/180)*char_speed));
}

if(keys[KEY_KEY_D])
{
tris_node->setPosition(tris_node->getPosition()+vector3df(cos(((char_direc+270)*pi/180)*char_speed,0,-sin(((char_direc+270)*pi/180)*char_speed));
}
Although the problem is is that I have no idea how to get the coordinated of the middle of the screen...

So to summarize:
*How do I set the camera to a certain point of another node(just the basic code, I'll come up with my own camera functions).

*How do I define the direction of a camera, to face the middle of the screen(I was thinking since the FPS camera already faces the middle of the screen, maybe I could just have the player face the direction the camera is facing, but I don't know which is better to use).


If you can't understand my gibber gabber, feel free to call me a noob and flame me for a while. But not too long.....cause it just gets old....
Last edited by g0bl1n on Fri Feb 22, 2008 9:17 am, edited 1 time in total.
morris
Posts: 36
Joined: Tue Jul 10, 2007 10:10 am

Post by morris »

usually, you woudn't use the built-in FPS camera. instead, create your own eventreceiver which controls your player node, and updates a normal ICameraSceneNode every frame. you should also look at the code of the FPS camera in the irrlicht/source folder to get an idea of how the eventreceiver should update the camera.
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Re: 3rd Person Camera (Oh not another one!!!!)

Post by rogerborg »

g0bl1n wrote:So I figured I would make a 3rd person camera

Code: Select all

ICameraSceneNode* camera=smgr->addCameraSceneNodeFPS();
Use smgr->addCameraSceneNode(); to create a non First Person Shooter camera.

[off topic]I actually wish the 'trick' cameras were just removed from Irrlicht, and the functionality moved to sample apps, because they send so many people off down the wrong track. They are usable only for very simple demos. :([/off topic]

g0bl1n wrote:

Code: Select all

camera->setRelativePosition(tris_node);
setRelativePosition() is not an ISceneNode / ICameraSceneNode method. In Irrlicht, all positions are set relative to a node's parent, so the appropriate method is just setPosition(), and the parameter is a const core::vector3df &, not a node, so it would be:

Code: Select all

camera->setPosition(tris_node->getAbsolutePosition());
Only it's not in this case, because I'm pretty certain that what you're trying to do is to have the camera follow another node and look at it. You could set its position relative to the 'player node' each frame, or you could do:

Code: Select all

camera->setParent(tris_node);
camera->setPosition(core::vector3df(0.f, 100.f, -100.f));
Which would make the camera a child of the player node (and then its position becomes relative to that node) and then offset its relative position to above and behind the player node.

Looking at the player node means tracking its position every frame (there's currently no method for having the camera track another node automatically, but I have a candidate up for consideration), so you'd have to do this every frame:

Code: Select all

camera->setTarget(tris_node->getAbsolutePosition());

Code: Select all

*How do I define the direction of a camera, to face the middle of the screen(I was thinking since the FPS camera already faces the middle of the screen, maybe I could just have the player face the direction the camera is facing, but I don't know which is better to use).
:? How could any camera not "face the middle of the screen" (or the middle of the viewport?)

Perhaps you mean "face a specific point in 3d space"? I don't know, I'm just guessing here. Could you clarify what you mean?
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

Post by g0bl1n »

In 3rd person games, such as Conker(strange its the only one that comes to mind...) the player faces the crosshairs in the middle of the screen. So I guess I would have to translate the 2d coordinates of the screen into a 3d space. Which if the screen res was 640x480, would be 320x240. As soon as I can I'm going to go back and look at the collisions example, maybe I can get an idea from that.

Before I go and experiment though I had another question. When using setPosition for the player model's node, if he goes off stairs he kind of floats in the air until you either hit the floor, or let go of the button moving him. Is there anyway to have him just go the direction you want, and still have him go down towards the floor like normal?

If the statement above made no sense to you, as it to me the person who wrote it, I'll try again. Whenever you move the player from a higher spot to a lower one, he floats down to it slowly. Now if u tap the button and let go of it, he goes down normally, its only when holding the button does he float. So I guess when your moving the gravity doesn't apply to him, which is what I don't want... /cry
g0bl1n
Posts: 63
Joined: Thu Feb 21, 2008 8:45 am

Post by g0bl1n »

I found this code in a post in the forums:

Code: Select all

        mousePos = device->getCursorControl()->getPosition();

        if(mousePos.X > lastMousePos.X)
        {char_direc+=1;}
        if(mousePos.X < lastMousePos.X)
        {char_direc-=1;}
        if(mousePos.Y > lastMousePos.Y)
        {}
        if(mousePos.Y < lastMousePos.Y)
        {}

        lastMousePos = mousePos;
So I believe this is exactly what I'm looking for. The only problem is is that I have no idea how to define the two variables mousePos and lastMousePos. I tried floats but that can't be done. So they would have to be defined by a Irrlicht standard, which I have no idea which one to use...
Post Reply