GameMaker: Studio

GameMaker: Studio

View Stats:
Jeb Dec 9, 2016 @ 12:00pm
Help with player acceleration
Im trying to implement player acceleration for a side scrolling platformer.
I have googled it but can't find anything which works well with the code I have wrote.

heres the code that I have in the step block:

///Movement left and right


left = keyboard_check(ord("D"));
right = -keyboard_check(ord("A"));

hspd = (left + right) * spd;
hspeed = hspd;

//spd is set to 5 in the create block.
//the hspd is there so that hspeed is either
//-1 * spd or 1 * spd or 0 * spd
//depending on which key is pressed.


This works fine as a basic move left and right funtcion though having some form of acceleration will make the movement smoother.

If you have any suggestions please let me know and thanks in advance :)
< >
Showing 1-15 of 17 comments
The Winter Bud Dec 9, 2016 @ 12:19pm 
EDIT: code has changed since original and new code has been supplied
Instead of giving the player a full 'spd' value when he moves, have him move towards that max value.
create event
hspd=0; // this must be initialized or you will get an error accl=1; // the ammount per step to increment speed by until maximum is reached hdir=0; // current horizontal direction player is moving
movement code, change your hspd= line to the one below
hdir=(left+right) hspd=median(-spd,spd, hspd+hdir*accl); hspeed=hspd;
median(value1,value2,value3,etc) is a math function that will return the value that is as close to the average of all values supplied to it. aka the middle value
example
hspd=0 -> accl=1
  • step 1 -> right key is pressed
    hspd=median(-5,5,0+1*1) -> hspd=1
  • step 2 -> right key is pressed
    hspd=median(-5,5,.1+1*1) -> hspd=2
  • step 3-5 -> the same thing hspd=3,4 and 5 respectively
  • step 6 -> right key pressed
    hspd=median(-5,5,5+1*1) -> hspd=5 <-- see maxium speed
  • step 7 -> left key being pressed
    hspd=median(-5,5,5+-1*1) -> hspd=4;
  • step 8 - left key being pressed
    hspd=median(-5,5,4+-1*1) -> hspd=3;
  • step 9, 10, and 11 -> the same thing hspd=2,1, and 0 respectively
  • step 12 -> left key being pressed
    hspd=median(-5,5,0-1*1) -> hspd=-1
Last edited by The Winter Bud; Dec 9, 2016 @ 12:42pm
Jeb Dec 9, 2016 @ 12:36pm 
Originally posted by Dennis Tudor Jr:
Instead of giving the player a full 'spd' value when he moves, have him move towards that max value.
create event
hspd=0; // this must be initialized or you will get an error accl=1; // the ammount per step to increment speed by until maximum is reached
movement code, change your hspd= line to the one below
hspd=min(spd, hspd+accl); hspeed=(left+right)*hspd;
min(val1,val2,etc..) is a math function that will find the minimum value of all the values provided to it. In this case we are providing it the maxium speed and the current speed with some acceleration added to it. Which ever value is smaller than the two, hspd will become.
example
hspd=0 -> accl=1
  • step 1 -> right key is pressed
    hspd=(1)*min(5,0+1) -> hspd=1
  • step 2 -> right key is pressed
    hspd=(1)*min(5,.1+1) -> hspd=2
  • step 3-5 -> the same thing hspd=3,4 and 5 respectively
  • step 6 -> right key pressed
    hspd=min(5,5+1) -> hspd=5 <-- see maxium speed

This works well for acceleration but how could I have it decelerate, so that when the key is released the character slowly stops moving, or do I need to use physics for that?
Last edited by Jeb; Dec 9, 2016 @ 12:37pm
The Winter Bud Dec 9, 2016 @ 12:40pm 
your second question's solution is to add friction to the object
friction=1; // how many pixels per step to make hspeed/vspeed come to zero

EDIT:
check if hdir==0 if so set friction to positive value, else set friction to zero
Last edited by The Winter Bud; Dec 9, 2016 @ 12:42pm
Jeb Dec 9, 2016 @ 12:48pm 
sorry but how do I add it, I tried typing
friction = 1; into the create block and step block but neither added any decceleration :/
The Winter Bud Dec 9, 2016 @ 12:57pm 
please re-read original post. I have changed the code as the heading says. My 2nd posts says how to impliment it
Here is how to solve the friction process
hdir=(left+right); // get the key dir if(hdir==0){ // if player is not pressing any buttons(EDIT: THIS LINE WAS WRONG) hspd=hspeed; // EDIT --> added this line to update correct hspd friction=accl; // apply friction }else{ // otherwise friction=0; // turn off friction hspd=median(-spd,spd, hspd+hdir*accl); // calculate the speed hspeed=hspd; // set the speed }
Last edited by The Winter Bud; Dec 9, 2016 @ 1:28pm
Jeb Dec 9, 2016 @ 1:14pm 
I just can't get this to work for me at the moment so I'm going to stick to having no acceleration for now.
Thanks for your help though I appreciate it :)
The Winter Bud Dec 9, 2016 @ 1:27pm 
don't give up friend. You'll get it. The basic construction is
assign key presses left and right which horizontal direction are you going (-1 left, 0 none, 1 right) if(horizontal direction ==none){ apply friction update hspd variable }else{ turn off friction get the new hspd set hspeed to hspd }
Last edited by The Winter Bud; Dec 9, 2016 @ 1:27pm
The Winter Bud Dec 9, 2016 @ 1:30pm 
You'l probably get a lot more if you re-read my posts. I do tend to Edit them and re edit them as my thought(s) form. I wish they came complete as I thought them, but alas, too fogged down from age I guess. The solution is present you just have to re-read to understand. :P
Jeb Dec 9, 2016 @ 1:32pm 
I did re-read the previous posts but couldn't get it to work, mabye I'm missing something or just can't think today :/
The Winter Bud Dec 9, 2016 @ 1:37pm 
computers are finicky. They do exactly what you tell them to and never what you want them to. :P

