Irrlicht to achieve shooting events

Source: Internet
Author: User

Shooting events, according to my unprofessional view can be divided into two kinds of realization, divided into fast shooting and slow shooting.

    • Quick shot: The bullet's flight time does not count, all distances arrive in a flash. At the moment of the shooting, the intersection of the ray and the object from the quasi-heart is judged, and the object is struck.
    • Slow shot: Bullet speed is limited. At this point the bullet, as an FPs object, needs to be tracked in real time until it collides with an object or reaches infinity. The targeted object can dodge bullets.

In the usual shooting game, the "Quick shot" type, that is, aiming at the object click on the shot will certainly be able to shoot. and slow shooting use less, FPS object overhead is actually unnecessary.

What this article is talking about is "quick shot". The effect is to shoot a monster and he will Flash (the blood is the sense of sight, highlighting a short period of time).

First, create a monster that can be shot.

voidGame0:: Setupmonster() {Scene:: Ianimatedmeshscenenode*Yodannode=Smgr -Addanimatedmeshscenenode (smgr -Getmesh (".. /.. /media/yodan.mdl "),0, idflag_isshootable//Signs that can be shot);if(Yodannode) {Scene:: Iscenenodeanimator*Anim=Smgr -Createflystraightanimator (Core:: Vector3df( -,- -, -), Core:: Vector3df(- -,- -, -),5500,true);if(ANIM) {Yodannode -Addanimator (ANIM); Anim -Drop (); } Yodannode -Setmaterialflag (Video:: emf_lighting,true);//Use lightingYodannode -Setframeloop (0, -); Yodannode -Setanimationspeed ( the); Yodannode -Setrotation (Core:: Vector3df(0, the,0)); Scene:: Itriangleselector*Selector=Smgr -Createtriangleselector (Yodannode); Yodannode -Settriangleselector (selector);//Add triangle selectorSelector -Drop (); }}

The next step is to update the centroid in real time in the main loop to see if the target is on this node.

 while(Device -Run ()) {if(Device -Iswindowactive ()) {Driver -BeginScene (true,true, video:: Scolor(0,0,0,0)); Smgr -Drawall (); This -Unlight ();//restore light, as described below        //Create a RayCore:: Line3d<F32>Ray Ray.Start=Camera -GetPosition (); Ray.End=Ray.Start+(Camera -Gettarget ()-Ray.Start.Normalize ()* 1000.0F//Intersection locationCore:: Vector3dfintersection;//Intersection triangleCore:: Triangle3dfHittriangle;//Get the node that belongs to Ray's intersection triangleScene:: Iscenenode *Selectedscenenode=Collman -Getscenenodeandcollisionpointfromray (Ray, intersection, Hittrian            GLE); The node currently being delivered this -Currentscenenode= NULL;if(Selectedscenenode) {if((Selectedscenenode -GetID ()&idflag_isshootable)==idflag_isshootable)//The node ID of the intersection satisfies the firing condition{This -Currentscenenode=Selectedscenenode; }} Driver -EndScene (); }  }

In this way, we can know at any time whether the target node can be shot (in this case, the only Yodan node). The next step is to press the shoot function called by a key.

voidGame0:: Shoot(){if(This -Currentscenenode)//Aiming at a node that can be shot at this time{This -Currentscenenode -Setmaterialflag (Video:: emf_lighting,false);//Turn off the light for this node to highlight the node (render in the original color of the model)Lightednode Lnode (This -Currentscenenode,device -Gettimer () -GetTime ()+ -);//The second parameter is the time that the secondary node cancels highlighting           //You can see that the length of the highlight is 0.1sThis -Lightednodes.Push_back (Lnode);//Add this node to a queue and cancel highlighting after some time} }

So the unlight function mentioned above will be the same. Its function is to determine which nodes in the current highlight queue highlight the time to cancel its highlighting

voidGame0::unLight(){    u32 now = device->getTimer()->getTime();    for (s32 i=0; i<(s32)this->lightedNodes.size(); ++i)        if (now > lightedNodes[i].when )        {            lightedNodes[i].node->setMaterialFlag(videotrue);            lightedNodes.erase(i);            i--;        }}

So that you can achieve the effect of flashing after shot

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

Irrlicht to achieve shooting events

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.