2 Replies - 346 Views - Last Post: 02 July 2017 - 10:45 AM Rate Topic: -----

#1 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Running traction / sliding

Posted 02 July 2017 - 03:49 AM

Hello.

I've been working on a platform game and not doing too bad, I kinda have something that resembles what could become a platformer. Although most of what I have doesn't work exactly how I would like and needs some refinement. Hence the reasons behind this post.

Think of Mario, in my platform game when I press the key (Right) my character instantly runs in the direction I want. When I release the key he stops. But it doesn't feel quite right. When Mario runs he takes a bit of time getting to top speed. He doesn't come to an instant stop either. I think this might be called traction, or slippy floors... At least that's what I've been trying to google anyway.

Can someone point me in the right direction of a good example? (Preferred: C# && XNA || Monogame but anything readable should do)

I thought that a while loop might work;

Here's a bit of sudocode of trying to come to a stop or something to that effect.


float traction = 0.01f;

if (Key Released)
{
	while (velocity.X > 0)
	{
		velocity.X = velocity.X * traction;
	}
	if (velocity.X < 1)
		velocity.X = 0;
}



But then having a while loop freezes the game of course.

I then thought about would it work if I make it, it's own thread??

I have never done multi-threading before, although it doesn't look too complicated. I just don't know how multi threading works as I've never tried it. Or would it even work within the game loop itself. Might cause some headaches when a better option is available, but my head has that idea stuck in there blocking anything else at the moment.

Thanks for reading,

Joe

Is This A Good Question/Topic? 0
  • +

Replies To: Running traction / sliding

#2 modi123_1  Icon User is offline

  • Suitor #2
  • member icon



Reputation: 13493
  • View blog
  • Posts: 53,896
  • Joined: 12-June 08

Re: Running traction / sliding

Posted 02 July 2017 - 09:13 AM

Why wouldn't that logic be in the player's class update which is triggered by the game engine update?

So ditch the while loop for the game loop.
Was This Post Helpful? 1
  • +
  • -

#3 eazyyzae  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 23
  • Joined: 23-February 13

Re: Running traction / sliding

Posted 02 July 2017 - 10:45 AM

View Postmodi123_1, on 02 July 2017 - 09:13 AM, said:

Why wouldn't that logic be in the player's class update which is triggered by the game engine update?

So ditch the while loop for the game loop.


You are right, I think I couldn't see the woods for the trees so to speak.

I was using Key.Released instead of !KeyDown
I think Key.Released might only run once and that was making me think it needed a loop of some sort.

I had to add a direction or I was getting strange results
if (input.KeyDown(Keys.Right))
            {
                direction = 3;
                moveAnimation.IsActive = true;
                moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 0);
                velocity.X = moveSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;           
            }
            else if (!input.KeyDown(Keys.Right) && direction == 3)
            {
                moveAnimation.IsActive = false;
                if(velocity.X > 0.1)
                    velocity.X = velocity.X -traction;
                else velocity.X = 0;

                // just for debuging
                Console.WriteLine("velocity right = " + velocity.X);
            }

            if (input.KeyDown(Keys.Left))
            {
                direction = 2;
                moveAnimation.IsActive = true;
                moveAnimation.CurrentFrame = new Vector2(moveAnimation.CurrentFrame.X, 2);
                velocity.X = (-moveSpeed) * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }       
            else if (!input.KeyDown(Keys.Left) && direction == 2)
            {
                moveAnimation.IsActive = false;
                if (velocity.X  < -0.1)
                    velocity.X = velocity.X +traction;
                else velocity.X = 0;

                // just for debuging
                Console.WriteLine("velocity left = " + velocity.X);
            }




Console Output I used for debuging

velocity left = -3.23334
velocity left = -3.13334
velocity left = -3.03334
velocity left = -2.93334
velocity left = -2.83334
velocity left = -2.733341
velocity left = -2.633341
velocity left = -2.533341
velocity left = -2.433341
velocity left = -2.333341
velocity left = -2.233341
velocity left = -2.133341
velocity left = -2.033341
velocity left = -1.933341
velocity left = -1.833341
velocity left = -1.733341
velocity left = -1.633341
velocity left = -1.533341
velocity left = -1.433341
velocity left = -1.333341
velocity left = -1.233341
velocity left = -1.133341
velocity left = -1.033341
velocity left = -0.9333409
velocity left = -0.8333409
velocity left = -0.7333409
velocity left = -0.6333408
velocity left = -0.5333408
velocity left = -0.4333408
velocity left = -0.3333408
velocity left = -0.2333408
velocity left = -0.1333408
velocity left = -0.03334083
velocity left = 0

velocity right = 3.23334
velocity right = 3.13334
velocity right = 3.03334
velocity right = 2.93334
velocity right = 2.83334
velocity right = 2.733341
velocity right = 2.633341
velocity right = 2.533341
velocity right = 2.433341
velocity right = 2.333341
velocity right = 2.233341
velocity right = 2.133341
velocity right = 2.033341
velocity right = 1.933341
velocity right = 1.833341
velocity right = 1.733341
velocity right = 1.633341
velocity right = 1.533341
velocity right = 1.433341
velocity right = 1.333341
velocity right = 1.233341
velocity right = 1.133341
velocity right = 1.033341
velocity right = 0.9333409
velocity right = 0.8333409
velocity right = 0.7333409
velocity right = 0.6333408
velocity right = 0.5333408
velocity right = 0.4333408
velocity right = 0.3333408
velocity right = 0.2333408
velocity right = 0.1333408
velocity right = 0.03334083

Thanks :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1