# 8 directional enemy rotation

12 replies to this topic

### #1 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 01:59 AM

I absolutely could not figure out how to word this on google, so I thought i'd post here for help, I hope no one minds answering this really noob question

Simply put, I want my enemy obj_zombie to face the direction he is walking. I have 8 zombie directional sprites, which are as follows:
spr_zombie_n
spr_zombie_s
spr_zombie_e
spr_zombie_w
spr_zombie_ne
spr_zombie_nw
spr_zombie_se
spr_zombie_sw

How can I do this? I tried a variation of
```if (direction>x and direction<x)
{sprite_index=spr_zombie_x}```
but all my directions seem screwed up, i cant figure out the game maker 360 clock, im real bad at math, and im looking for a simpler way. Thanks!

Edited by zkhammer, 03 October 2010 - 02:28 AM.

• 0

### #2 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 02:06 AM

If you have Pro use image_angle if not then use image_index = direction.

I do have pro, but the thing is that I don't want to angle the image, I want to change it based on what direction it is moving.
• 0

### #3 ND4SPD

ND4SPD

GMC Member

• GMC Member
• 2180 posts

Posted 03 October 2010 - 02:09 AM

If you have Pro use image_angle if not then use image_index = direction.

Depends on the view he's using. Is this top-down, do you have pro, and are the sprites animated?

Anyway, my suggestion is to put them all in an array and then you can put the angle in the index... that explanation didn't make as much sense as I'd hoped, so heres the code:

CREATE:
```zombie_sprite[0] = obj_zombie_e;
zombie_sprite[1] = obj_zombie_ne;
zombie_sprite[2] = obj_zombie_n;
zombie_sprite[3] = obj_zombie_nw;
zombie_sprite[4] = obj_zombie_w;
zombie_sprite[5] = obj_zombie_sw;
zombie_sprite[6] = obj_zombie_s;
zombie_sprite[7] = obj_zombie_se;```

STEP:
`sprite_index = zombie_sprite[ round(direction/45) ];`

• 0

### #4 FoxInABox

FoxInABox

GMC Member

• GMC Member
• 5586 posts
• Version:GM:Studio

Posted 03 October 2010 - 02:14 AM

```angle[0]=obj_zombie_e
angle[1]=obj_zombie_ne
angle[2]=obj_zombie_n
angle[3]=obj_zombie_nw
angle[4]=obj_zombie_w
angle[5]=obj_zombie_sw
angle[6]=obj_zombie_s
angle[7]=obj_zombie_se

sprite_index = angle[direction div 45]```
does the enemy move in 45 angles .. if it can move any angle then it migth be better to use:
((direction + 22.5) div 45) mod 8
direction mod 45
• 0

### #5 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 02:25 AM

If you have Pro use image_angle if not then use image_index = direction.

Depends on the view he's using. Is this top-down, do you have pro, and are the sprites animated?

Anyway, my suggestion is to put them all in an array and then you can put the angle in the index... that explanation didn't make as much sense as I'd hoped, so heres the code:

CREATE:
```zombie_sprite[0] = obj_zombie_e;
zombie_sprite[1] = obj_zombie_ne;
zombie_sprite[2] = obj_zombie_n;
zombie_sprite[3] = obj_zombie_nw;
zombie_sprite[4] = obj_zombie_w;
zombie_sprite[5] = obj_zombie_sw;
zombie_sprite[6] = obj_zombie_s;
zombie_sprite[7] = obj_zombie_se;```

STEP:
`sprite_index = zombie_sprite[ round(direction/45) ];`

this worked perfectly thanks! except now whenever the zombie is moving east, he disappears (as if obj_zombie_e was a blank sprite). Also, instead of facing north when moving north, he faces east. Anyone have any idea why this is? And thanks again for the help!

Edited by zkhammer, 03 October 2010 - 02:26 AM.

• 0

### #6 ND4SPD

ND4SPD

GMC Member

• GMC Member
• 2180 posts

Posted 03 October 2010 - 03:13 AM

If you have Pro use image_angle if not then use image_index = direction.

Depends on the view he's using. Is this top-down, do you have pro, and are the sprites animated?

Anyway, my suggestion is to put them all in an array and then you can put the angle in the index... that explanation didn't make as much sense as I'd hoped, so heres the code:

