Need help rotating an image to face the mouse.
in
Programming Questions
•
1 year ago
Hey everyone.
I'm really new to processing and programming in general, and I'm trying to create a simple game, for a touch screen installation.
The game is supposed to have a spider chase the mouse, and progress through a few levels where more spiders are added.
The issue I'm having at the moment is getting the image of the spider to rotate to face the location of the mouse.
I've read through the site, and a few people have asked the same question and were told to use the function atan2, I've read about atan2, but have no clue on how to use it or implement it into my code. If anyone could show me how it would work with my code, I'd be very grateful, thanks.
This is what I have so far (I know there are probably things that make the average coder wince in here, as I said, I'm new to this whole thing)
- float x = -1;
- float y = 14400;
- int radius = 50;
- float easing = 0.05;
- float speed = 0;
- float gravity = 0.1;
- float upwardforce = -0.75;
- float angle, oldAngle = 0;
- PImage a;
- PImage b;
- void setup() {
- size(1440, 900);
- smooth();
- ellipseMode(RADIUS);
- angle = atan2(mouseY-height/2,mouseX-width/2);
- a = loadImage("background.png");
- b = loadImage("spider.png");
- }
- void draw() {
- background(a);
- image(b,x, y, radius, radius);
- rotate(angle);
- fill(0);
- fill(2);
- float targetX = mouseX;
- float targetY = mouseY;
- if (mousePressed) {
- if (targetX > x && x < width - 12 || targetX < x && x > 12) {
- x += (targetX - x) * easing;
- }
- if (targetY < y && y > 12 || targetY > y && y < height - 12) {
- y += (targetY - y) * easing;
- }
- }
- }
1