/// <summary> /// Add a body to the simulation. /// </summary> /// <param name="body">Body to add.</param> public void addBody(Body body) { bodies.Add(body); if (body.star) nbStars++; else nbPlanets++; }
private void drawBody(Graphics g, Body body, int absX, int absY, int absRadius) { /* * The coordinates of the objects drawn to the screen are calculated from the up-left corner * the coordinates of the bodies in the simulation are calculated from their center * we do a correction on the offset and we transform it in integer (pixel) */ int x = (int)(absX - absRadius); //(px) int y = (int)(absY - absRadius); //(px) //Color Brush brush = new SolidBrush(body.color); //Draw body g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; g.FillEllipse(brush, x, y, absRadius*2, absRadius*2); g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.Default; brush.Dispose(); }
private void drawBody(Graphics g, Body body) { Point2D absPosition = relToAbsCoors(body.position.x, body.position.y); int absRadius = radiusToAbs(body.radius, body.star); drawBody(g, body, absPosition.x, absPosition.y, absRadius); }
public void addBody(Body body) { //Transform the absolute mouse position in position relative to the center //Find where this position is in the simulation model (Scaling) body.position.x = (mouseX - centerX)*1/positionScale; body.position.y = (mouseY - centerY)*1/positionScale; body.speed = speed; space.addBody(body); }
/// <summary> /// Do the collision of 2 bodies. /// </summary> /// <param name="A">Body A</param> /// <param name="B">Body B</param> private void collide(Body A, Body B) { if (A != B) { //A < B if (A.mass < B.mass) { B.collide(A); //explode(A, B, 1); removeBody(A); } //A > B else if (A.mass > B.mass) { A.collide(B); //explode(A, B, 2); removeBody(B); } //A == B else { //explode(A, B, 0); removeBody(A); removeBody(B); } } }
/// <summary> /// Remove a body from the simulation. /// </summary> /// <param name="body">Body to remove.</param> public void removeBody(Body body) { if (body.star) nbStars--; else nbPlanets--; bodies.Remove(body); }
/// <summary> /// Check if the body is in collision with another body. /// The collisions detection depends on the collision scales: /// Different scales for radius and distance doesn't result in "real" collision detection, /// it will collide if it appear that they are colliding, not if it is really colliding in the simulation. /// </summary> /// <param name="body">The other body.</param> /// <returns>True if there is a collision with the other body</returns> public bool isColliding(Body body) { if (body != this) { //Distance between the 2 bodies possibly in collisions (Squared) int distanceSquare = (int)((position - body.position).lengthSquared * positionScale * positionScale); //Radius sum of the 2 bodies (Squared) int radiusA = (int)(star ? radius * starRadiusScale : radius * bodyRadiusScale); int radiusB = (int)(body.star ? body.radius * starRadiusScale : body.radius * bodyRadiusScale); int sumRadiusSquare = (radiusA + radiusB) * (radiusA + radiusB); if (distanceSquare < sumRadiusSquare) return true; } return false; }
/// <summary> /// Do a perfecly inelastic collision another body. /// </summary> /// <param name="body">The other body.</param> public void collide(Body body) { mass += body.mass; //v = (m1*u1 + m2*u2)/(m1 + m2) Vector2D newSpeed = (speed * mass + body.speed * body.mass) / (mass + body.mass); speed = newSpeed; colorAddition(body.color); }
/* * How it works * ------------ * Fg = GMm/r^2 (Fg> = GMm/r^2 * r>unit) * F = ma * GMm/r^2 = ma * a = -GM/r^2 (a> = -GM/r^2 * r>unit) * * v = v + a*dt * x = x + v*dt */ /// <summary> /// Calculate the acceleration produced by the gravity of a body from another. /// </summary> /// <param name="body">The other body</param> /// <param name="dt">Time step (s)</param> public void calcAcceleration(Body body, double dt) { if (this != body) //A body can't interact with itself { //r> = A.pos> - B.pos> Vector2D radius = position - body.position; //a> = -Gm/r^2 * r> Vector2D acceleration = (-G * body.mass / radius.lengthSquared) * radius.getUnitVector(); speed += acceleration * dt; //v> = v> + a>*dt } }