0
\$\begingroup\$

I am working on a 2D ship that will fly around in space (for now, no friction or gravity). The controls are similar to the game Asteroids. My problem is that I am having trouble connecting some variables to real life applications.

For example, if I'm defining the ships velocity, which in real physics is V=distance traveled (or change in position)/time. Should velocity be a Vector2 or just a float (or int). It's how far an object moves so I would think it to be just a single integer or number but then again, your moving on an X and Y axis so shouldn't there be two velocities? One for the X and another for the Y? The velocity being a vector2 actually works really well but when I'm doing other formulas, which are floats or ints, that require velocity I run into issues because I don't have a single number then.

Another issue i'm having trouble grasping is the direction. My code for the direction is:

shipVariables.shipDirection = new Vector2((float)Math.Sin(shipVariables.shipRotation), -(float)Math.Cos(shipVariables.shipRotation));

Which works but it will immediately turns the ship if you rotate the object while moving "forward". My most successful approach to this method was doing this:

shipVariables.shipDirection += new Vector2((float)Math.Sin(shipVariables.shipRotation), -(float)Math.Cos(shipVariables.shipRotation));

The effect I like is that instead of just turning, it essentially has to overcome the previous direction to move to the next. The part I don't like is that it gains speed way to much and it's supposed to just be a direction, not a direction and magnitude.

If I have the velocity and I can get the direction, I will be able to calculate everything else with other formulas that I know (momentum, acceleration, etc...).

Any tips to lead me in the right directions would be great, thanks.

\$\endgroup\$
  • 1
    \$\begingroup\$ Have you taken any of Vector Algebra, Calculus, or Mechanics (Physics) at a first year university level? I believe you will need at least these three subjects in order to build your game. \$\endgroup\$ – Pieter Geerkens Dec 14 '13 at 7:06
  • \$\begingroup\$ @PieterGeerkens I am currently in Physics and Calculus. I understand physics and Vector Algebra very well, I just can't seem to grasp putting physics in a 2D dimension. We have never (yet) used Physics in a 2D level either yet, this game is just for fun. \$\endgroup\$ – TrevorPeyton Dec 14 '13 at 17:25
  • \$\begingroup\$ My guess is you are still a term or two away from really being able to tackle this material with confidence. Here are two thoughts for consideration: 1) Work on non-technical aspects of your game for a bit, until your studies catch up; or 2) with the Christmas break upon us, devote 1 week of it to advancing your knowledge in Vector Algebra. That is, get a Vector Algebra text and do 1 weeks' worth of material each day for 5 or 6 days. That half-term of material should be enough to really advance your ability to work on the game; and will help your grades also. Bonus! \$\endgroup\$ – Pieter Geerkens Dec 14 '13 at 18:06
  • \$\begingroup\$ If you have access to a copy of Feynman Lectures on Physics, Volume 1, you could do worse than to browse a few chapters of that. \$\endgroup\$ – Pieter Geerkens Dec 14 '13 at 18:09
  • \$\begingroup\$ @PieterGeerkens Alright, thanks for all the input. I plan on looking over most of that over break (I do have access to Feynman Lectures on Physics, Volume 1). \$\endgroup\$ – TrevorPeyton Dec 14 '13 at 18:29
2
\$\begingroup\$

Physics for simple points in 2D is basically the same as doing 1D physics twice - once for each dimension (X and Y). So you can use the same formulas you would use on floats on Vector2s and it generally "just works".

But if you add rotation, you must calculate a 1D angular "dimension" as well. (It's more of a "degree of freedom".)

So start with your variables:

float angle;
// You could have a "float angularVelocity" here if you wanted
Vector2 position;
Vector2 velocity;

And then update something like this:

if(leftKey)
    angle -= rotateAmount;
if(rightKey)
    angle += rotateAmount;

// Note: A proper cartesian coordinate system with "Y+ = up" would be (cos, sin).
//       But I'll just use what you have:
Vector2 direction = new Vector2((float)Math.Sin(angle), -(float)Math.Cos(angle));

float thrust = thrustKey ? 1f : 0f;

velocity += direction * thrust * time;
position += velocity * time;

Finally: Generally don't use int for physics. It is for integer (whole) values, which makes it difficult to get the desired results. A Vector2 is made up of two floats.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.