Making something spin/rotate

I made this small survival game where you are a ball and are supposed to dodge shurikens using the arrow keys.I want to draw the 2 shuriken triangles in a way so that they rotate around the center of the shuriken at a constant speed while still maintaining their shape as a triangle but can’t figure out how to. Help would be appreciated. thanks

Ball[] balls = {};
Shuriken[] shurikens = {};
color shurikenColor = color(150);
color ballColor = color(200,0,200);
long startGame = 0;
long lastShuriken = -3000;
long lastAddition= 0;
int score, addition, multiplier, time;
int spawnCooldown = 5000;
PFont fontTitle, fontN, fontD;
Boolean lose, play, escape;

void setup() {
   size (1200,800);
  fontTitle = createFont("Arial", 50);
  fontN = createFont("Arial", 30);
  fontD = createFont("Arial", 70);
  lose = false;
  play = false;
  escape = false;
}
void draw(){
  background(255); 
 
  
 if(!escape && play && !lose){ 
   balls = (Ball[]) append(balls,new Ball(width/2,height/2,0,0,40));
  if (balls[0].getDy()>7){
    balls[0].setDy(7);
  }
  if (balls[0].getDy()<-7){
    balls[0].setDy(-7);
  }
  if (balls[0].getDx()<-7){
    balls[0].setDx(-7);
  }
  if (balls[0].getDx()>7){
    balls[0].setDx(7);
  }
   multiplier = shurikens.length;
   if(millis () - lastAddition - startGame > 998) {
    addition = 1;
    score = score + addition * multiplier;
    lastAddition = millis () - startGame;
  }
  if ((millis()-startGame-1999)/1000 < 0){
  time = 0;
  } else {
  time = int((millis()-startGame-1999)/1000);
  }
    textFont(fontN);
   fill(0);
   text("Time:",10,50);
   text(time,110,50);
   text("Score:",500, 50);
   text(score,600,50);
   text("Shurikens:",1000,50);
   text(shurikens.length,1150,50);
  balls[0].showAndMove();
  for (int i=0;i<shurikens.length;i++) {
     shurikens[i].showShuriken();
  }
  if(millis () - lastShuriken - startGame > spawnCooldown) {
    Shuriken nShuriken = new Shuriken(40 ,40);
   shurikens = (Shuriken[]) append(shurikens,nShuriken);
    lastShuriken = millis ()- startGame;
  }
  
}else if(lose) {
  background(255);
 textFont(fontD);
 fill(120,0,0);
  text("You Died!", 430, 400);
  textFont(fontN);
  text("Score:",510,500);
  text(score,610,500);
  text("Time survived:", 460,550);
  text(time, 670, 550);
  text("Shurikens spawned:",430,600);
  text(shurikens.length, 715,600);
   textFont(fontN);
  fill(0,255,0);
 text("Click anywhere to play again!", 380, 700); 
} else if (escape){
  background(255);
textFont(fontD);
 fill(120,0,0);
  text("There is no escaping your fate!", 120, 400); 
   textFont(fontN);
  text("Score:",510,500);
  text(score,610,500);
   
  text("Time survived:", 460,550);
  text(time, 670, 550);
  text("Shurikens spawned:",430,600);
  text(shurikens.length, 715,600);
  textFont(fontN);
  fill(0,255,0);
 text("Click anywhere to play again!", 380, 700); 
} else {
background(255);
 textFont(fontN);
 fill(0);
 text("Survive for as long as possible by avoiding the shurikens!", 220, 300); 
 text("You can move around with the arrow keys.", 300, 360);  
 
 textFont(fontTitle);
 text("Objective:",470,200);
 fill(0,255,0);
 text("Click anywhere to start game!", 260, 550); 
}
}
class Ball {
   float bx;
   float by;
   float dx;
   float dy;
   int balldiam;
   
   Ball (float x, float y, float vx, float vy, int diam)
   {  
     bx = x;
     by = y;
     dx = vx;
     dy = vy;
     balldiam = diam;
   }
   void setDx(float x){
   dx = x;
   }
   void setDy(float y){
   dy = y;
   }
   float getDx(){
   return dx;
   }
   float getDy(){
   return dy;
   }
   void showAndMove() {
      fill(ballColor);
      ellipse(bx,by,balldiam,balldiam);
       if (checkAgainstShurikens(bx,by,balldiam)) {
        dx = 0;
        dy = 0;
        lose = true;
      }
      bx = bx + dx;
      by = by + dy;
      if ((bx>=width) || (bx<=0)) {dx = -dx;}
      if ((bx>=width+10) || (bx<=-10)) { escape = true;}
      if ((by>=height) || (by<=0)) {dy = -dy;}
      if ((by>=height+10) || (by<=-10)) { escape = true;}
     
   }  
}
void mouseReleased(){
  if(lose ||escape ||!play){
   play = true;
   lastShuriken = -3000;
   lastAddition= 0;
   startGame = millis();
   for(int i =shurikens.length ; i>0;i--){
   shurikens = (Shuriken[]) shorten(shurikens);
   }
   lose = false;
   escape = false;
   score = 0;
   for(int i = balls.length ; i>0;i--){
   balls = (Ball[]) shorten(balls);
   }
  }
}

