Jump to content


Photo
- - - - -

Selecting/rotating The Right Sprite From Angle


  • Please log in to reply
2 replies to this topic

#1 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 13 December 2007 - 11:37 PM

  • 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: :GM7: :GM6:
  • Registered: No
  • File Type: .zip
  • File Size: 4kb
  • File Link: http://host-a.net/ic.../directions.zip
  • Required Extensions: none
  • Required DLLs: none
Summary
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.

[edit]
Important note...
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 360
before using the sprite selection code
[/edit]

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[0] = right_spr
dirs[1] = rightup_spr
dirs[2] = up_spr
dirs[3] = leftup_spr
dirs[4] = left_spr
dirs[5] = leftdown_spr
dirs[6] = down_spr
dirs[7] = rightdown_spr

var numdirs;
numdirs = 8

var d;
d = floor(((direction+180/numdirs) mod 360)/(360/numdirs))
sprite_index = dirs[d]

This function:
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[0] = right_spr
dirs[1] = up_spr
dirs[2] = left_spr
dirs[3] = 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[0] = rightup_spr
dirs[1] = leftup_spr
dirs[2] = leftdown_spr
dirs[3] = 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 = 0
and 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_number
Will 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:
:GM6: :P Projects
Posted Image

Edited by icuurd12b42, 25 September 2011 - 02:08 AM.

  • 1

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#2 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 18 December 2007 - 01:46 AM

I was finally able to access my account on host-a.net. So the host-a and the hostforfood are now the same file
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button


#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • GMC Elder
  • 18181 posts
  • Version:GM:Studio

Posted 22 July 2011 - 06:07 PM

Std Msg: Please fix the download link (The Tutorial Clean Up Crew).

Std Msg: Please use the tutorial template header (The Tutorial Clean Up Crew).
  • 0

gmcbanner.pnggmcbanner_tools.png

ICU Live Tutoring Through Slack or Skype | My Tools Page follow.png

I FRANTICALLY MADE MY 18000 POST TOPIC BEFORE MIKE ANNOUNCED A DELAY...
Now I'm squirming not to hit that reply button