Pixel-Perfect (better than 360 degrees) Shooting Tutorial
Author:|| Jack Zhou
Submitted:|| 11th July, 2009
This tutorial allows you to create a shooter that will shoot to where your mouse is, accurate to the pixel.
-counter (our timer)
Events (comments come after //):
First, we do some random config/setup:
Start of Frame
(shooter): Set position at (320,240) //nothing, just centres shooter
Hide Windows mouse pointer
(crosshair): Set X and Y positions to XMouse and YMouse
(shooter): Look at (0, 0) from (crosshair) //cosmetic
The timer is used to keep track of how many centiseconds (0.01s) has passed since the start of the frame. We'll let it default to start at 0 and max at 999999999
(counter): Add 1 to counter
Global Val A is used to store the bullet counter, and we'll store this to the bullet's Alt Val A on every shot (i.e. so every bullet has its own number). Global Vals X and Y are used to temporarily store the location of the mouse at the time of the shot. The time itself is temporarily stored into Global Val T. These will be stored into the bullet's X, Y, and T values later, and values C and D will be used to hold the bullet's original position.
Note: These values will all be used to calculate the movement later on.
Repeat while left mouse-key is pressed //can be replaced for single shots only
Add 1 to Global Value A
Create (bullet) at (0, 0) from (shooter) (action spot)
Set Global Value X to XMouse
Set Global Value Y to YMouse
Set Global Value T to value(counter)
Alterable Value A of (bullet) = 0 //only happens right after the shot is made, when all its values are 0 by default
(bullet): Set Alterable Value A to Global Value A
(bullet): Set Alterable Value C to X(bullet)
(bullet): Set Alterable Value D to Y(bullet)
Flag 3 (randomly chosen) will be used to store the "has the bullet's direction and movement been determined?", where off = no, on = yes.
To calculate the direction, we take the difference in X and Y values between the bullet and the mouse position, and use the Pythagorean Theorem to find the absolute distance. We want the bullet to move at a fixed rate (e.g. 5 in this example), so we divide the length determined by Pythagorean Theorem by 5 to find out what we have to multiply the difference in X and Y by so that the bullet will in fact move 5 pixels every 00"-01. (This applies similar triangles. Try drawing stuff out if you don't understand. This is the most complicated part of the tutorial.
Although not necessary, per se, we'll use Alt Vals V and W to store the difference between the X and Y values of the bullet and the cursor at the time of the shot.
We'll use a bullet's Alt Val U to store the length of the hypotenuse determined by the Theorem. We divide this by 5, and then multiply the result by V and W, and store these in M and N. M and N basically says how many pixels in X and Y the bullet should move per 00"-01. This is a decimal value. After everything, set flag 3 on so the bullet can move.
(bullet): Internal flag 3 is off
(bullet): Set Alterable Value V to Alterable Value X(bullet) - X(bullet)
(bullet): Set Alterable Value W to Alterable Value Y(bullet) - Y(bullet)
(bullet): Set Alterable Value U to Sqr(Alterable Value V^2 + Alterable Value W^2)/5 //note you divide by the number of pixels you want the bullet to move, per 00"-01
(bullet): Set Alterable Value M to Alterable Value V(bullet)/Alterable Value U(bullet)
(bullet): Set Alterable Value N to Alterable Value W(bullet)/Alterable Value U(bullet)
(bullet): Set internal flag 3 on
Once a bullet's flag 3 is on, we set position to original + time passed * change in position (stored in M and N).
Note: it is VITAL that you do it this way, and not move the bullet by M and N every 00"-01. The latter way ignores decimals, which will ruin the accuracy.
(bullet): Internal flag 3 is on
(bullet): Set X position to Alterable Value C(bullet) + (value(counter) - Alterable Value T(bullet)) * Alterable Value M(bullet) //this is just original + time passed * change
(bullet): Set Y position to Alterable Value D(bullet) + (value(counter) - Alterable Value T(bullet)) * Alterable Value N(bullet)
Well, that's it! It looks complicated, but the math behind it is actually really simple. A downloadable MMF/MMF2 example is available, at http://www.create-games.com/download.asp?id=7842.
1. If you somehow use the program for more than 115 days at once, the timer counter will max at 999999999 and will not move. You can make this over 231 days by having it start at -999999999, or you can just reset the counter to 0 afterwards (although that might cause problems).
2. This depends on the timer counter to work, so if the game lags very hard, it's possible the bullets will lag equally hard.
3. You tell me! =P
Enjoy, and have fun!