Search Home Members Contacts
About Us
Products
Downloads
Community
Support
Pages: [1]
  Print  
Author Topic: My Math-Fu is weak and my ship speed is whacked  (Read 430 times)
Arakiel
Community Member
*
Posts: 26


« on: June 09, 2008, 09:37:57 PM »

Evening all.  I've got a piece of code that I'm hoping someone more clever then me could take a look at and point me in the direction of my mistake.  What I have here is some simple (as in, not realistic) movement code for my "space ship".  The problem is, while it works well on one computer with a fairly terrible video card (50-60fps) it is uncontrollable and movement is far too fast on a machine with a good card (1800fps).  I hit the acceleration and move half way across the universe in a flash and when I try to rotate the ship is spins around like mad.

First is some of the variables being used for reference:
Code:
    //ship rotation variables
    float _angleX;
    float _angleY;
    float _angleZ;

    //movement variables
    float _acceleration = 2.0f;
    float _deceleration = 1.0f;
    float _turnRatio = 5.0f;
    float _maxSpeed = 40.0f;
    float _speed = 0.0f;

This is the function handling input:
Code:
    private void ProcessInput()
    {
      // Check if we pressed the Up key, if so, then we are accelerating
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_UP))
        _speed += (_acceleration / (float)TV.AccurateTimeElapsed());

      // Check if we pressed the Down key, if so, then we are decelerating
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_DOWN))
        _speed += -(_deceleration / (float)TV.AccurateTimeElapsed());
     
      // Check if we pressed the A key, if so, then turn to the left
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_A))
        _angleY += -(_turnRatio / (float)TV.AccurateTimeElapsed());
      // Check if we pressed the D key, if so, then turn to the right
      else if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_D))
        _angleY += (_turnRatio / (float)TV.AccurateTimeElapsed());

      // Check if we pressed the W key, if so, then rotate down
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_W))
        _angleX += (_turnRatio / (float)TV.AccurateTimeElapsed());
      //Check if we pressed the S key, if so, rotate up
      else if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_S))
        _angleX += -(_turnRatio / (float)TV.AccurateTimeElapsed());

      // Check if we pressed the Q key, if so, then rotate the ship counter clockwise
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_Q))
        _angleZ += (_turnRatio / (float)TV.AccurateTimeElapsed());
      // Check if we pressed the E key, if so, then rotate the ship clockwise
      else if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_E))
        _angleZ += -(_turnRatio / (float)TV.AccurateTimeElapsed());

      // Check if we pressed the Backspace key, if so, cut all speed
      // TODO: this is hacky, should slow down not full stop, but good enough for now
      if (Input.IsKeyPressed(CONST_TV_KEY.TV_KEY_BACKSPACE))
        _speed = 0;

    }

Lastly here is the function which updates the ship position:
Code:
    private void ProcessMovement()
    {
      if (_speed > _maxSpeed)
        _speed = _maxSpeed;

      // From the angle variable, we update the ship rotation
      PlayerShip.SetRotation(_angleX, _angleY, _angleZ);

      PlayerShip.MoveRelative(_speed, 0.0f, 0.0f);

      Scene.GetCamera().ChaseCamera(PlayerShip, new TV_3DVECTOR(0.0f, 200.0f, -500.0f), new TV_3DVECTOR(0.0f, 0.0f, 0.0f), 10);
    }

I assumed at first that it had to do with me not accounting for elapsed time, but even after adding that in the movement on the low end machine is fine and the high end machine is unusable Sad  I missed something I'm just not sure what.  Any help would be appreciated.

-Arakiel
Logged
serial
Customers
Community Member
*****
Posts: 299


« Reply #1 on: June 09, 2008, 10:35:37 PM »

Thats easy you aren't using timeElapsed Correctly


You are only incrementing your speed by the same amount per second on
both machines but the machine with the high FPS is still going to move
your ship fast the way its coded.

You need to use   _speed / Timeelapsed
Logged
Arakiel
Community Member
*
Posts: 26


« Reply #2 on: June 10, 2008, 07:00:08 AM »

I'm afraid I'm still confused.  Sad

In the ProcessInput function I'm already adding to speed based upon time using speed += (_acceleration / TV.AccurateTimeElapsed());

So it should only add an appropriate amount to speed for the machine it is running on yes?

I then apply the current speed to the mesh with MoveRelative.

Figuring that I might better understand what you meant by just doing it, I added the same "/TimeElapsed" math to my MoveRelative call and (after handling divide by 0 errors on initialization) the ships speed was so low at that point that it essentially wasn't moving on the low end machine.

Could you explain a little more what you are saying?  Thank you!

-Arakiel
Logged
Arakiel
Community Member
*
Posts: 26


« Reply #3 on: June 10, 2008, 07:37:43 AM »

Never mind I think I've got it.

I found a little article here: http://gamedesign.wikidot.com/brutus2d:timing-tutorial

I think my mistake was that I was dividing by time instead of multiplying.  When I switched to multiplication by time when calling MoveRelative and removed the division by time during process input, both machines appear to move forward at the same rate.  Far faster then I intend but I can adjust that later I think...or just make my universe bigger LOL

Thanks for the help.

-Arakiel
Logged
Lordtek
Customers
Community Member
*****
Posts: 35


« Reply #4 on: June 12, 2008, 12:09:28 AM »

Yeah I was about to say that... never ever divide if you can avoid it. float point errors, division by zero crashes and then theres compilation and runtime crap that happens when your user A has a celeron in Europe, user B has an AMD in the US and user C has a modified Pentium from China. It will get ugly debugging something so simple especially when you start throwing  data directly to the GPU.
Logged

common sense isn't common anymore
Pages: [1]
  Print  
 
Jump to:  

Powered by SMF 1.1.3 | SMF © 2006-2007, Simple Machines LLC
Seo4Smf v0.2 © Webmaster's Talks