Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / [LOCKED] timer based friction

Author
Message
bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 25th Apr 2008 06:42
I'm trying to figure out how to setup friction when an object moves using timer based movement. I've done some searching but can't find a proper formula. Here's what I have:



Of course, this doesn't take into account the "elapsedTime#" variable. Tacking on 0.995*elapsedTime# doesn't work.

For example, .995 * 1ms = 0.000995, which basically stops the player.

If elapsedTime# is 1 second and I bump up .995 * 60FPS (target frame rate) so that it works at the 1ms speed, when the framerate slows down it works out to be accelerating the player, rather than slowing him down.

So I need some sort of formula that slows down the player at the same speed no matter the frame rate.

Thanks!


Hurray for teh logd!
tiresius
22
Years of Service
User Offline
Joined: 13th Nov 2002
Location: MA USA
Posted: 25th Apr 2008 06:49
You're doing something similar to curvevalue, which is very difficult to predict values with it based on time. I'm not very good at math but I haven't heard anybody come up with an easy formula for doing what you want to do. I have done some tricks with inverse FPS (1/fps) and such stuff but it's not great.

In physics simulations we do what is called "time slicing". It is where you write your code so it guarantees that the calculations will be done 60, 100 or more times per second. This means that if the framerate is slow (like 20 fps), it will do 5 calculations between each sync. If the framerate is fast then it will do about 1.5 calculations between each sync. Either way, your object is slowing down at the same speed regardless of the sync.

Hope this helps. Search on curvevalue and you should find a post about it...

I'm not a real programmer but I play one with DBPro!
bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 25th Apr 2008 18:20 Edited at: 25th Apr 2008 18:22
well, curvevalue may be able to give more predictable results if I place a low or high value into that box (for example, if I want the ship to slow 1 unit a second, all I'd have to put is 1/elapsedTime#. However, it won't be 1 unit a second, it will be somewhere in that neighborhood, and on some sort of curve. This isn't practical for my application, as I'm really kind of in need of a formulaic solution to this issue where I'm able to control this aspect.


Hurray for teh logd!
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 26th Apr 2008 02:28
I know exactly the problem you have here - I've encountered it before, and remember trying out several solutions. Off the top of my head I can't remember which one worked the best, but here's the thread which you might find useful (if you've got your maths head screwed on!): http://forum.thegamecreators.com/?m=forum_view&t=44948&b=1

bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 26th Apr 2008 04:34
Wow, that was way beyond me... both methods :/

How can I integrate that into something, like say asteroids?


Hurray for teh logd!
Ric
20
Years of Service
User Offline
Joined: 11th Jul 2004
Location: object position x
Posted: 26th Apr 2008 11:53
Yeah - it got pretty deep. You could try this, which was one of the suggestions, and see if it works:

velocity = velocity * frictional constant ^ dt
position = position + velocity * dt

where dt is the time elapsed since the last loop. Note the ^, which means 'to the power of'. Test to see if it works by setting the sync rate to different values, and see if it gives the same kind of motion regardless.

calcyman
17
Years of Service
User Offline
Joined: 31st Aug 2007
Location: The Uncertainty Principle
Posted: 26th Apr 2008 12:42
It's very similar to exponential decay:

player(player).Xvel = player(player).Xvel * (0.995 ^ elapsedTime#)
player(player).Zvel = player(player).Zvel * (0.995 ^ elapsedTime#)

The optomist's right, The pessimist's right.
bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 27th Apr 2008 23:28
I calculated it out and it seems to do the job, I'll let you know once I run some real world tests if I need more help

Thanks!




Hurray for teh logd!
bitJericho
22
Years of Service
User Offline
Joined: 9th Oct 2002
Location: United States
Posted: 28th Apr 2008 05:49
Ok, so in testing it out, things are slower when you decrease the framerate, (changed from 85ish to 40, the object moves slightly slower).

Can someone hook me up with an example using the code provided in that thread Ric linked to?

I'd appreciate it a lot!


Hurray for teh logd!

Login to post a reply

Server time is: 2025-05-12 09:26:03
Your offset time is: 2025-05-12 09:26:03