Yup, all three covered in one easy-to-understand article. It's simpler than you think, especially when you use extensions to do most of the work.


360° shooting

This is done using the trigonometric functions Sine and Cosine (sin and cos). Although these functions are used a lot in triangles in Maths, they're also used in circles since the principles are the same. If you want to learn more about them, search on Google - it's not important here. All you need to understand is the following formulae:

Movement in X axis = Cos( Angle of movement ) * Speed
Movement in Y axis = Sin( Angle of movement ) * Speed * -1

This is assuming that 0° is east and 90° is north (MMF's direction system).

The way you would use these formulae in your game is by adding the X and Y movement to the X and Y positions, as so:

Set X position to X( Active ) + Cos( Angle( Active ) ) * Speed( Active )
Set Y position to Y( Active ) - Sin( Angle( Active ) ) * Speed( Active )
(Subtracting the value means we can remove the * -1 from the equation.)

Instant hit bullets

So how does this work in our games? When firing bullets, we can use the Move Safely object to help us create instantly hitting bullets. This object simply requires you to prepare a safety procedure (which remembers the position of the bullet), move the bullet yourself, and then start the safety procedure. The object will then move the object very slowly, one pixel at a time, from its starting position until it reaches its final destination. It provides an event for you to test if the object has hit any obstacles or enemies along the way - 'On Safety', which occurs each time the Move Safely object moves the object. DONATE TO TDC.

Using this, all we need to do is prepare the safety procedure, move the bullet in the angle that was fired using a very high speed, commence the safety procedure and set up a few events to handle collisions with the bullet. The speed of the bullet must be high enough so that it can leave the play area comfortably, but not too high that it will wrap around the other side. I suggest 5000 pixels; you shouldn't need to change that. The events used are as follows:

• Player presses fire 1 (or whatever shooting event you have)
(Create object) : Create (Bullet) at (0,0) from (Player's gun)
(Move Safely) : Set object to (Bullet)
(Move Safely) : Prepare Safety
(Bullet) : Set X Position to X( Bullet ) + Cos( Angle( Player ) ) * 5000
(Bullet) : Set Y Position to Y( Bullet ) - Sin( Angle( Player ) ) * 5000
(Move Safely) : Commence Safety Procedure
(Bullet) : Destroy

• On Safety
+ (Bullet) is overlapping a backdrop
(Bullet) : Destroy
(Move Safely) : Stop Safety Procedure

• On Safety
+ (Bullet) is overlapping (Enemy)
(Bullet) : Destroy
(Enemy) : Destroy
(Move Safely) : Stop Safety Procedure

• On Safety
+ (Bullet) is out of the play area
(Bullet) : Destroy
(Move Safely) : Stop Safety Procedure


It's as simple as that. You now have an instantly hitting bullet. The example assumes that you have the angle (0-359) of the player stored in an alterable value - if you're not using 360° movement and want to use the actual direction instead, multiply the direction by 11.25. For example:

(Bullet) : Set X Position to X( Bullet ) + Cos( Dir( Player ) * 11.25 ) * 5000


Inaccurate fire

Of course, guns don't fire in a perfectly straight line, especially when you've already fired bullets before (the kickback will affect the person's own accuracy, too). Fortunately, this is extremely easy to simulate - we simply adjust the angle to fire at by a random value. If we want to change the fire angle to ±3° of the gun angle, we subtract 3 and add a random value from 0 to 6. (In short, Angle - 3 + random(7) ) However, there are two things to realise:

1. Set the new angle in an alterable value, so it will be the same in both actions
2. MMF's Random() expression doesn't use floats, so we lose a lot of variance.

To fix 2, we instead use Random(601) and divide by 100.0 - this gives us a random DECIMAL number, accurate to two decimal places (so we can have things like 2.72). (Why 601 and not 700? Because the Random() expression gives you a number from 0 to your number - 1. Confusing, huh?)

However, all this really means in the grand scheme of things is that you add another action to set an alterable value, and refer to this alterable value instead of the angle used in the Set X/Y Position actions.

The new shooting event is as follows:

• Player presses fire 1 (or whatever shooting event you have)
(Create object) : Create (Bullet) at (0,0) from (Player's gun)
(Move Safely) : Set object to (Bullet)
(Move Safely) : Prepare Safety
(Bullet) : Set alterable value Angle to Angle( Player ) - 3 + ( Random( 601 ) / 100.0 )
(Bullet) : Set X Position to X( Bullet ) + Cos( Angle( Bullet ) ) * 5000
(Bullet) : Set Y Position to Y( Bullet ) - Sin( Angle( Bullet ) ) * 5000
(Move Safely) : Commence Safety Procedure
(Bullet) : Destroy

Note the 5 letters that change in the Set X/Y position action


Example file

If looking at real code helps, download this example on the Clickteam Forums:
http://www.clickteam.com/CTforum/showflat.php3?Board=upload&Number=238757&page=0&view=collapsed


If all went well, you'll have learnt how to make 360 degree instant hit bullets with inaccuracy, and/or picked up on my subliminal message. Have fun in games creation! (This article was sponsored by ShadowCaster. Maybe.)