Jump to content

  • Log In with Google      Sign In   
  • Create Account


Shooting Accuracy


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
8 replies to this topic

#1 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 08 January 2013 - 01:28 AM

Hi guys,

 

In FPS game, each weapon should have different accuracy, for example: A sniper usually have higher accuracy than a machine gun or pistol.

 

I'm creating a ray for shooting from the cross hair and looking for a way to set the ray accuracy in percentage (accuracy of 100 means it will shoot exactly the same point infront of the cross hair).

 

D3DXVECTOR3 rayOrigin;
D3DXVECTOR3 rayDirection;

// Shooting
Raycasting(rayOrigin, rayDirection);

How do I update rayDirection to set the weapon accuracy?



Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7116

Like
4Likes
Like

Posted 08 January 2013 - 01:59 AM

Instead of a single direction vector you should use the orientation matrix (e.g. camera matrix), including lookat,right and up vector. This way your direction could look like

rayDirection = normalize(lookat + right * random() * accuracy + up * random() * accuracy)

your best accuracy would be 0, higher values would decrease the accuray.



#3 Tom KQT   Members   -  Reputation: 1557

Like
1Likes
Like

Posted 08 January 2013 - 02:18 AM

rayOrigin is the position of the gun (position vector) and rayDirection is the vector towards the ideal centre of the crosshair (direction vector), right?

Now with accuracy:
rayOrigin will be the same.
And about rayDirection - each weapon could have its precision defined as the maximum angle the shot could go off the ideal centre (for example 5 degrees).
You would generate a random number from 0 to this maximum angle and rotate the rayDirection around the rayOrigin point by this random degree lets say to the right. So, for example, if the random number was 2.1 degrees, the shot at this stage would go exactly 2.1 degrees to the right of the ideal direction.
And as the next step, you would generate a random angle from 0 to 360 degrees (so a full "circle") and rotate this new direction vector around the ideal direction vector by this amount.
As a result, the shots can go randomly anywhere around the ideal target and only by a limited (but random) distance from the target.

Edited by Tom KQT, 08 January 2013 - 02:21 AM.


#4 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 08 January 2013 - 02:39 AM

rotate the rayDirection around the rayOrigin point by this random degree lets say to the right

 

So, I need to create a matrix and use D3DXVec3TransformCoord(), right?

 

How should I do the rotation according to the random degree? I usually use D3DXMatrixRotationYawPitchRoll() for rotation but not sure how you are explaining the rotation here.


D3DXMATRIX rotation;
// Rotate 2.1 degrees (random number)
// Rotate 0-360 angle
D3DXVec3TransformCoord(&rayDirection, &rayOrigin, &rotation);


#5 Tom KQT   Members   -  Reputation: 1557

Like
1Likes
Like

Posted 08 January 2013 - 04:18 AM

It should work this way. Maybe it could be done more effectively, dunno.

Anyway:

// lets presume D3DXVECTOR3 rayDirection is already defined and contains your ideal shooting direction vector
 
// first we need to find any vector perpendicular to rayDirection. There is infinite number of such vectors, we need any one of them. This could be done for example this way:
D3DXVec3 perpVec(rayDirection.y, -rayDirection.x, 0);
// when using this very simple way you would however have to make sure that you don't get 0 vector by this, which would happen if both rayDirection.x and rayDirection.y were zero. In this case you would have to use slightly different core, for example:
//D3DXVec3 perpVec(0, rayDirection.z, -rayDirection.y);
// The whole point of finding a perpendicular vector this way is to swap two components, change the sign of ONE of them and make the 3rd component zero.
 
// Normalize it for safety:
D3DXVec3Normalize(&perpVec, &perpVec);
 
// Now the shooting accuraty:
float shootingDeviationAngle = GetRandomNumberInInterval(0, maxDeviationAngleForCurrentWeapon); // this is partly pseudo-code, but I think it's clear what it does. Values are in radians!
 
// Rotate vector rayDirection around vector perpVec by the shootingDeviationAngle angle:
Matrix m;
Vec3 v;
D3DXMatrixRotationAxis(&m, &perpVec, shootingDeviationAngle);
D3DXVec3TransformNormal(&v, &rayDirection, &m);
 
// Get a random angle from 0 to 360 degrees:
float angle = GetRandomNumberInInterval(0, D3DX_PI * 2.0f);
 
// And rotate vector v around vector rayDirection by this angle:
D3DXMatrixRotationAxis(&m, &rayDirection, angle);
Vec3 finalRayDirection;
D3DXVec3TransformNormal(&finalRayDirection, &v, &m);


Edited by Tom KQT, 08 January 2013 - 04:22 AM.


#6 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 08 January 2013 - 05:02 AM

It works!

 

Thanks :)



#7 ankhd   Members   -  Reputation: 1213

Like
1Likes
Like

Posted 09 January 2013 - 07:49 PM

You should base it on how far the target is. Because with a real gun anyone can shoot some thing when it's close. Eg the game Americas army must do something like your doing and I cant shoot anything in that game and that sux

#8 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 09 January 2013 - 11:45 PM

@ankhd: That's true, the player should be able to have better accuracy especially if the target is very close.

 

I'm not sure exactly what is the most efficient way to calculate the accuracy based on the weapon diversion angle and the distance, however I have done the calculation according to the weapon diversion angle and then increased the accuracy if the target is close enough.



#9 Medo3337   Members   -  Reputation: 665

Like
0Likes
Like

Posted 09 January 2013 - 11:53 PM

One thing that I think I should take care of is the bullet speed, in real life the bullet will arrive based on the speed and the distance.

So, I'm trying to find a way to delay the bullet arrival time for certain amount of time in milliseconds (depends on how far is the target).

 

The following should do the trick but definitely it will not only delay the bullet arrival but it will delay rendering as well:

while(...)
{
    // Delay until the bullet arrive (based on the bullet speed and the distance)
}

 

Maybe I can create a thread for shooting and do the above code in that thread then use a callback method to get the shooting result?

 

I need suggestion on what is the best way to do it without affecting the rendering, BTW I'm using a single method that I call for shooting and it should get me the shooting results every time I call it.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS