- Title: Selecting/rotating The Right Sprite From Angle
- Description: Code to properly select sprite index or an array holding multiple angled animated sprites according to an angle
- GM Version:
- Registered: No
- File Type: .zip
- File Size: 4kb
- File Link: http://host-a.net/ic.../directions.zip
- Required Extensions: none
- Required DLLs: none
Selecting the right animation sprite from angle for Zelda or top down type game, even platforms or rotating a sprite for unregistered GM users.
Trying to look at the direction angle to figure out what animation sprite should be chosen can be tedious, even tricky when the angle goes below 0 or above 360.
This code snippet will help you to select the right sprite animation according to the direction of the instance, or any other angle based variable you choose.
You can use this to select the correct sprite index such as for an animated sprite in a Zelda type game or to emulate rotating a sprite using sub images such as for unregistered GM users.
By splitting the direction into 8 triangular regions (pieces of pie) and associating each region with a sprite, you can easily select the right sprite.
In this example, I actually used the mouse position in relation to the object for quick angle detection to simplify the example.
you have to make sure your angle does not go below 0 using
while (direction <0) direction +=360; //or while (angle<0) angle+=360; //if using your own variable //or angle = ((angle mod 360) + 360) mod 360before using the sprite selection code
This example select sprites for 8 directions, right, rightup, up, leftup, left, leftdown, down and downrightâ€¦
//////8 directions direction = point_direction(x,y,mouse_x,mouse_y); var dirs; dirs = right_spr dirs = rightup_spr dirs = up_spr dirs = leftup_spr dirs = left_spr dirs = leftdown_spr dirs = down_spr dirs = rightdown_spr var numdirs; numdirs = 8 var d; d = floor(((direction+180/numdirs) mod 360)/(360/numdirs)) sprite_index = dirs[d]
d = floor(((direction+180/numdirs) mod 360)/(360/numdirs))
Maps the angles to 8 different positions and offsets the pieces of pie so the right sprite will be selected when the angle is 0 +- a few degrees. In short, the first piece of pie will be directly on the right.
If I remove +180/numdirs from the equation then the pieces of pie will not be offset, causing the right sprite to be selected only when the angle is above 0â€¦ The edge of the piece of pie starts at 0 degrees. The gmk/gm6 file illustrates this better.
This example select sprites for 4 directions, right, up, left and downâ€¦
//////4 directions direction = point_direction(x,y,mouse_x,mouse_y); var dirs; dirs = right_spr dirs = up_spr dirs = left_spr dirs = down_spr var numdirs; numdirs = 4 var d; d = floor(((direction+180/numdirs) mod 360)/(360/numdirs)) sprite_index = dirs[d]
By removing the pieces of pie offset, you can have the system select sprites for moving in a diagonal fashion like in a 4 direction movement isometric game.
direction = point_direction(x,y,mouse_x,mouse_y); var dirs; dirs = rightup_spr dirs = leftup_spr dirs = leftdown_spr dirs = rightdown_spr var numdirs; numdirs = 4 var d; d = floor(((direction) mod 360)/(360/numdirs)) sprite_index = dirs[d]
You can also use the system to select a sprite from sub images such as for unregistered GM users trying to add rotation to their top down game sprites.
You create your sprite facing right, then use the Animation/Rotation Sequence/Counter-Clockwise option, Specify the number of frames to be 8,16 or 36. The example gmk uses 36 and 18... I screwed up. Shows the code does not care how many sub images you use though. You then set the sprite to the object, making sure you set the animation speed to 0 in the create event
image_speed = 0and the following code should select the right sub image from the sprite
direction = point_direction(x,y,mouse_x,mouse_y); var numdirs; numdirs = image_number; var d; d = floor(((direction+180/numdirs) mod 360)/(360/numdirs)) //rotate the image for unregistered using image_index image_index = d;
Of course, you can always merge all the line of code into a single one...
sprite_index = dirs[floor(((direction+180/numdirs) mod 360)/(360/numdirs))]or, for rotating using sub images
image_index = (((direction+180/image_number) mod 360)/(360/image_number))or course, GM handles image index being a fraction and being beyond the range of image_number so this code:
image_index = direction/360*image_numberWill also do the trick but the angles will not be corrected to match the pieces of pie.
This applies the right correction
image_index = (direction+180/image_number)/360*image_number
See it in action:
Edited by icuurd12b42, 25 September 2011 - 02:08 AM.