## Posts Tagged ‘math’

### Inertia fixed

After much thought and messing around, I realized that I needed to control the total velocity of the ship instead of trying to control the X and Y amounts separately.  So now I’m checking the velocity using Pythagoras’ handy theorem (a squared x b squared = c squared) and then scaling back the vector when it goes over the limit.

i still have some polishing to do, but here’s the code real quick…

double thrust = .27;

double velX = _playerVel.X;
double velY = _playerVel.Y;

double newX;
double newY;

double curRot = GetRotation();
pnl0.Text = “rot: ” + curRot;

Utils.CalculateMovement((int)curRot, thrust, out newX, out newY);

velX += newX;
velY += newY;

_playerVel = new Vector2D(velX, velY);

double veloc = Math.Sqrt(Math.Pow(_playerVel.X, 2) + Math.Pow(_playerVel.Y, 2));
pnl3.Text = “vel: ” + veloc;

double testVel = 20;

if (veloc > testVel)
{
double scale = testVel / veloc;
_playerVel.X = _playerVel.X * scale;
_playerVel.Y = _playerVel.Y * scale;
}

Now, fly around the galaxy yelling “Git some!  Git some!”  I’ll have something for you to shoot at pretty soon.