using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace ConsoleApplication3 { class vec { public double x; public double y; public vec(double x, double y) { this.x = x; this.y = y; } public vec sub(vec vec1) { return new vec(this.x - vec1.x, this.y - vec1.y); } public double magnitude() { return Math.Sqrt((this.x * this.x) + (this.y * this.y)); } public vec div(double scalar) { return new vec(this.x / scalar, this.y / scalar); } public static double dot(vec vec1, vec vec2) { double ret = (vec1.x * vec2.x) + (vec1.y * vec2.y) ; return ret; } } class Program { static void Main(string[] args) { vec ball1pos = new vec(110,90); vec ball2pos = new vec(100,100); vec difference = ball2pos.sub(ball1pos); double distance = difference.magnitude(); vec normal = difference.div(distance); // unit vector of pos delta vec ball1vel = new vec(0, 2); vec ball2vel = new vec(0,-3); vec velocityDelta = ball2vel.sub(ball1vel); double velmag = velocityDelta.magnitude(); vec velUnit = velocityDelta.div(velmag); // unit vector of vel delta double dot = vec.dot(velUnit, normal); // dot product of unit vectors System.Console.Write(dot); } } }