third person camera + keyboard

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
orgad
Posts: 5
Joined: Wed Aug 27, 2008 10:22 am

third person camera + keyboard

Post by orgad »

Hello there :)
i recently started using irrlicht(.NET) and i saw the fps camera function which is very nice. but i want my camera to follow a character.
easy, i made a camera that just looks at the character.
good, now i just needed to learn how to rotate the camera and the object and to take input from keyboard.

the rotation of the camera and object is something i still didnt discover.
and the event that is suppoused to make something when S or W is pressed doesnt seem to work.

how can i solve these problems?
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

The best thing to do would be to control your character with the keys (when you get a WASD key event (or whatever your controls are) you move your character in the necessary direction) and then just get the camera to follow where the character goes so make it a child of the character node and give it a nice offset so you see the character from the perspective you desire and each frame you just set the camera's target to be the character's position (or could be an offset from the character's position if that wasn't desirable).

Camera's pretty much completely ignore setRotation() as you should use setTarget() to make the camera look at a certain position.
Image Image Image
orgad
Posts: 5
Joined: Wed Aug 27, 2008 10:22 am

Post by orgad »

well i would make WASD move my character but i dont know how to make the event actually work, because it doesnt seem to do what i say when W or S is pressed.

and making sure the camera will always look at the object wont do. lets say i turn around the object but i dont change its xyz position. the object will turn but the camera wont and i aim that the camera will always look at the back of the object
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Ok so you update the camera's position to always be behind the character based on the character's rotation using characterNode->getAbsoluteTransformation().rotateVect(cameraOffset)

What is your event receiver code for dealing with the WASD key presses?
Image Image Image
orgad
Posts: 5
Joined: Wed Aug 27, 2008 10:22 am

Post by orgad »

well the one i saw was

Code: Select all

  static bool device_OnEvent(Event p_e)
        {
            if (node != null && p_e.Type == EventType.KeyInputEvent &&
                !p_e.KeyPressedDown)
            {
                switch (p_e.KeyCode)
                {
                    case KeyCode.Key_W:
                    case KeyCode.Key_S:
                        {
                            Vector3D v = node.Position;
                            v.Y += p_e.KeyCode == KeyCode.Key_W ? 2.0f : -2.0f;
                            node.Position = v;
                        }
                        return true;
                }
            }

            return false;
        }

the only thing i needed to change was the "what key was pressed" and what to do. but this event never seem to happen.
taken from the code in http://irrlichtnetcp.sourceforge.net/wi ... :_Movement
JP
Posts: 4526
Joined: Tue Sep 13, 2005 2:56 pm
Location: UK
Contact:

Post by JP »

Is that implemented within a class that inherits from IEventReceiver? It has to be. And the resulting class must then be registered with the device to the device knows where to send events to.
Image Image Image
rogerborg
Admin
Posts: 3590
Joined: Mon Oct 09, 2006 9:36 am
Location: Scotland - gonnae no slag aff mah Engleesh
Contact:

Post by rogerborg »

RTFPost: He's using Irrlicht.NetCP, which is where this question should be asked.

Again: Irrlicht.NetCP is an independent project.
Please upload candidate patches to the tracker.
Need help now? IRC to #irrlicht on irc.freenode.net
How To Ask Questions The Smart Way
maddog39
Posts: 10
Joined: Sat Aug 23, 2008 1:23 am
Location: Pennsylvania, US
Contact:

Post by maddog39 »

I created a topic sometime last week about modernizing an old third person camera code snippet which you might find helpful. Although its in C++ and not C# but you might be able to read it enough to make a port.

http://irrlicht.sourceforge.net/phpBB2/ ... hp?t=30035
Post Reply