在处理过程中确定旋转期间的Pshape坐标

时间:2016-02-16 18:00:26

标签: processing

我很难弄清楚如何确定旋转形状顶点的新坐标。我正在制作一个小型游戏,它有一个可以旋转的聚光灯,并在聚光灯和敌人重叠并检测敌人位置时进行检测。我想知道旋转之外是否有解决方案,或者我是否遗漏了某些东西。

 
float sightPos = 100;
float posX, posY;
float spotlightRY = 320;
float spotlightRX = 0;
float spotlightLY = 0;
float spotlightLX = -320;
int score;

PFont f;

void setup(){
size(800,800);
background(127);
frameRate(60);
posX = random(800);
posY = random(800);
score = 0;

f = createFont("Arial",72,true);

}



void draw (){
  background(255);

  noStroke();
  smooth();



   if(dist(posX,posY,spotlightLX,spotlightLY) <=10 && dist(posX, posY, spotlightRX,spotlightRY) >= 10){
    background(227); 
    score += 1;
    posX = random(800);
    posY = random(800);


  }else{
   background(50); 


  }


  textFont(f,16);                  
  fill(255);  

  text("Score: " + score, 20,20);

  text("Spotlight Controls:", 20,50);
  text("SHIFT button= Left, ALT button = Right", 20,70);  
  text("Enemy Controls:",20,110); 
  text("Use arrow keys", 20,130); 

  enemy();

  fill(235,171,235);
  ellipse(height/2, width/2,  100, 100); 
  translate(width / 2, height / 2);




  strokeWeight(2);
  //float radMin = sightPos;
  pushMatrix();
  rotate(radians(sightPos));
  spotLight();
  popMatrix();





}

void spotLight(){
   noStroke();
   fill(235, 235,117,127);
 beginShape();
  vertex(0, 5);
  vertex(spotlightRX, spotlightRY);
  vertex(spotlightLX, spotlightLY);
  vertex(-5, 0);
  endShape(CLOSE); 

  println("spotlightLX is:" + spotlightLX);
  println("spotlightLY is:" + spotlightLY);
  println("spotlightRX is:" + spotlightRX);
  println("spotlightRY is:" + spotlightRY);


}

void enemy(){


 //translate(random(800),random(880)); 
   fill(255, 100,100);



 ellipse(posX,posY,10,10);






}

void keyPressed()
{
  if(key == CODED)
  {
    if (keyCode == LEFT)
    {
     posX -= 5 ;
    }
    if(keyCode == RIGHT)
    {
      posX += 5;
    }
    if(keyCode == UP)
    {
      posY -= 5;
    }
    if(keyCode == DOWN)
    {
      posY += 5;
    }
     if(keyCode == CONTROL)
    {
      sightPos -= 5;
      println("ctrl is being pressed");
    }
     if(keyCode == ALT)
    {
      sightPos += 5;
      println("alt is being bressed");
    }

  }
}

非常感谢你的帮助,如果我能说清楚的话,请告诉我。

1 个答案:

答案 0 :(得分:0)

您可以使用screenX()screenY()函数将模型坐标转换为屏幕坐标。这是一个在75,75处旋转点的示例,并在应用旋转后显示屏幕坐标。

 
void setup() {
  size(200, 200);
}

void draw() {
  background(0);

  pushMatrix();
  rotate(((float)mouseX)/width);
  rect(50, 50, 50, 50);
  ellipse(75, 75, 10, 10);
  float sx = screenX(75, 75);
  float sy = screenY(75, 75);
  popMatrix();

  text("screen: " + sx + ", " + sy, 10, 20);
}

可以在the Processing reference找到更多信息。