It's just part of being a programmer. Learning the code is the easy part, learning how to put code together so the computer does exactly what you want it to when you want it to do it? Well my friend, that is a life time job LOL.

Sometimes older code can conflict with things making it hard to understand what goes wrong. Learning to use the debugger can help a lot. Especially with the function
show_debug_message("tell me something")
that has saved me tons of hair that I didn't need to pull out. But again, it might mean you need to start the script over. Save the pieces you know that work then go from there. Think about the outline I gave you and try to find a solution that fits your moving game mechanic.
Jeb Dec 9, 2016 @ 1:44pm 
Matter of fact I have been working on this project for a while, with several previous versions, it has slowly developed from a bug riddled game where you would stick to walls, to now having pixel perfect collisions. The next step I wanted to take was fine tuning player movement a bit more which I just can't get to work in game and in head :L
I also sometimes type in wrong bits of code which are ment for different programming languages, which confuses me even more xD
But hey I'll get there soon.

Thanks again for your help and support :D
Jeb Dec 9, 2016 @ 1:45pm 
Also I'll take your advice with getting a hang of the debugger.
The Winter Bud Dec 9, 2016 @ 1:54pm 
Did some testing myself to see if I could see an issue, and mostly what I saw are the values for accl are too high and the value for friction when no key is pressed is set to high as well.
try changing the accl value to something smaller than 1
and then try changing the friction value where we say -> friction=1 <- in the step event? to a value smaller than the value you have accl.
example
Information about object: obj_walker
Sprite: sprWalker
Solid: false
Visible: true
Depth: 0
Persistent: false
Parent:
Children:
Mask:

No Physics Object
Create Event:

execute code:
/// initialize movement mechanics spd=16; // maxium speed hspd=0; // current direction player is moving (-1 left, 0 none, 1 right) accl=.5; // the ammount per step to increment speed by until maximum is reached hdir=0; // current horizontal direction player is moving fric=.25; // how fast to come to a hault

Step Event:

execute code:
/// update movement mechanics left=-keyboard_check(vk_left); right=keyboard_check(vk_right); hdir=(left+right); // get the key dir if(hdir==0){ // if player is not pressing any buttons hspd=hspeed; // update hspd so it matches when player is moving friction=fric; // apply friction }else{ // otherwise friction=0; // turn off friction hspd=median(-spd,spd, hspd+hdir*accl); // calculate the speed hspeed=hspd; // set the speed }

Draw GUI Event:

execute code:
///DEBUG - draw hspeed value to GUI draw_text(bbox_left,bbox_top-30,"hspeed: "+string(hspeed));
Last edited by The Winter Bud; Dec 9, 2016 @ 2:28pm
Jeb Dec 9, 2016 @ 2:21pm 
This works!
Thank you for helping me out, it was really kind of you.
I see what you mean by the values being too large, though I don't understand how the median function works :P (well I know what a median is but I don't know why it is used). So I think I'm going to mess with it for a while to get used to what it can be used for.

I won't forget you helping me like this dude ty alot ;u;
The Winter Bud Dec 9, 2016 @ 2:38pm 
the median value is kind of explained in post #2

the function itself returns the value that is in the middle of all the values you give it
so let's say spd=5
that means -spd would be -5
let's say hspd = 2
if we check for the median value of all three of those
value=median(spd,-spd,hspd)
then it would return the value that it is in the middle or in between the largest or smallest
-5, 2, 5 -> which turns out to be (2)

the way we are using it in the hspd function is we are keeping 2 values the same (maximum speed going left (-5) and maxium speed going right (5), and changing one value (hspd) what ever is the middle value between those three will be what hspd will be. hspd will never go left faster than -5, nor will it go faster than 5 to the right
lets say that hspd+hdir*accl came out to be -6? let's see it
median(-5,5,-6) what is the middle value of -6,-5, and 5? it's -5 so it's a way to maximize a range with a dynamic variable. It's kind of like a volume knob. The knob set's how loud you can hear something but the median() function limits the knob to a minimum(which in our case is the maxium negative speed or left side speed) and a maximum (which is our maxium right side speed)
< >
Showing 1-15 of 17 comments
Per page: 1530 50

Date Posted: Dec 9, 2016 @ 12:00pm
Posts: 17