this
// v 0.1 use mousePressed and mouseReleased
// v 0.2 give speed to slow down rotation
// v 0.3 and rotate 7 times ( not use mouseReleased )
String rev = " v0.3";
int x=100, y=x, w=50, h=w, grid=3, many=grid*grid, diam=30;
int dice = 0;
int[][] points = {
{0, 0, 0, 0, 0, 0, 0, 0, 0}, // none
{0, 0, 0, 0, 1, 0, 0, 0, 0}, //1
{0, 0, 1, 0, 0, 0, 1, 0, 0}, //2
{0, 0, 1, 0, 1, 0, 1, 0, 0}, //3
{1, 0, 1, 0, 0, 0, 1, 0, 1}, //4
{1, 0, 1, 0, 1, 0, 1, 0, 1}, //5
{1, 0, 1, 1, 0, 1, 1, 0, 1}, //6
{1, 1, 1, 1, 1, 1, 1, 1, 1} //all used as dice = 7 rolling
};
PShape squircle;
float speed, slower = 0.0001, ang = 0; // animation: dice rolling
int roll = 3; // roll limit
void setup() {
size(300, 300);
surface.setTitle("myDice"+rev);
println("mouse click makes the magic");
noStroke();
my_squircle(1, 90, 90); // generate the dice shape
}
void draw() {
background(200, 200, 0);
draw_dice();
}
void mousePressed() {
dice = 7;
ang = 0;
speed = 0.10;
}
//__________________________________________________________ DICE
void draw_dice() {
if ( dice == 7 ) { // dice rolling
translate(width/2, height/2);
speed -= slower; // v0.2 slow down rotation
if ( speed < 0 ) speed = 0; // avoid rolling back
ang += speed;
rotate(ang);
if ( speed == 0 || ang > roll * TWO_PI ) { // v 0.3 roll limit
dice = int(random(1, 7));
println("new dice: "+dice);
}
translate(-width/2, -height/2);
}
shape(squircle, width/2, height/2); // shape first
for (int i = 0; i < many; i++) {
fill(180, 180, 180); // same dice squircle border, 190 would be invisible
if ( points[dice][i] == 1 ) fill(200, 0, 0);
ellipse(x+(i%grid)*w, y+(floor(i/grid))*h, diam, diam);
}
}
//__________________________________________________________ SQUIRCLE
void my_squircle(float r, float wx, float wy) {
float x1, y1;
squircle = createShape();
squircle.beginShape();
squircle.stroke(180, 180, 180);
squircle.strokeWeight(5);
squircle.fill(190, 190, 190);
for (float t = 0; t < TWO_PI; t += TWO_PI/160) { // 160 vertex points is too much
x1 = pow(abs(cos(t)), 0.5) * r*wx * sign(cos(t));
y1 = pow(abs(sin(t)), 0.5) * r*wy * sign(sin(t));
squircle.vertex(x1, y1);
}
squircle.endShape(CLOSE);
}
float sign(float input) {
if (input < 0) return -1.0;
if (input > 0) return 1.0;
return 0.0;
}