Eye Into The World

Tag: development

A few 2D game algorithms

by Ambiguous on May.24, 2008, under Programming

Some simple C# 2D game algorithms I made to make the creation of 2D games easier. A few are untested, and my I might have made a mistake in the formulas. If you find anything, just let me know.

Static speed - calculates a speed so that all objects with the same baseSpeed travel at a common speed, regardless of their direction vector

public static float StaticSpeed(Vector2 direction, float baseSpeed)
{
return baseSpeed / (float)System.Math.Sqrt((direction.X * direction.X) + (direction.Y * direction.Y));
}

public static float StaticSpeed(Vector2 position, Vector2 destination , float baseSpeed)
{
return baseSpeed / (float)System.Math.Sqrt(System.Math.Pow(destination.X - position.X, 2) + System.Math.Pow(destination.Y - position.Y, 2));
}

public static void StaticSpeed(ref Vector2 direction, ref float baseSpeed, out float result)
{
result = baseSpeed / (float)System.Math.Sqrt((direction.X * direction.X) + (direction.Y * direction.Y));
}

UpdateMove - calculates a new position for the object based on its position, direction vector, and the calculated staticSpeed

public static Vector2 UpdateMove(Vector2 position, Vector2 direction, float staticSpeed)
{
Vector2 result = position;
result.X += direction.X * staticSpeed;
result.Y += direction.Y * staticSpeed;
return result;
}

public static void UpdateMove(ref Vector2 position, ref Vector2 direction, ref float staticSpeed, out Vector2 result)
{
result = new Vector2(position.X + direction.X * staticSpeed, position.Y + direction.Y * staticSpeed);
}

DirectionalVector - converts a coordinate vector into a directional vector

public static Vector2 DirectionalVector(Vector2 a, Vector2 b)
{
return new Vector2(b.X - a.X, b.Y - a.Y);
}

public static void DirectionalVector(ref Vector2 a, ref Vector2 b, out Vector2 result)
{
result.X = b.X - a.X;
result.Y = b.Y - a.Y;
}

public static void DirectionalVector(ref Vector2 a, ref Vector2 b, out float r_X, out float r_Y)
{
r_X = b.X - a.X;
r_Y = b.Y - a.Y;
}

Perpendicular - gets the perpendicular vector of a given vector

public static Vector2 Perpendicular(Vector2 a)
{
return new Vector2(a.Y * -1, a.X);
}

public static Vector2 Perpendicular(Vector2 a, Vector2 b)
{
return new Vector2((b.Y - a.Y) * -1, b.X - a.X);
}

Midpoint - gets the midpoint points a and b

public static Vector2 Midpoint(Vector2 a, Vector2 b)
{
return new Vector2((a.X - b.X) / 2, (a.Y - b.Y) / 2);
}

Reflect - takes a direction vector and reflects it based on the normal (basically, like a ball heading in the direction vector bouncing off the normal vector)

public static Vector2 Reflect(Vector2 vector, Vector2 normal)
{
Vector2 result;
float a = (vector.X * normal.X) + (vector.Y * normal.Y);
result.X = vector.X - ((2f * a) * normal.X);
result.Y = vector.Y - ((2f * a) * normal.Y);
return result;
}

public static void Reflect(ref Vector2 vector, ref Vector2 normal, out Vector2 result)
{
float a = (vector.X * normal.X) + (vector.Y * normal.Y);
result.X = vector.X - ((2f * a) * normal.X);
result.Y = vector.Y - ((2f * a) * normal.Y);
}

If you find any bugs, just leave a comment.

If you enjoyed this post, please subscribe for more!

© G 2the R/Th!rdeye - visit the Eye Into The World for more great content!

8 Comments :, , , , , , , , , , , , , , , more...

Looking for something?

Use the form below to search the site:

Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Visit our friends!

A few highly recommended friends...