Created
July 28, 2013 14:59
-
-
Save GeertVL-zz/6098877 to your computer and use it in GitHub Desktop.
Game math
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
http://www.youtube.com/watch?v=hh-3xLawoYo&list=PLW3Zl3wyJwWOpdhYedlD-yCB7WQoHf-My