Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I have an object face the mouse in p5?

I'm have a problem making an object face the mouse and the websites tutorials don't help!

function draw() {
    background(0, backgroundColor2, 0); 
    cursor('crosshair.png')
    frameRate(1000);
    angleMode(DEGREES);  
    imageMode(CORNER)
    let a = atan2(mouseY - height / 2, mouseX - width / 2);
    rotate(a);
    image(gun, width/2, height/2, 40, 40);
}
like image 515
TheDiamondfinderYT Avatar asked Dec 05 '25 18:12

TheDiamondfinderYT


1 Answers

The object is rotating but it is rotating on 0, 0

So if you want the object to rotate on the center of the screen then,

    function draw() {
       background(0, backgroundColor2, 0); 
       cursor('crosshair.png')
       frameRate(1000);
       angleMode(DEGREES);  
       imageMode(CORNER)
       let a = atan2(mouseY - height / 2, mouseX - width / 2);
       translate(width/2, height/2);
       rotate(a);
       image(gun, 0, 0, 40, 40);
    }

Here is a link for you to understand better https://www.youtube.com/watch?v=o9sgjuh-CBM

like image 68
Vivek Avatar answered Dec 08 '25 07:12

Vivek



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!