CREATE:
```zombie_sprite[0] = obj_zombie_e;
zombie_sprite[1] = obj_zombie_ne;
zombie_sprite[2] = obj_zombie_n;
zombie_sprite[3] = obj_zombie_nw;
zombie_sprite[4] = obj_zombie_w;
zombie_sprite[5] = obj_zombie_sw;
zombie_sprite[6] = obj_zombie_s;
zombie_sprite[7] = obj_zombie_se;```

STEP:
`sprite_index = zombie_sprite[ round(direction/45) ];`

this worked perfectly thanks! except now whenever the zombie is moving east, he disappears (as if obj_zombie_e was a blank sprite). Also, instead of facing north when moving north, he faces east. Anyone have any idea why this is? And thanks again for the help!

That's wierd. Does it give errors? Are you sure there's nothing else changing sprites? Are the sprites correct, with correct origins and so forth?
• 0

### #7 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 03:41 AM

Nope, only one zombie sprite changing code. I've uploaded my .gmk to mediafire in case anyone wants to take a look

http://www.mediafire...5x4k6cemd992m0k

The disappearing actually happens in the transition between east and southeast, which is really weird to me. Zombies still face right when moving up. Thanks for taking the time to help
• 0

### #8 ND4SPD

ND4SPD

GMC Member

• GMC Member
• 2180 posts

Posted 03 October 2010 - 03:44 AM

Nope, only one zombie sprite changing code. I've uploaded my .gmk to mediafire in case anyone wants to take a look

http://www.mediafire...5x4k6cemd992m0k

The disappearing actually happens in the transition between east and southeast, which is really weird to me. Zombies still face right when moving up. Thanks for taking the time to help

Sorry, I only have GM6.1.

Perhaps try some of FoxInABox's suggestions?

angle[0]=obj_zombie_e
angle[1]=obj_zombie_ne
angle[2]=obj_zombie_n
angle[3]=obj_zombie_nw
angle[4]=obj_zombie_w
angle[5]=obj_zombie_sw
angle[6]=obj_zombie_s
angle[7]=obj_zombie_se

sprite_index = angle[direction div 45]

does the enemy move in 45 angles .. if it can move any angle then it migth be better to use:
((direction + 22.5) div 45) mod 8
direction mod 45

• 0

### #9 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 03:55 AM

Nope, only one zombie sprite changing code. I've uploaded my .gmk to mediafire in case anyone wants to take a look

http://www.mediafire...5x4k6cemd992m0k

The disappearing actually happens in the transition between east and southeast, which is really weird to me. Zombies still face right when moving up. Thanks for taking the time to help

Sorry, I only have GM6.1.

Perhaps try some of FoxInABox's suggestions?

angle[0]=obj_zombie_e
angle[1]=obj_zombie_ne
angle[2]=obj_zombie_n
angle[3]=obj_zombie_nw
angle[4]=obj_zombie_w
angle[5]=obj_zombie_sw
angle[6]=obj_zombie_s
angle[7]=obj_zombie_se

sprite_index = angle[direction div 45]

does the enemy move in 45 angles .. if it can move any angle then it migth be better to use:
((direction + 22.5) div 45) mod 8
direction mod 45

yeah, ive tried that and 2 different other ways and the same thing happens in all of them. which leads me to conclude that there is nothing wrong with the code, but rather something is wrong with my sprites. i got the moving up to work for some reason
so now the only problem and question i have is:

how come between 90 degrees and 135 degrees, the sprite disappears?
(that is, 90 and 135 in real world not game maker world)

Edited by zkhammer, 03 October 2010 - 04:09 AM.

• 0

### #10 ND4SPD

ND4SPD

GMC Member

• GMC Member
• 2180 posts

Posted 03 October 2010 - 05:05 AM

You gotta get the angles in GM - clockwise from the right starting with 0*.

OMG JUST REALISED THAT YOU'VE GOT OBJ_ZOMBIE_ETC INSTEAD OF SPR_ZOMBIE_ETC!!!

