Use Irrlicht to build a game lead

Source: Internet
Author: User

The so-called game protagonist is the third view of the screen near the middle of the run to run, and then the surrounding scenery with the transformation of the node.

The idea of implementation is simple, divided into the following three steps
1. Get keyboard input
2. Move the protagonist and change the animation when needed
3. Moving the camera
The premise here is that the surrounding environment is unchanged relative to the world coordinate system.

So how do you use Irrlicht? Step-by-step.

1. Get keyboard input
Nature is to overload our Ieventreceiver class, this part of the general idea can refer to the implementation of the tutorial 04.Movement: Maintenance of a key table, each of the keys have two states (pressed and not pressed), when the keyboard has a signal to modify the table corresponding key value of the state For the main renderer to read.

Class Myeventreceiver: Publicieventreceiver{ Public:Myeventreceiver()    { for(U32 i=0; i<key_key_codes_count; ++i) Keyisdown[i] =false; Runcontrlcounter =0; Presskeycounter =0; }Virtual BOOLOnEvent (Constsevent&Event)    {//Change the state of the keys        if(Event. EventType = = irr::eet_key_input_event) {keyisdown[Event. Keyinput.key] =Event.        Keyinput.presseddown; }return false; }Virtual BOOLIskeydown (Ekey_code keycode)Const{returnKeyisdown[keycode]; }Private:BOOLKeyisdown[key_key_codes_count];};

And then the second step
2. Move the character and change the animation
These actions are relatively simple, but the pit on the node class Ianimatedmeshscenenode Incredibly is an abstract class , and I did not find a suitable way to inherit it into the entity class (if please inform). That can only be wrapped in a class of operations in a proxy class myroledelegate . This class has a render () function to read the current keyboard state from the event receiver and manipulate the master node.

There are eight of the characters of WASD operation, at this time corresponding to eight models of rotation transformation (two-dimensional), is also relatively simple. Attached code

Class myroledelegate{ Public: Myroledelegate (Ianimatedmeshscenenode*Node_p, Myeventreceiver*Receiver_p, Irrlichtdevice*device_p): Role (node_p), receiver (receiver_p), device (device_p) {init (); }voidInit () {Movement_speed= .F Available= true; CurrentPosition=Role -GetPosition (); Role -Setmd2animation (Emat_stand);//Initial stand-up animation}voidSetanimationtype (Emd2_animation_type atype) {//Avoid stuttering by repeatedly setting the same action        if(Currentanimationtype!=Atype) {Role -Setmd2animation (Atype); Currentanimationtype=Atype; }    }voidRender () {if(false) {Setanimationtype (Emat_stand); }Else{Setanimationtype (Emat_run); F32 Framedeltatime= 0.1; CurrentPosition=Role -GetPosition (); Float move_distance=Movement_speed*Framedeltatime; Vector3df dir (0.0,- -,0);//direction of the character            if(Receiver -Iskeydown (Key_key_w)) {dir.Y= - -; CurrentPosition.Z+=Move_distance*0.7;if(Receiver -Iskeydown (key_key_a)) {//wa DirectionDir.Y= -135; CurrentPosition.X-=Move_distance*0.7; }Else if(Receiver -Iskeydown (Key_key_d)) {//wd DirectionDir.Y= - $; CurrentPosition.X+=Move_distance*0.7; }Else{//w direction, similar to the followingCurrentPosition.Z+=Move_distance*0.3; }            }Else if(Receiver -Iskeydown (key_key_s)) {dir.Y=  -; CurrentPosition.Z-=Move_distance*0.7;if(Receiver -Iskeydown (key_key_a)) {dir.Y= 135; CurrentPosition.X-=Move_distance*0.7; }Else if(Receiver -Iskeydown (Key_key_d)) {dir.Y=  $; CurrentPosition.X+=Move_distance*0.7; }Else{currentposition.Z-=Move_distance*0.3; }            }Else if(Receiver -Iskeydown (key_key_a)) {dir.Y=  the; CurrentPosition.X-=Move_distance; }Else if(Receiver -Iskeydown (Key_key_d)) {dir.Y=  the; CurrentPosition.X+=Move_distance; }Else{dir=Currentrotation;            Setanimationtype (Emat_stand); }if(dir!=currentrotation) {Role -Setrotation (dir); Currentrotation=Dir } role -SetPosition (currentposition); }} VECTOR3DF Getcameraposition () {//Set where the camera should be at this timeVECTOR3DF POS=currentposition; Pos.Y+=  -; Pos.Z-=  -;returnPos }Private: Irrlichtdevice*Device//Master device pointerMyeventreceiver*Receiver//Main class event receiverIanimatedmeshscenenode*Role//proxy node pointerBOOL available;    Emd2_animation_type Currentanimationtype;    VECTOR3DF currentposition, currentrotation; F32 movement_speed;};

In addition, this proxy class involves changing the animation API, only for the MD2 format model

The third step is the simplest, just change the camera position in the main render function.

while(device->run()){    rDele->render()//rDele 为主节点代理    camera->setPosition(rDele->getCameraPosition());    //其他渲染操作}

Of course, you also need a main Game class to initialize all the variables, load the model, create a triangular fragment selector, add collision detection and gravity, and so on, not here to explain.

The final effect is the one that starts with the animation.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Use Irrlicht to build a game lead

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.