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

#2 Davve

Davve

    GMC Member

  • GMC Member
  • 4014 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

#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

#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

#5 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Retired Staff
  • 15744 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

#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

#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

#9 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Retired Staff
  • 15744 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

#11 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Retired Staff
  • 15744 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

#13 Dan1

Dan1

    As Cool As Custard

  • GMC Member
  • 1830 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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users