[13.04.2016]
At some point, you might want to attach a rifle to your top-down hero.
And, you might want to shoot it.
My game involves a crosshair that determines the player body's rotation around its origin.
The body's origin is the center of the character's head O.
In other words, the player's head always looks at the crosshair.
The body may have several fixtures which necessarily rotate therewith, for example a gun attached to the primary hand.
As such, the crosshair is used to properly direct every gunshot.
Now, everything's all and well if the rifle's center, the rifle's muzzle and your player's head center are all colinear.
I assume here that each launched bullet follows the ray from the rifle's center to the rifle's muzzle.
In other words, you're fine if you position your gun like this:
So if your head looks at the crosshair, so does the rifle and its muzzle.
But we're not always this lucky, especially if you're after gangsta-pistol layout:
It becomes obvious that if the gun is to look exactly at crosshair, your character's eyes certainly cannot so.
They need to be directed somewhere a little bit different,
in the picture above for example, you'd need to tilt the whole character's body a tiny bit counter-clockwise.
Let us express the problem in mathematical terms - given are points:
O (head), A (rifle center), B (muzzle), C (crosshair).
Let us define a circle whose center is at O and whose radius is OB.
These conditions follow:
1. A lies inside the circle.
2. C lies outside the circle.
Notice that B lies on the circle.
The question becomes:
By what angle should we rotate (around the center of rotation O) points A and B in order to make A, B and C colinear?
Well, I'm not particularly good at geometry but fortunately, Marcel, a friend of mine, came to the rescue!
It took him no more than 15 minutes of scribbling with his compass to come up with a solution:
1. Consider a circle Y whose center is at O and whose radius is OC.
2. Let G be the point of intersection of Y and a ray BA.
3. The angle we're looking for is twice the angle CGA.
Without really trying to validate his mysterious recommendation, I went on to code the damn thing.
And guess what, it works flawlessly!
This is how these hardcore calculations look like in debug drawing mode:
Legend:
OG
Input A, B and properly rotated A, B
line going through the points A, B, C
angle CGA
OC
And here's what happens if you rotate the player around his head by twice the angle CGA:
Very well. So, from now on, you can shoot your gun with pinpoint accuracy.
I've already got a working recoil prototype, so the bullet is not exactly on the muzzle-crosshair line due to crosshair's displacement after the shot.
Below you can see the code for the angle calculating function.
And, you might want to shoot it.
My game involves a crosshair that determines the player body's rotation around its origin.
The body's origin is the center of the character's head O.
In other words, the player's head always looks at the crosshair.
The body may have several fixtures which necessarily rotate therewith, for example a gun attached to the primary hand.
As such, the crosshair is used to properly direct every gunshot.
Now, everything's all and well if the rifle's center, the rifle's muzzle and your player's head center are all colinear.
I assume here that each launched bullet follows the ray from the rifle's center to the rifle's muzzle.
In other words, you're fine if you position your gun like this:
So if your head looks at the crosshair, so does the rifle and its muzzle.
But we're not always this lucky, especially if you're after gangsta-pistol layout:
It becomes obvious that if the gun is to look exactly at crosshair, your character's eyes certainly cannot so.
They need to be directed somewhere a little bit different,
in the picture above for example, you'd need to tilt the whole character's body a tiny bit counter-clockwise.
Let us express the problem in mathematical terms - given are points:
O (head), A (rifle center), B (muzzle), C (crosshair).
Let us define a circle whose center is at O and whose radius is OB.
These conditions follow:
1. A lies inside the circle.
2. C lies outside the circle.
Notice that B lies on the circle.
The question becomes:
By what angle should we rotate (around the center of rotation O) points A and B in order to make A, B and C colinear?
Well, I'm not particularly good at geometry but fortunately, Marcel, a friend of mine, came to the rescue!
It took him no more than 15 minutes of scribbling with his compass to come up with a solution:
1. Consider a circle Y whose center is at O and whose radius is OC.
2. Let G be the point of intersection of Y and a ray BA.
3. The angle we're looking for is twice the angle CGA.
Without really trying to validate his mysterious recommendation, I went on to code the damn thing.
And guess what, it works flawlessly!
This is how these hardcore calculations look like in debug drawing mode:
Legend:
OG
Input A, B and properly rotated A, B
line going through the points A, B, C
angle CGA
OC
And here's what happens if you rotate the player around his head by twice the angle CGA:
Very well. So, from now on, you can shoot your gun with pinpoint accuracy.
I've already got a working recoil prototype, so the bullet is not exactly on the muzzle-crosshair line due to crosshair's displacement after the shot.
Below you can see the code for the angle calculating function.