void keyPressed() { 
  if (keyCode==UP && lose == false) {
    if (balls[0].getDy() > 0){
       balls[0].setDy(0);
    }else if(balls[0].getDy()>-7){
         balls[0].setDy(balls[0].getDy() - 1.5);
    }
  }
  if (keyCode==DOWN && lose == false) {
    if (balls[0].getDy() < 0){
       balls[0].setDy(0);
    }else if(balls[0].getDy()<7){
         balls[0].setDy(balls[0].getDy() + 1.5);
    }
  }
  if (keyCode==LEFT && lose == false) {
    if (balls[0].getDx() > 0){
       balls[0].setDx(0);
    }else if(balls[0].getDx()>-7){
         balls[0].setDx(balls[0].getDx() - 1.5);
    }
  }
  if (keyCode==RIGHT && lose == false) {
    if (balls[0].getDx() < 0){
       balls[0].setDx(0);
    }else if(balls[0].getDx()<7){
         balls[0].setDx(balls[0].getDx() + 1.5);
    }
  }
}
Boolean checkAgainstShurikens(float bx, float by, float bdiam) {
  Boolean response = false;
  for  (int i=0;i<shurikens.length;i++) {
      if (shurikens[i].tooClose(bx,by,bdiam)) {
         response = true;
         break;
      }
  }
  return response;
}
class Shuriken {
   float sx;
   float sy;
   float dx;
   float dy;
   float shurikenDiam;
   Shuriken (float x, float y) {
      sx = x;
      sy = y;
      dx = random(-8,8);
      dy = random(-8,8);
      while(!((dx > 5 || dx <-5)) || (dy > 5 || dy <-5)){
      dx = random(-8,8);
      dy = random(-8,8);
      }
      shurikenDiam = dist(sx-15,sy-15,sx+50,sy);
   }
   Boolean tooClose(float ax, float ay,float bdiam) {
       float dis = dist(ax+bdiam/2,ay+bdiam/2,sx+shurikenDiam/2,sy+shurikenDiam/2);
       return (dis<(bdiam/2)+(shurikenDiam/2));      
   }
   void showShuriken() {
       fill(shurikenColor);
       triangle(sx-15,sy-15,sx+50,sy,sx,sy+50);
       triangle(sx+15,sy-25,sx-25,sy+25,sx+35,sy+37);
     
      sx = sx + dx;
      sy = sy + dy;
      if ((sx>=width) || (sx<=0)) {dx = -dx;}
      if ((sy>=height) || (sy<=0)) {dy = -dy;}
     
   }
}

Your game looks pretty cool, and I think you are close to making them rotate.
A few things to note:

  1. rotate from the origin ie. (0, 0), so redraw your shape from the origin. (I think your sx and sy values are slightly off from the center.
  2. use pushMatrix() and popMatrix() to isolate the transformation.
float sx;
float sy;

void setup() {
  size(500, 500);

  sx = width/2;
  sy = height/2;
}

void draw() {
  background(220);

  pushMatrix();
  translate(sx, sy);
  rotate(frameCount/20.);
  triangle(-15, -15, 50, 0, 0, 50);
  triangle(15, -25, -25, 25, 35, 37);
  popMatrix();

  sx += 1;
  sy += 2;
}

Thanks a lot! Didn’t know it was that simple.

class Shuriken {
   float sx;
   float sy;
   float dx;
   float dy;
   float shurikenDiam;
   Shuriken (float x, float y) {
      sx = x;
      sy = y;
      dx = random(-8,8);
      dy = random(-8,8);
      while(!((dx > 5 || dx <-5)) || (dy > 5 || dy <-5)){
      dx = random(-8,8);
      dy = random(-8,8);
      }
      shurikenDiam = dist(sx-15,sy-15,sx+50,sy);
   }
   Boolean tooClose(float ax, float ay,float bdiam) {
       float dis = dist(ax+bdiam/2,ay+bdiam/2,sx+shurikenDiam/2,sy+shurikenDiam/2);
       return (dis<(bdiam/2)+(shurikenDiam/2));      
   }
   void showShuriken() {
      fill(shurikenColor);
      pushMatrix();
      translate(sx, sy);
      rotate(frameCount/20.);
      triangle(-15, -15, 50, 0, 0, 50);
      triangle(15, -25, -25, 25, 35, 37);
      popMatrix();
     
      sx = sx + dx;
      sy = sy + dy;
      if ((sx>=width) || (sx<=0)) {dx = -dx;}
      if ((sy>=height) || (sy<=0)) {dy = -dy;}
    
   }
} 
1 Like