Skip to content

Instantly share code, notes, and snippets.

@GeertVL-zz
Created July 28, 2013 14:59
Show Gist options
  • Save GeertVL-zz/6098877 to your computer and use it in GitHub Desktop.
Save GeertVL-zz/6098877 to your computer and use it in GitHub Desktop.
Game math
Point p = new Point ();
p.X = 3;
p.Y = 4;
Point i = new Point ();
i.X = 1;
i.Y = 2;
Vector v = i - p;
Vector normalized = v.Normalized();
Console.WriteLine ("Result: (" + normalized.X + ", " + normalized.Y + ", " + normalized.Length + ")\n");
using System;
namespace MacRocketFly
{
public class Point
{
public float X {
get;
set;
}
public float Y {
get;
set;
}
public Point ()
{
}
public Point AddVector(Vector v)
{
Point p2 = new Point();
p2.X = this.X + v.X;
p2.Y = this.Y + v.Y;
return p2;
}
public static Vector operator -(Point a, Point b)
{
Vector v = new Vector();
v.X = a.X - b.X;
v.Y = a.Y - b.Y;
return v;
}
}
}
using System;
namespace MacRocketFly
{
public class Vector
{
public Vector ()
{
}
public Vector (float x, float y)
{
this.X = x;
this.Y = y;
}
public float X {
get;
set;
}
public float Y {
get;
set;
}
public double Length
{
get
{
return Math.Sqrt (X * X + Y * Y);
}
}
public float LengthSqr
{
get
{
return (X * X + Y * Y);
}
}
public Vector SpeedUp(float s)
{
Vector scaled = new Vector();
scaled.X = this.X * s;
scaled.Y = this.Y * s;
return scaled;
}
public Vector SlowDown(float s)
{
Vector scaled = new Vector();
scaled.X = this.X / s;
scaled.Y = this.Y / s;
return scaled;
}
public Vector Normalized()
{
return new Vector (((float)X / this.Length), ((float)Y / this.Length));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment