void ApplyCollision(Collision collision) { model.player.CopyFrom(copyCircle); Vector c = model.player.center; Vector d; if (Collision.Type.Corner == collision.type) { d = Geom.GetNearestPoint(collision.line, model.player.center); } else { d = Geom.GetCrossPosition(c, collision.line); } /* view.SetDebugNormal(new Line(d, new Vector(c))); */ Vector normal = Geom.Normalize(Geom.Sub(c, d)); float dv = -2.0f * Geom.Dot(model.player.velocity, normal); Vector normalDV = Geom.Mul(normal, dv); Vector reflected = Geom.Sum(normalDV, model.player.velocity); model.player.velocity.y = reflected.y * FADE_Y_INDEX; model.player.velocity.x = reflected.x * FADE_X_INDEX; }
public static bool PointOnLine(Vector point, Line line) { Vector a, b; Vector ap = Geom.Sub(point, line.a); Vector ab = Geom.Sub(line.b, line.a); // colinear check if (Geom.Mul(ap, ab) != 0) { return(false); } // check x if (line.a.x < line.b.x) { a = line.a; b = line.b; } else { a = line.b; b = line.a; } if (point.x < a.x || point.x > b.x) { return(false); } // check y if (line.a.y < line.b.y) { a = line.a; b = line.b; } else { a = line.b; b = line.a; } if (point.y < a.y || point.y > b.y) { return(false); } return(true); }