Hey y'all, so I've created a 2D billiard program that basically just collides 2 balls together and determines their new direction and velocity however I now want to be able to change the colors of the balls after collision by using
Color.FromArgb(int r, int g, int b)
. I would use something like
Red component = max( velocity * 25, 255 )
... etc for blue and green to create the colours of the balls afterwards (velocity would just be the sum of the square horizontal and vertical velocities). I was wondering if anyone had any tips other than what I have already stated to help me along on this. Thanks (below is code thus far)
using System;
using System.Drawing;
using System.Collections.Generic;
public class Ball: Shape {
double radius;
private double mass;
// constructor for a ball with radius r, centred at position x,y,
// moving at velocity vx,vy, and with the given colour and mass
public Ball( double x, double y, double vx, double vy, double radius, double mass )
: base(x,y,vx,vy) {
this.radius = radius;
if (vx == 0.0 && vy == 0.0)
FillColour = Color.Black;
this.mass = mass;
}
// access the ball's mass
public double Mass {
get{ return mass; }
set{ mass = value; }
}
// access the ball's radius
public double Radius {
get{ return radius; }
set{ radius = value; }
}
// Moves the ball in the direction of the its vector for the given time duration,
// bouncing off the sides if there is a collision with any of the 4 sides
public override void Move( Size rectangleSize, IList<Shape> otherObjects, double duration ) {
double top = (double)rectangleSize.Height;
double right = (double)rectangleSize.Width;
X += VelocityX * duration;
if (VelocityX < 0.0 && (X - Radius) < 0.0) { // bounce off left wall
X = -(X - Radius);
VelocityX = -VelocityX;
} else
if (VelocityX > 0.0 && (X + Radius) > right) { // bounce off right wall
X = right*2.0 - X - Radius;
VelocityX = -VelocityX;
}
Y += VelocityY * duration;
if (VelocityY < 0.0 && (Y - Radius) < 0.0) { // bounce off bottom wall
Y = -(Y - Radius);
VelocityY = -VelocityY;
} else
if (VelocityY > 0.0 && (Y + Radius) > top) { // bounce off top wall
Y = top*2.0 - Y - Radius;
VelocityY = -VelocityY;
}
}
// Draw the circle at the current X,Y position
public override void Draw( Graphics grfx ) {
grfx.FillEllipse(brush, (float)(X-Radius), (float)(Y-Radius), (float)(Radius*2.0), (float)(Radius*2.0));
}
}