*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.

Started by olivebates, Jul 02 2011 11:49 AM

13 replies to this topic

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.

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

- Olivebates

Posted 02 July 2011 - 11:56 AM

Well, GM internally does...

-MoK

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

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.**

Posted 02 July 2011 - 11:57 AM

Maybe like this:

EDIT: Ninja'd!

var fric; fric=0.2 if not (speed<=0) speed-=fricBut 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.**

Posted 02 July 2011 - 11:58 AM

Something like

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

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

Thanks

- Olivebates

Posted 02 July 2011 - 01:02 PM

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

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

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

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

- Olivebates

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

and, thats about it.

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

and, thats about it.

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

Posted 02 July 2011 - 01:20 PM

Aaaah.

And so, how about cos and sin?

And so, how about cos and sin?

- Olivebates

Posted 02 July 2011 - 01:28 PM

A common (and I believe, better) way to simulate friction is:

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.

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

Edit: some of them are also un-effective.

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

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

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:

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) * 25Which makes like a swaying rotation motion

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

- Olivebates

Posted 02 July 2011 - 01:48 PM

this image will help youI 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) * 25Which 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.

Posted 02 July 2011 - 02:09 PM

Sweet, I understand it much better now.

Thanks

Thanks

- Olivebates