Reputation: 23
I am having a hard time trying to figure out how to determine the new coordinates of the vertices of my rotating shape. I am working on a little game that has a spotlight that rotates and detects when there is an overlap of the spotlight and an enemy and resets the enemies position. I am wondering if there is a solution to this outside of rotate or whether or not I'm missing something.
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");
}
}
}
Thank you so much for your help and left me know if I can clarify anything.
Upvotes: 2
Views: 246
Reputation: 42176
You can use the screenX()
and screenY()
functions to convert from model coordinates to screen coordinates. Here's a little example that rotates a point at 75,75
and displays the screen coordinates after the rotation is applied.
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);
}
More info can be found in the Processing reference.
Upvotes: 1