views:

51

answers:

1

My game is a small shooting game in cocos2d. The enemy generates the bullets to shoot player at intervals of time. I have created a random y , so that bullets touch the opposite edge at random heights. If the bullet touches the player the enemy wins.
But, I need to set probability for the enemy accuracy. If the probability of enemy is given as 80% accuracy? How can I set in my program? For 10 shots 8 should be straight towards player.
How can I decide which ones should go straight in 10 which one should miss. In mean time player also shoots the enemy.

Thank You.

+2  A: 

I would do it like this.

Assume your player has position y. To get 80% accuracy choose a random number from the interval [y - height*0.2, y + height*0.2], where height is the height of the screen. In general to get accuracy p choose a number from [y - height*(1-p), y + height*(1-p)]. When p = 1.0 (100%) then the bullet will be aimed at exactly the position of the player.

This doesn't mean that an enemy with 80% accuracy will fire 8 out of 10 shots exactly at the player, but the more accurate the closer the bullets are.

This of course neglects the time bullet travels, but that can be included without that much work.

Il-Bhima
Thank You. I am trying on it. I have another doubt. Hoe to represent nan in objective C . I searched for it. Btu I could not understand it. I have seen this nan(char) . But what char we should take here ?Thank You.
srikanth rongali
I did as you said . It is good. Thanks a lot.
srikanth rongali
I would ask a separate question on stackoverflow since there are more experienced osx devs than me. If I remember correctly nan(char*) is used to define a quiet nan, that is, an "infinite" number which will not cause aborts when used in arithmetic. The string parameter is any name for the number, typically "nan" or "not-a-number". I stand to be corrected.
Il-Bhima
This is also how I'd do it. However, unless I'm mistaken, to get the proper accuracy for the shots, shouldn't it be in the range [y - height * (1 - p) / 2, y + height * (1 - p) / 2] to maintain the desired percentage?
chaosTechnician