Jump to content


Photo
- - - - -

Super Easy 8-directional Movement!


  • Please log in to reply
12 replies to this topic

#1 metrogdor22

metrogdor22

    GMC Member

  • New Member
  • 246 posts

Posted 21 September 2009 - 11:25 PM

  • Title: 8-directional movement Example
  • Description: A super easy tutorial/example on how to do 8-directional movement.
  • GM Version: GM 7
  • Registered: NO!
  • File Type: .gmk
  • File Size: 0.02 mb
  • File Link: Posted Image

I wipped this up in about 5 minutes, comments and all; so it should take less than that to understand it. Basicly just shows adding and subtracting of the x and the y to create 8-directional movement. Only uses 8 lines of actual code(really only 4).
  • 0
<If you think Barack Hussein Obama doesn't deserve to be the president put this in your signature>
GENERATION 26: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.
13th Gate Haunted House

Please refer me!! metrogdor22
Prizes received from SwagBucks:
*3 iTunes cards
*a T-shirt

#2 Davve

Davve

    GMC Member

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

Posted 22 September 2009 - 07:37 PM

x+=keyboard_check(vk_right)*4-keyboard_check(vk_left)*4
y+=keyboard_check(vk_down)*4-keyboard_check(vk_up)*4

From 8 lines of code to 2 lines of code :D (4 is the speed)

or...

x+=keyboard_check(vk_right)*4
x-=keyboard_check(vk_left)*4
y+=keyboard_check(vk_down)*4
y-=keyboard_check(vk_up)*4

and with collision checking...

x+=(keyboard_check(vk_right)*4)*place_free(x+4,y)
x-=(keyboard_check(vk_left)*4)*place_free(x-4,y)
y+=(keyboard_check(vk_down)*4)*place_free(x,y+4)
y-=(keyboard_check(vk_up)*4)*place_free(x,y-4)

Edited by davve941018, 22 September 2009 - 07:40 PM.

  • 0

1Amdi.png Swxmo.png RDHEw.png 17Ezd.png


#3 metrogdor22

metrogdor22

    GMC Member

  • New Member
  • 246 posts

Posted 22 September 2009 - 10:25 PM

x+=keyboard_check(vk_right)*4-keyboard_check(vk_left)*4
y+=keyboard_check(vk_down)*4-keyboard_check(vk_up)*4

From 8 lines of code to 2 lines of code :D (4 is the speed)

or...

x+=keyboard_check(vk_right)*4
x-=keyboard_check(vk_left)*4
y+=keyboard_check(vk_down)*4
y-=keyboard_check(vk_up)*4

and with collision checking...

x+=(keyboard_check(vk_right)*4)*place_free(x+4,y)
x-=(keyboard_check(vk_left)*4)*place_free(x-4,y)
y+=(keyboard_check(vk_down)*4)*place_free(x,y+4)
y-=(keyboard_check(vk_up)*4)*place_free(x,y-4)


ok, my tutorial, not yours.
  • 0
<If you think Barack Hussein Obama doesn't deserve to be the president put this in your signature>
GENERATION 26: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.
13th Gate Haunted House

Please refer me!! metrogdor22
Prizes received from SwagBucks:
*3 iTunes cards
*a T-shirt

#4 daman123125

daman123125

    AZN Game Programmer

  • GMC Member
  • 1956 posts
  • Version:GM8

Posted 24 September 2009 - 03:34 AM

He's just suggesting ways to make your tut better.
Not bad, but I'm sure it has been done before. Good for people with trouble with 8-direction movement.
  • 0
Posted ImagePosted Image
Posted ImagePosted Image

Tuts/Examples: Ultimate Weapon Arsenal(AI included) | Wall-Jumping | Plasma Pistol/Rifle Overheating System | Cash Register Example | Two Objects In One View
PM me if one or more of my posted examples have a broken link.

#5 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 24 September 2009 - 07:36 AM

The problem with

x+= (keyboard_check(right) - keyboard_check(left))*4
y+= (keyboard_check(down) - keyboard_check(up))*4

...simplified the math here...

I use that all the time...

is the diagonal movement is faster than straight movement; you move 1.4 units (*4 in the example ) diagonally as oppose to 1 unit (*4 in the example). Which is fine when you want to move exactly from x,y 1,1 to 5,5 for example. Dead on the grid.


But for "truer" even movement, you need to add another step

