Jump to content


Photo

Smallest Movement Script Challenge


  • Please log in to reply
14 replies to this topic

#1 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 30 May 2012 - 10:33 PM

  • Title: Smallest Movement Script Challenge
  • Description: Make player or AI movement scripts as short and as efficient as possible
  • Deadline: Ongoing
Summary
Ladies and gents, show us your knowledge of GM functions and math by reducing player and AI movement to just a few lines!!!

Categories
  • 2d Top Down
    • Walking Person
    • Driving Car
    • Driving Tank
    • Flying Plane
    • Flying Space Ship
  • 2d Platform
    • Walking Person
    • Driving Car
    • Driving Tank
    • Flying Plane (Think Atari Plane Combat on the 2600)
    • Flying Space Ship (Think Defender or Lunar Lander)

Control Type
  • KB Only
  • Mouse
  • Joystick
  • Other (Phone Tilt/Touch Screen)

Bonus
Extra Points if your method can support multiple Control Types.
Extra points if your movement can supports speed based and coordinate based movement.
Extra points if your movement can supports Player and AI.
Mega Extra Points if your movement method supports multiple categories

Restrictions
Collision must be handled separately and not be dependent on your movement code.
Method has to be encapsulated in a single script, and, possibly a support init script called in the create of an object (no penalties for init scripts)
You must provide a project file. GMK to GMZ (Studio) format.
If packaged, use zip format.

Prize
Bragging rights and your code be referenced when a newbie asks a "How do I move like this" question...
Best methods will be referenced below and the winner will be referenced in the lists above.

ATTENTION
One script per post, one post may show variants of the same script and suggestions on usage. I plan to link to the individual solutions and so I do not want people getting confused with 5 scripts for 3 categories in one post. I will forgo the no double post rule for this so if you have a solution for many categories you may post them one after the other.

Simplicity and Efficiency and Human readable; a few vars and comments are allowed and encouraged, being able to define the keys via variables is also encouraged.

Please add the tags
[2d Top Down]
[Walking Person]
[KB][Mouse]
for example for a Top down Walking Person Keyboard and Mouse movement, matching the above mentioned categories
  • 0

#2 ThatSnail

ThatSnail

    That Guy

  • GMC Member
  • 1035 posts
  • Version:GM8

Posted 31 May 2012 - 03:27 AM

[2d Top Down]
[Walking Person]
[KB][Mouse]

Are we going for simplicity and efficiency or the fewest lines?
Because this is technically a one-liner that supports both keyboard and mouse input.
motion_set(radtodeg(arctan2((keyboard_check(vk_up)-keyboard_check(vk_down))*4-lengthdir_y(mouse_check_button(mb_left)*4,point_direction(x,y,mouse_x,mouse_y)),(keyboard_check(vk_right)-keyboard_check(vk_left))*4+lengthdir_x(mouse_check_button(mb_left)*4,point_direction(x,y,mouse_x,mouse_y)))),(mouse_check_button(mb_left) || keyboard_check(vk_left) || keyboard_check(vk_right) || keyboard_check(vk_up) || keyboard_check(vk_down))*4);
.GMK

Edited by ThatSnail, 31 May 2012 - 03:31 AM.

  • 1

#3 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 31 May 2012 - 04:12 AM

[2d Top Down]
[Walking Person]
[KB][Mouse]

Are we going for simplicity and efficiency or the fewest lines?
Because this is technically a one-liner that supports both keyboard and mouse input.

motion_set(radtodeg(arctan2((keyboard_check(vk_up)-keyboard_check(vk_down))*4-lengthdir_y(mouse_check_button(mb_left)*4,point_direction(x,y,mouse_x,mouse_y)),(keyboard_check(vk_right)-keyboard_check(vk_left))*4+lengthdir_x(mouse_check_button(mb_left)*4,point_direction(x,y,mouse_x,mouse_y)))),(mouse_check_button(mb_left) || keyboard_check(vk_left) || keyboard_check(vk_right) || keyboard_check(vk_up) || keyboard_check(vk_down))*4);
.GMK



Simplicity and Efficiency and Human readable. a few vars and comments are allowed and encouraged, being able to define the keys via variables is also encouraged.
  • 0

#4 slayer 64

slayer 64

    GMC Member

  • GMC Member
  • 3276 posts
  • Version:GM8.1

Posted 14 June 2012 - 01:15 AM

[top down]
[flying spaceship]
[KB]

this is a real interesting topic to me, wonder why there's so little posts. what do mean by changing the keys with variables? can you show an example? i use this code a lot for movement.

var ax,ay;

ax=keyboard_check(vk_right)-keyboard_check(vk_right)
ay=keyboard_check(vk_down)-keyboard_check(vk_up)

if ax!=0
or ay!=0
{
motion_add(1,point_direction(0,0,ax,ay))
}

  • 2

