This project is read-only.

Calculating Movement

Positions and movement in the GdiGaming API are handled by Vector2 structures. The Vector2 is like a point, specifying an X and Y value, but it uses System.Single for the underlying values and it also offers a number of helper methods for performing calculations.

The Vector2 structure provides a number of helper methods to assist with calculating the positions and movement of GameObjects. It is often helpful to know what direction a target lies in, or to measure distance at an angle. The following information will assist with some of these objectives.

Distance and Speed

You can use Vector2.GetDistance (or the DistanceTo method on an instance of Vector2) to get the distance between two vectors. The result of this method will always be a positive value reflecting the distance between the two vectors.

When calculating how far to move an object based on its speed, it is often best to work in units of pixels-per-second. This is accomplished by taking the speed value for an object and multiplying it by the current GameTime.LastFrameTime value.


The Vector2.GetVectorToward method can be used to easily calculate a vector which is a certain distance toward a target, taking into account the angle between the two vectors. So it is fairly easy to move an object toward a target with code like:
myObject.Position = Vector2.GetVectorToward(myObject.Position, otherObject.Position, myObject.Speed * time.LastFrameTime)
Since this kind of code is typically performed in the OnUpdate method, time would come from e.GameTime. The Speed property would be defined on the custom GameObject myObject (GameObject does not contain a speed property by default).

Last edited Jan 2, 2012 at 3:59 AM by ReedKimble, version 1


No comments yet.