If that doesn't solve it, try this in the step event:
```if direction >= 0+22.5 && direction < 45+22.5
sprite_index = spr_zombie_NE
if direction >= 45+22.5 && direction < 90+22.5
sprite_index = spr_zombie_N
if direction >= 90+22.5 && direction < 135+22.5
sprite_index = spr_zombie_NW
if direction >= 135+22.5 && direction < 180+22.5
sprite_index = spr_zombie_W
if direction >= 180+22.5 direction < 225+22.5
sprite_index = spr_zombie_SW
if direction >= 225+22.5 && direction < 270+22.5
sprite_index = spr_zombie_S
if direction >= 270+22.5 && direction < 315+22.5
sprite_index = spr_zombie_SE
if direction >= 315+22.5 && direction < 360
sprite_index = spr_zombie_E
if direction >= 0 && direction < 22.5
sprite_index = spr_zombie_E```

• 0

### #11 zkhammer

zkhammer

GMC Member

• GMC Member
• 163 posts

Posted 03 October 2010 - 05:16 AM

You gotta get the angles in GM - clockwise from the right starting with 0*.

OMG JUST REALISED THAT YOU'VE GOT OBJ_ZOMBIE_ETC INSTEAD OF SPR_ZOMBIE_ETC!!!

If that doesn't solve it, try this in the step event:

```if direction >= 0+22.5 && direction < 45+22.5
sprite_index = spr_zombie_NE
if direction >= 45+22.5 && direction < 90+22.5
sprite_index = spr_zombie_N
if direction >= 90+22.5 && direction < 135+22.5
sprite_index = spr_zombie_NW
if direction >= 135+22.5 && direction < 180+22.5
sprite_index = spr_zombie_W
if direction >= 180+22.5 direction < 225+22.5
sprite_index = spr_zombie_SW
if direction >= 225+22.5 && direction < 270+22.5
sprite_index = spr_zombie_S
if direction >= 270+22.5 && direction < 315+22.5
sprite_index = spr_zombie_SE
if direction >= 315+22.5 && direction < 360
sprite_index = spr_zombie_E
if direction >= 0 && direction < 22.5
sprite_index = spr_zombie_E```

haha yeah i facepalmed myself earlier for that but still after fixing was getting the disappearing problem.

buuuuut.......
.
.
.

YOU WIN!!! YOU FIXED IT!!! it seems i needed that last spr_zombie_e to complete the circle loop or something, thanks so much for the help man! ill add you to the "helped me with code" credits list
• 0

### #12 ND4SPD

ND4SPD

GMC Member

• GMC Member
• 2180 posts

Posted 03 October 2010 - 05:28 AM

You gotta get the angles in GM - clockwise from the right starting with 0*.

OMG JUST REALISED THAT YOU'VE GOT OBJ_ZOMBIE_ETC INSTEAD OF SPR_ZOMBIE_ETC!!!

If that doesn't solve it, try this in the step event:

```if direction >= 0+22.5 && direction < 45+22.5
sprite_index = spr_zombie_NE
if direction >= 45+22.5 && direction < 90+22.5
sprite_index = spr_zombie_N
if direction >= 90+22.5 && direction < 135+22.5
sprite_index = spr_zombie_NW
if direction >= 135+22.5 && direction < 180+22.5
sprite_index = spr_zombie_W
if direction >= 180+22.5 direction < 225+22.5
sprite_index = spr_zombie_SW
if direction >= 225+22.5 && direction < 270+22.5
sprite_index = spr_zombie_S
if direction >= 270+22.5 && direction < 315+22.5
sprite_index = spr_zombie_SE
if direction >= 315+22.5 && direction < 360
sprite_index = spr_zombie_E
if direction >= 0 && direction < 22.5
sprite_index = spr_zombie_E```

haha yeah i facepalmed myself earlier for that but still after fixing was getting the disappearing problem.

buuuuut.......
.
.
.

YOU WIN!!! YOU FIXED IT!!! it seems i needed that last spr_zombie_e to complete the circle loop or something, thanks so much for the help man! ill add you to the "helped me with code" credits list

You're welcome bud
• 0

### #13 FoxInABox

FoxInABox

GMC Member

• GMC Member
• 5586 posts
• Version:GM:Studio

Posted 03 October 2010 - 05:39 AM

I tested:
`sprite_index = zombie_sprite[ ((direction+22.5) div 45) mod 8 ];`
in your game .. it seemed to work fine from all angles, and I didn't get any dissapearing sprite then..

but I had to remove the other sprite code you had there .. because you had set it right after so it messed with the other code ^^
• 0

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users