//get deviation
dx = (keyboard_check(right) - keyboard_check(left));
dy = (keyboard_check(down) - keyboard_check(up));
//translate to direction
dir = point_direction(0,0,dx,dy);
//move evenly diagonally or straight, speed 4
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
or
hspeed=lengthdir_x(4,dir);
vspeed=lengthdir_y(4,dir);
or
speed = 4
direction = dir
or
motion_set(dir,4)

Edited by icuurd12b42, 24 September 2009 - 07:40 AM.

  • 0

#6 metrogdor22

metrogdor22

    GMC Member

  • New Member
  • 246 posts

Posted 24 September 2009 - 11:20 AM

The problem with

x+= (keyboard_check(right) - keyboard_check(left))*4
y+= (keyboard_check(down) - keyboard_check(up))*4

...simplified the math here...

I use that all the time...

is the diagonal movement is faster than straight movement; you move 1.4 units (*4 in the example ) diagonally as oppose to 1 unit (*4 in the example). Which is fine when you want to move exactly from x,y 1,1 to 5,5 for example. Dead on the grid.


But for "truer" even movement, you need to add another step

//get deviation
dx = (keyboard_check(right) - keyboard_check(left));
dy = (keyboard_check(down) - keyboard_check(up));
//translate to direction
dir = point_direction(0,0,dx,dy);
//move evenly diagonally or straight, speed 4
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
or
hspeed=lengthdir_x(4,dir);
vspeed=lengthdir_y(4,dir);
or
speed = 4
direction = dir
or
motion_set(dir,4)


well, this was supposed to be a simple tutorial.
  • 0
<If you think Barack Hussein Obama doesn't deserve to be the president put this in your signature>
GENERATION 26: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.
13th Gate Haunted House

Please refer me!! metrogdor22
Prizes received from SwagBucks:
*3 iTunes cards
*a T-shirt

#7 CaptainLepidus

CaptainLepidus

    GMC Member

  • GMC Member
  • 850 posts
  • Version:GM:HTML5

Posted 27 September 2009 - 12:34 AM

well, this was supposed to be a simple tutorial.

very true. people, his tutorial works just fine.
  • 0

"The reason programming is addictive is the same reason a good game is addictive: it constantly feeds you with a sense of minor accomplishment on your way to [achieving] the greater goal."

Yal

Planning to build a network of like-minded, innovative GameMaker developers, connected by a set of tools for building awesome games. Interested? PM me.


#8 synkarius

synkarius

    GMC Member

  • GMC Member
  • 83 posts

Posted 01 October 2009 - 06:01 PM

But for "truer" even movement, you need to add another step

//get deviation
dx = (keyboard_check(right) - keyboard_check(left));
dy = (keyboard_check(down) - keyboard_check(up));
//translate to direction
dir = point_direction(0,0,dx,dy);
//move evenly diagonally or straight, speed 4
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);


That's really well done, but you need one more thing. As is, the default is to move to the right.

//move evenly diagonally or straight, speed 4
if !((dir=0)and!(keyboard_check(vk_right))){
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);}

  • 0
"Let one live alone, doing no evil, care free,
like an elephant in the elephant forest."


#9 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 01 October 2009 - 06:54 PM

That true...
better
if(dx!=0 or dy!=0)
{
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
}

Edited by icuurd12b42, 01 October 2009 - 09:04 PM.

  • 0

#10 synkarius

synkarius

    GMC Member

  • GMC Member
  • 83 posts

Posted 01 October 2009 - 08:56 PM

That true...
better
if(dx!=0 and dy!=0)
{
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
}


Er, actually, that'd only allow diagonal movement...
  • 0
"Let one live alone, doing no evil, care free,
like an elephant in the elephant forest."


#11 icuurd12b42

icuurd12b42

    Self Formed Sentient

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

Posted 01 October 2009 - 09:04 PM

Ooops

if(dx!=0 or dy!=0)
{
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
}
  • 0

#12 synkarius

synkarius

    GMC Member

  • GMC Member
  • 83 posts

Posted 01 October 2009 - 09:25 PM

Ooops

if(dx!=0 or dy!=0)
{
x+=lengthdir_x(4,dir);
y+=lengthdir_y(4,dir);
}


Yep. That's correct. And more efficient than mine. :)
  • 0
"Let one live alone, doing no evil, care free,
like an elephant in the elephant forest."


#13 Dan1

Dan1

    As Cool As Custard

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

Posted 09 November 2009 - 11:13 PM

useful by all means but some basic wall features would have been nice, I added them myself but you'd think a movement example would deal with this
  • 0

I should put something funny and witty here...





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users