public override bool Equals(object obj) { if (obj == null || GetType() != obj.GetType()) { return(false); } BallV b = (BallV)obj; return(this.position.Equals(b.position) && this.velocity.Equals(b.velocity)); }
public void courseCollision(BallV b) { //new collision checking - rolled in for efficiency Vec2 delta = (this.position.Subtract(b.position)); float r = this.radius + b.radius; float dist2 = delta.Dot(delta); //if (dist2 > r * r) return; float d = delta.GetLength(); //mtd - minimum translation distance to avoid sticking Vec2 mtd; if (d != 0.0f) { mtd = delta.Multiply(((this.radius + b.radius) - d) / d); } else //special, balls are exact overlap { d = this.radius + b.radius - 1.0f; delta = new Vec2(b.radius + this.radius, 0.0f); mtd = delta.Multiply(((this.radius + b.radius) - d) / d); } //inverse mass quantities float im1 = 1.0f / this.mass; float im2 = 1.0f / b.mass; //push-pull apart this.position = this.position.Add(mtd.Multiply(im1 / (im1 + im2))); b.position = b.position.Subtract(mtd.Multiply(im2 / (im1 + im2))); //impact speed Vec2 v = (this.velocity.Subtract(b.velocity)); float vn = v.Dot(mtd.Normalize()); //intersecting but already receding if (vn > 0.0f) { return; } //impulse on collision float i = (-(1.0f + 0.80f) * vn) / (im1 + im2); Vec2 impulse = mtd.Multiply(i); //momentum change this.velocity = this.velocity.Add(impulse.Multiply(im1)); b.velocity = b.velocity.Subtract(impulse.Multiply(im2)); }
//old collision checking public bool BallsColliding(BallV b) { float xd = position.x - b.position.x; float yd = position.y - b.position.y; float sqrDist = (xd * xd) + (yd * yd); int sumRad = radius + b.radius; int sqrRad = sumRad * sumRad; if (sqrDist <= sqrRad) { return(true); } return(false); }
//create on click private void canvasArea_MouseLeftButtonDown(object sender, MouseButtonEventArgs e) { Vec2 currentVel = new Vec2(0, 0); currentVel = currentVel.Add(creatorVelocity); var currentRad = creatorRadius; if (creatorIsBody) { Body newBody; newBody = new Body((int)e.GetPosition(canvasArea).X, (int)e.GetPosition(canvasArea).Y, 0, 0, currentRad); bodies.Add(newBody); DrawObjects(); } else { BallV newBall; newBall = new BallV((int)e.GetPosition(canvasArea).X, (int)e.GetPosition(canvasArea).Y, currentVel, currentRad); balls.Add(newBall); DrawObjects(); } }