DX10 engine plan-bullets and dx10 engine bullets

Source: Internet
Author: User

DX10 engine plan-bullets and dx10 engine bullets

Today, I changed the bullet release mechanism to "collision N times" or "death (suicide)" after a period of time ).

The difficulty is to commit suicide. It is difficult to ensure that the system will not "return to Soul" after the suicide ".

After searching for a long time, I finally found out which statement is called "soul ":

This sentence is the fault. If you release yourself in the ActionUpdate of an object, the next sentence pNew = pNew-> pNext has a problem and points to a space that has been released. So I made the following changes:

It's a classic and well-developed method, but it's not a try. Successfully committed suicide. (It's hard to mix and commit suicide)

2015/03/27

***********************************

I finally wrote it out, a little later than I expected, and I have been fixing bugs ,:

Image Description:

Click the left mouse button to launch a bullet along the camera's line of sight. The speed and types of bullets must be set in advance. I will paste the code below.

In order to prevent the number of bullets from being too large and the interval between bullets is too short, the adhesion (I haven't completely solved the problem yet) is generated, the cool-down time of a bullet and the regular recovery mechanism of the bullet are set.

I recycled the bullets in a unified manner, not every bullet carries a timer (it can be carried, it is another bullet ).

**************************************** *****************************

Technical issues:

When you look at it, you will understand that a bullet is a heap. First, first, and foremost (only for the bullets I have chosen. The bullets with a timer can be linked lists and deleted randomly ). The memory occupied by bullets cannot be too large, so a meshCopy class is required. I have written two copy classes, MeshCopy and MeshCopyNew. The difference is that MeshCopyNew creates a completely independent space, it is not affected by the parent. MeshCopy shares a part of memory space (vertex and texture) with the parent ). The parent is released, and the copy object must be released. If this process is written as an arbitrary deletion mode, it is troublesome and it is not worth the candle. The entire MeshCopy heap is a whole, that is, the Bullet class seen now. Bullet is responsible for adding and deleting new bullets, at the same time, bullets have their own "consciousness" to execute different tasks. This seems to save a lot of trouble. In fact, the speed is much faster.

Note: When writing a heap, note that the memory should be released clean.

Below is a part of the Code, only paste so much, to prevent the spread of lazy cancer.

--------------------------------------------------

Code for regularly launching bullets:

 1 static float t_base_bullet = 0.0f, t_cur_bullet = 0.0f; 2  3     if(GetMouse().GetState() == LKEY_PRESSED) 4     { 5         t_cur_bullet = mTimer->getGameTime(); 6         if( (t_cur_bullet - t_base_bullet) >= BULLET_TIME_GAP) 7         { 8             gBullet->CreateOneBullet(curScene->GetObjList(), *gCrashList, GetCamera().position(), GetCamera().GetLook()); 9             t_base_bullet = t_cur_bullet;10         }11     }


The code for recycling bullets at regular intervals (a little different from the code for timed launch, but it's just a little clever. It's hard to understand ):

1 void Bullet: ActionUpdate (float dt) 2 {3 // regularly reclaim Bullet resources 4 static float t_base_bullet = 0.0f, t_cur_bullet = 0.0f; 5 6 if (! ExistBullet) 7 {8 // when no bullet exists, 9 t_base_bullet = t_cur_bullet = mTimer-> getGameTime (); 10 return; 11} 12 13 if (pHead-> pNext) 14 {15 t_cur_bullet = mTimer-> getGameTime (); 16 if (t_cur_bullet-t_base_bullet)> = BULLET_LIFE_TIME) 17 {18 DeleteOneBullet (); 19 20 // timing 21 t_base_bullet = t_cur_bullet; 22} 23} 24}

 

I used a little bit of PV operation knowledge to add and delete bullets.
Code for creating a bullet:

1 void Bullet: CreateOneBullet (DemoList & paintList, CrashList & crashList, D3DXVECTOR3 & position, D3DXVECTOR3 & speed) // create a bullet 2 {3 // V operation 4 // if there is no bullet, it is set to exist. If so, do not modify the value 5 existBullet | = true; 6 7 MeshCopy * pNew = new MeshCopy (); 8 pNew-> Copy (* bulletTypeArray [curBulletTypeIndex]); 9 pNew-> SetPosition (position ); 10 pNew-> SetSpeed (speed * mSpeed); 11 pNew-> OpenCrash (); 12 13 pNew-> SetMass (1.0f); 14 15 paintList. addObj (pNew); 16 crashList. addObj (pNew); 17 18 pTail-> pContent = pNew; 19 20 pBulletPoint pP = (pBulletPoint) malloc (sizeof (BulletPoint); 21 pP-> pContent = NULL; 22 pP-> pNext = NULL; 23 24 pTail-> pNext = pP; 25 pTail = pP; 26 27}


Delete bullet code

1 void Bullet: DeleteOneBullet () // heap, first-in-first-out Delete 2 {3 if (pHead = pTail) 4 {5 // The heap is empty 6 // P operation 7 existBullet = false; 8 return; 9} 10 11 static pBulletPoint pT; 12 13 pT = pHead-> pNext; 14 15 delete (pHead-> pContent); 16 17 free (pHead); 18 pHead = pT; 19}

 

-----------------------------------------

Well, this article ends here, hoping to be useful to readers.

In the next stage, add music and say "it's a man!". Let's make a Demo of the bullet from the CS design perspective.

(The source of the BUG of adhesion in the Demo is being searched. If you find anything, please kindly advise. Thank you! Orz)

-------------------------------

This chapter demonstrates the Demo download link (Baidu online storage is used for a small number of times. If the link is broken, I hope readers can notify me in time ):

Http://pan.baidu.com/s/18R6iU

Related Article

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.