Hey everyone,
i started arround 1 month ago with programming and loving p5js. Right now I have a problem I can’t figure out to implement into my code. I want to have a hover effect on a triangle. I did this with the collide2d library. See code here:
function setup(){
createCanvas(400, 400);
}
var hit = false;
function draw() {
background(255);
triangle(200, 120, 120, 290, 280, 290);
hit = collidePointTriangle(mouseX, mouseY, 200, 120, 120, 290, 280, 290);
if(hit){
triangle(200, 100, 100, 300, 300, 300);
} else {
triangle(200, 120, 120, 290, 280, 290);
}
stroke(hit ? color('red') : 0);
print('colliding?', hit);
}
So far so good, but when I want to implement this into my code where I’m working with objects, it wont work.
var hit = false;
// Triangel
myt = {
x1: 800,
y1: 830,
x2: 1150,
y2: 165,
x3: 1550,
y3: 830
};
dt();
}
function dt() {
push();
fill(drawColor);
triangle(myt.x1, myt.y1, myt.x2, myt.y2, myt.x3, myt.y3);
pop();
}
function draw() {
background(255);
hit = collidePointTriangle(mouseX, mouseY, myt.x1, myt.y1, myt.x2, myt.y2, myt.x3, myt.y3)
if (hit) {
print('colliding?', hit);
triangle(myt.x1 + 60, myt.y1 + 60, myt.x2 + 40, myt.y2, myt.x3, myt.y3);
}
dt();
}
Can anyone help me? The collision detection works, but I cant implemt the hover effect.