Shooting Accuracy

Started by
7 comments, last by Medo Mex 11 years, 3 months ago

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?

Advertisement

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.

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.
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);

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

Anyway:

[source]

// 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);

[/source]

It works!

Thanks :)

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

@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.

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.

This topic is closed to new replies.

Advertisement