# How does friction work?

13 replies to this topic

### #1 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 11:49 AM

So like, how would you program friction in GML, with out using the actual variable friction?

I would like to be able to do it myself, so I can also customize it a little myself.
• 0

- Olivebates

### #2 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 02 July 2011 - 11:56 AM

Well, GM internally does...
`speed=median(speed,0,speed+(sign(speed)*friction));`
Every step, so replicating it is easy (well, I just did).

-MoK

EDIT: Opps.. GM makes speed go towards 0. Fixed code.

Edited by MasterOfKings, 02 July 2011 - 12:01 PM.

• 1

Did I help you? If so, help me by pressing the + button above here.

### #3 Guest_ihato_*

Guest_ihato_*
• Guests

Posted 02 July 2011 - 11:57 AM

Maybe like this:
```var fric;
fric=0.2
if not (speed<=0) speed-=fric
```
But i think this is the real source of the friction function:
```hfriction = cos(direction*pi/180)*friction;
vfriction = -sin(direction*pi/180)*friction;
hspeed -= min(abs(hfriction),abs(hspeed)) * sign(hspeed) * sign(hfriction);
vspeed -= min(abs(vfriction),abs(vspeed)) * sign(vspeed) * sign(vfriction);
x+=hspeed;
y+=vspeed;
```

EDIT: Ninja'd!

Edited by ihato, 02 July 2011 - 01:01 PM.

### #4 chaz13

chaz13

GMC Member

• GMC Member
• 4214 posts
• Version:Unknown

Posted 02 July 2011 - 11:58 AM

Something like

`speed+=((speed<0)*fric)-((speed>0)*fric)`

• 1

### #5 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 12:50 PM

Oh wow, those look like some complex algorythems. I guess thats why Mark put it all into the variable friction xD

Thanks
• 0

- Olivebates

### #6 Guest_ihato_*

Guest_ihato_*
• Guests

Posted 02 July 2011 - 01:02 PM

degree of complexity (0-10) = 2 (i think)

Edited by ihato, 02 July 2011 - 01:03 PM.

### #7 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 01:04 PM

Really? I would like to see a 10 then.

eh, I think my brain will explode if I try to understand it xD
• 0

- Olivebates

### #8 Guest_ihato_*

Guest_ihato_*
• Guests

Posted 02 July 2011 - 01:12 PM

No, it is easy, look:
min functions returns the smallest argument
for ex: min(1,2,3,4,5,6) is 1

median function returns the value that is nearest to the avrage value.
for ex:
median(1,2,3,4,5,6)
the avrage value (main(1,2,3,4,5,6)) is: (1+2+3+4+5+6)/6 = 3.5 and the median should then be the 3 and 4 (ie. avrage value of 3 and 4), but because the avrage value is not the integer, the GM takes the smaller value for the median. (3)

sign function just returns -1,0, or 1.
If the value is negative, then sign will return -1
If the value is positive, then sign will return 1
If the value is 0, then sign will return 0
for ex: sign(-432.2)=-1

Edited by ihato, 02 July 2011 - 01:15 PM.

### #9 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 01:20 PM

Aaaah.

And so, how about cos and sin?
• 0

- Olivebates

### #10 MasterOfKings

MasterOfKings

The True Master

• GMC Member
• 4888 posts
• Version:GM8

Posted 02 July 2011 - 01:24 PM

cos (cosine) and sin (sine) relate to trigonometry (along with tan). Google those.

-MoK
• 0

Did I help you? If so, help me by pressing the + button above here.

### #11 YellowAfterlife

YellowAfterlife

GMC Member

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

Posted 02 July 2011 - 01:28 PM

A common (and I believe, better) way to simulate friction is:
```if speed != 0 then speed = sign(speed) * max(0, abs(speed) - friction)
/*
if speed is not equal to zero,
set it to sign of speed (-1 or +1),
multiplied by absolute (positive)
value of speed, 'influenced' by friction
and limited to not go below zero.
*/
```
Notice that friction can be negative in GameMaker, causing acceleration if object is not 'static'.
GameMaker keeps hspeed+vspeed & speed+direction variables in sync automatically - when you change variable from one group, variables in other group are adjusted accordingly.

Other code suggestions in this topic are more or less incorrect.
Edit: some of them are also un-effective.

Edited by YellowAfterlife, 02 July 2011 - 01:33 PM.

• 1
If my posts contain broken links, try looking around my website. I gradually make blog posts for any examples I make.

### #12 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 01:39 PM

@YellowAfterlife
Sweeet B)

@MoK
I have been looking in wikipedia and trying to understand, but i dont understand xD
Edit: But I use it often, like this:
```aa+=0.1
image_angle = sin(aa) * 25
```
Which makes like a swaying rotation motion

Edited by olivebates, 02 July 2011 - 01:41 PM.

• 0

- Olivebates

### #13 Guest_ihato_*

Guest_ihato_*
• Guests

Posted 02 July 2011 - 01:48 PM

I have been looking in wikipedia and trying to understand, but i dont understand xD
Edit: But I use it often, like this:

```aa+=0.1
image_angle = sin(aa) * 25
```
Which makes like a swaying rotation motion

The angles are in radians, so you have to convert them into degrees with proportion system (angle*pi/180 )
The "point_direction(center_of_the_circle.x,center_of_the_circle.y,dot_on_the_circle.x,dot_on_the_circle.y)" is the direction.

### #14 olivebates

olivebates

GMC Member

• GMC Member
• 526 posts
• Version:GM8

Posted 02 July 2011 - 02:09 PM

Sweet, I understand it much better now.

Thanks
• 0

- Olivebates