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));
	}
	
}

Don't forget to do a modulus operation with 255 after you called the max method.
Your color component might end up being greater than 255.

Perhaps this code could be of interest.

Hokay so I got something that kinda works until it crashes. It goes until the balls first impacts then "Value was either too large or too small for an Int32", must be when I try converting it to an integer however the error message pops up but the program kinda works in the background with a lot of lag time and the balls do change colour.

public void Speed(double VelocityX, double VelocityY){
		double x;
		VelX = VelocityX*VelocityY;
		VelY = VelocityY*VelocityY;
		Vel = VelX + VelY;
		x = Math.Sqrt(Vel);
		speed = Convert.ToInt32(x);
		Console.WriteLine(speed);
		
	}	
	

public Color ChangeColour{
		
		get {
			return Color.FromArgb(max( speed * 25, 255 ) , max( speed * 20, 255 ), min( 255 - 20*speed, 0));
		}

		
	}
	
	public int max(int velocity, int maximum){
		if (velocity<maximum){return velocity;}
		else 
			return maximum;
		
	}
	
	public int min(int velocity, int minimum){
		if(velocity>minimum){return velocity;}
		else
			return minimum;
	}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.