#5 YellowAfterlife

YellowAfterlife

    GMC Member

  • Global Moderators
  • 3491 posts
  • Version:GM:Studio

Posted 14 June 2012 - 01:33 AM

Thing similar to slayer's code. Math way:
//@tags [2d top down] [flying spaceship] [KB]
var kx, ky, kl;
kx = keyboard_check(key_right) - keyboard_check(key_left)
ky = keyboard_check(key_down) - keyboard_check(key_up)
kl = sqrt(kx * kx + ky * ky) // vector length
if (kl != 0) { // add normalized vector to speed:
    hspeed += kx / kl
    vspeed += ky / kl
}
// optional: friction & limitation
speed = median(0, speed - 0.3, 10)

  • 0

#6 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 14 June 2012 - 02:10 AM

I like to use this after code like yours
friction = speed /20;

works for when the speed is negative as well...

so here is my usual spaceship code

//[2d top down Asteroid] [2d platform Lander] [flying spaceship] [KB]
//gravity = .5;
m_turns = 5;
m_accel = .2;
m_decel = 20;

image_angle+=(keyboard_check(vk_left)-keyboard_check(vk_right)) * m_turns
motion_add(keyboard_check(vk_up)-keyboard_check(vk_down))*m_accel,image_angle)
friction = speed/m_decel;


//[2d top down ASteroid] [2d platform Lander] [flying spaceship] [KB + turn to mouse]
//gravity = .5;
m_accel = .2;
m_decel = 20;

image_angle=point_direction(x,y,mouse_x,mouse_y);
motion_add(keyboard_check(vk_up)-keyboard_check(vk_down))*m_accel,image_angle)
friction = speed/m_decel;

//[2d platform Defender] [flying spaceship] [KB]
m_accel = .2;
m_decel = 20;
m_ys = 2;

