Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [Solved] Player moves faster to the left :*(  (Read 3212 times)

0 Members and 1 Guest are viewing this topic.

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« on: January 19, 2011, 12:45:03 am »
I'm running into some problems with my fixed time step and calculating negative movement.

When the player pushes the left key mMovement = -1; right key mMovement = 1;

my problem is that when moving left "-1" the player goes faster then if they were going right "1";


main loop
Code: [Select]

float deltaTime = App.GetFrameTime();

mainPlayer.Update(deltaTime);
otherPlayer.Update(deltaTime);



player update
Code: [Select]

// Player pushes Left then mMovement is "-1" right mMovement is "1"

mVelocity.x += mMovement * 2000 * ElapsedTime;
mVelocity.x = clamp(mVelocity.x, -100, 100);

// Apply velocity.
mPosition.x += mVelocity.x * ElapsedTime;

mSprite.SetPosition((int)mPosition.x, 600);



0_o I'm sure it's something very simple, but I just can't seem to figure it out.

when I breakpoint it,  the velocity seems to be clamped at the proper positive or negative number so it's something with

Code: [Select]

mPosition.x += mVelocity.x * ElapsedTime;

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[Solved] Player moves faster to the left :*(
« Reply #1 on: January 19, 2011, 12:51:18 am »
Code: [Select]
mVelocity.x += mMovement * 2000 * ElapsedTimeShouldn't it be = instead of +=? Do you work with immediate movement or acceleration?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #2 on: January 19, 2011, 12:56:05 am »
if I remove the + I lose the ability to go right.

hrmm, but then left seems to be the proper speed :{

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[Solved] Player moves faster to the left :*(
« Reply #3 on: January 19, 2011, 01:00:28 am »
Quote from: "Pirate"
if I remove the + I lose the ability to go right.
No, you don't. You needn't change the velocity relatively to the former velocity (unless you work with acceleration).

The velocity (let's call it vx) is either positive or negative, depending on the sign of mMovement.
Then you write x += vx * dt; which adds the velocity to the position. If the velocity is positive, the object moves right; if negative, it moves left.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #4 on: January 19, 2011, 01:07:06 am »
I must of really messed something up somewhere then.
What you are saying makes perfect sense.

just for some reason It won't move right :{

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6287
  • Thor Developer
    • View Profile
    • Bromeon
[Solved] Player moves faster to the left :*(
« Reply #5 on: January 19, 2011, 01:08:56 am »
Reduce your code to a minimal and complete example, omitting all details irrelevant to the problem.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #6 on: January 19, 2011, 01:22:35 am »
changing mMovement from = 1; to = 2;  seemed to fix it.
I have no idea why.

But now left (-1) and right (2) have the same speeds :*(

I made this change after stripping everything down, and It still wouldn't go right.

and if I make changes to "2000" in the velocity formula it breaks right movement again.

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #7 on: January 19, 2011, 01:46:25 am »
Well I figured it out.


I was storing mVelocity.x and .y as integers :*( switched it to float and everything is all good now.

tntexplosivesltd

  • Full Member
  • ***
  • Posts: 163
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #8 on: January 19, 2011, 02:13:21 am »
So, the truncation of integers was your problem then? As in, if it was 2.5, it would be 2 as an int.

Pirate

  • Newbie
  • *
  • Posts: 6
    • View Profile
[Solved] Player moves faster to the left :*(
« Reply #9 on: January 19, 2011, 02:19:09 am »
yes since the ElapsedTime was a fraction It was messing up the formula.