keyboard_check_pressed(vk_space) image_xscale *= -1;
y+=(keyboard_check(vk_down)-keyboard_check(vk_up)) * ys;
hspeed += keyboard_check(ord("Z") *m_accel * image_xscale;
friction = speed/m_decel;

  • 0

#7 blopit

blopit

    KING OF EVERYTHING

  • GMC Member
  • 784 posts
  • Version:GM8.1

Posted 18 June 2012 - 09:38 PM

this is pretty long for a small movement code but w/e
got carried away



//[2d platform] [flying spaceship] [KB] [Mouse]
g = .5;
turn_spd = 2;
acc = .75;
vert_acc = 1;
steady_rate = .1;
fric = .01;
max_hsp = 8;
max_vsp = 10;
max_turn = 30;

type = 0; // 0 = keyboard; 1 = mouse; 2 = coordinate;

//configure coordinates
dx = 320;
dy = 200;

//configure control type

c1 = abs(sign(-type+1))*abs(sign(-type+2));
c2 = abs(sign(type))*abs(sign(-type+2));
c3 = abs(sign(type))*abs(sign(-type+1));
n = (keyboard_check(vk_left)-keyboard_check(vk_right))*c1 + sign(x-mouse_x)*c2 + sign(x-dx)*c3;
up = keyboard_check(vk_up)*c1 + (mouse_y<=y)*c2 + (dy<=y)*c3;

//movement
image_angle = median(image_angle*(1-(!abs(n)*steady_rate))+n*turn_spd,max_turn,-max_turn);
vspeed = median(vspeed+(g-up*vert_acc),-max_vsp,max_vsp);
hspeed = median(hspeed*(1-(!abs(n)*fric))+lengthdir_x(acc,image_angle+90),-max_hsp,max_hsp);

Edit: fixed control problems

Edited by blopit, 18 June 2012 - 09:53 PM.

  • 0

#8 michael pw

michael pw

    GMC Member

  • GMC Member
  • 1488 posts
  • Version:GM8.1

Posted 26 June 2012 - 07:12 AM

Here's my attempt, pretty compact tbh:
Primary Method:
//[2D Top Down] [Basic Top Down movement in 8 directions. With customization controls]
//define custom controls
global.key[0/*left*/]  = vk_right;
global.key[1/*up*/]    = vk_up;
global.key[2/*right*/] = vk_left;
global.key[3/*down*/]  = vk_down;
//other variables (just replacing hard-coded variables, could be made smaller if code is hard coded)
ms = 3; //Movement Speed
//Move
var mdir, mc; mdir=0; mc=0;
for(i=0; i<=3; i+=1){ k[i]=0; if(keyboard_check(global.key[i])){mdir += (i*90); mc += 1 k[i]=1;}} 
motion_set(((mdir)/(mc+.0001))+(((k[0]+k[3])==2)*180), (mc>=1)*ms);


Alternative methods to motion set:
//Second Method replacing Motion_set
//*********************************************************
var d,s;
d = ((mdir)/(mc+.0001))+(((k[0]+k[3])==2)*180);s = (mc>=1)*ms;
x += lengthdir_x(d,s);
y += lengthdir_y(d,s);
//*********************************************************
//OR
//*********************************************************
var d,s;
d = ((mdir)/(mc+.0001))+(((k[0]+k[3])==2)*180);s = (mc>=1)*ms;
x += cos(d*pi/180)*s;
y -= sin(d*pi/180)*s;
//*********************************************************

Edited by michael pw, 26 June 2012 - 07:17 AM.

  • 0

#9 TheJob

TheJob

    GMC Member

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

Posted 26 September 2012 - 11:28 PM

///Top-Down, Basic Zombie AI Movement
{
	direction = point_direction(x,y,obj_player.x,obj_player.y);
	image_angel = direction;
	speed = random_range(1,3);
}


I win?

Edited by TheJob, 26 September 2012 - 11:29 PM.

  • 0

#10 johnki

johnki

    GMC Member

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

Posted 02 October 2012 - 04:24 AM

Wow, I've been looking at movement code the wrong way, apparently...

The whole "if right key pressed, x += 1" thing seems ridiculously simple compared to some of these.
  • 0

#11 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 02 October 2012 - 05:04 AM

Wow, I've been looking at movement code the wrong way, apparently...

The whole "if right key pressed, x += 1" thing seems ridiculously simple compared to some of these.


Most if logic can be moved right in the math

if (right==true) x+=4
if (left==true) x+=4

becomes
x+=4*(right==true)
x-=4*(left==true)
for example

then merged into a single line
x+= 4*((right==true)-(left==true));

becomes 4*(0-0), 4*(1-0), 4*(0-1), 4*(1-1)
yielding 4*0, 4*1, 4*-1, 4*0;
finally 0, 4, -4, 0
x+=0, x+=4, x+=-4, x+=0;


It's only simpler if you look at it in the right light.
  • 1

#12 KeeZeeK

KeeZeeK

    Nerd and Proud

  • GMC Member
  • 71 posts
  • Version:GM8

Posted 09 October 2012 - 02:57 PM

Here I come. But there's a bug: Due to math problems, when pressing LEFT and DOWN (or RIGHT and UP), it goes to the top (or to the bottom).

Top-down, Player Movement, Only Keyboard.

left=keyboard_check(vk_left); //check if left is pressed.
up=keyboard_check(vk_up); //check if up is pressed.
down=keyboard_check(vk_down); //check if down is pressed.
direction=90*((2*left)+(1*up)+(3*down)); //calculate the direction using math and Game Maker true and false variables (respectively 0 and 1).
speed=keyboard_check(vk_left or vk_right or vk_up or vk_down)*4; //if one of the arrow keys is pressed, move.

or in a single line:

direction=90*((2*keyboard_check(vk_left))+(1*keyboard_check(vk_up))+(3*keyboard_check(vk_down))); //putting the checks in there will still work, but it's a bit confusing.
speed=keyboard_check(vk_left or vk_right or vk_up or vk_down)*4; //if one of the arrow keys is pressed, move.

Edited by KeeZeeK, 09 October 2012 - 03:25 PM.

  • 0

#13 Schalk

Schalk

    GMC Member

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

Posted 12 October 2012 - 02:45 AM


Wow, I've been looking at movement code the wrong way, apparently...

The whole "if right key pressed, x += 1" thing seems ridiculously simple compared to some of these.


Most if logic can be moved right in the math

if (right==true) x+=4
if (left==true) x+=4

becomes
x+=4*(right==true)
x-=4*(left==true)
for example

then merged into a single line
x+= 4*((right==true)-(left==true));

becomes 4*(0-0), 4*(1-0), 4*(0-1), 4*(1-1)
yielding 4*0, 4*1, 4*-1, 4*0;
finally 0, 4, -4, 0
x+=0, x+=4, x+=-4, x+=0;


It's only simpler if you look at it in the right light.


Or even
x+= 4*(right-left);

instead of

x+= 4*((right==true)-(left==true));
  • 0

#14 icuurd12b42

icuurd12b42

    Self Formed Sentient

  • Global Moderators
  • 14389 posts
  • Version:GM:Studio

Posted 12 October 2012 - 02:59 AM

Good keep em coming.
  • 0

#15 Popcorn123

Popcorn123

    JNC Games Creator

  • GMC Member
  • 371 posts
  • Version:GM8

Posted 27 November 2012 - 01:08 AM

Here's my attempt (sorry, breaking the no if statement streak)
//    [2d top down]   [KB]
if keyboard_check(vk_up) {y-=5}
if keyboard_check(vk_down) {y+=5}
if keyboard_check(vk_right) {x+=5}
if keyboard_check(vk_left) {x-=5}

Edited by Popcorn123, 27 November 2012 - 01:10 AM.

  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users