Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  [Solved] Rotation following Mouse  (Read 4282 times)
0 Members and 1 Guest are viewing this topic.
Offline duce

Senior Newbie





« Posted 2011-09-12 19:58:50 »

In my 2D game, I have a simple player sprite that moves with WASD. I want to make it rotate to follow my mouse, so the front of the sprite always points at the mouse. I know this is probably some advanced math, can anyone help?

Edit: Here is the final code we came up with:

1  
2  
3  
4  
5  
6  
7  
8  
9  
// centerX and centerY are the center points of the sprite. playerX and playerY are the co-ordinates of the sprite.
float centerX = playerX + player.getWidth() / 2;
float centerY = playerY + player.getHeight() / 2;
         
float radiansToMouse = (float) Math.atan2(centerX - input.getMouseX(), centerY - input.getMouseY());
         
float degreesToMouse = (57.2957795f * radiansToMouse) * -1;
         
player.setRotation(degreesToMouse);
Offline zlandorf

Junior Devvie


Medals: 1
Projects: 1



« Reply #1 - Posted 2011-09-12 20:27:41 »

In my 2D game, I have a simple player sprite that moves with WASD. I want to make it rotate to follow my mouse, so the front of the sprite always points at the mouse. I know this is probably some advanced math, can anyone help?

luckily for you I've done that not long ago :

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
public double computeAngle(Vector2f vec) {
       double distance = distance(0, 0, (int)vec.getX(), (int)vec.getY());
       double angle = getAngle(vec, new Vector2f(0,-1));
   if (vec.getX() < 0) angle *= -1;
   angle = Math.toDegrees(angle);
}

public double getAngle(Vector2f vec1, Vector2f vec2) {
   double distanceV1 = distance(0, 0, (int)vec1.getX(), (int)vec1.getY());
   double distanceV2 = distance(0, 0, (int)vec2.getX(), (int)vec2.getY());
     
   double angle = Math.acos((vec1.getX() * vec2.getX() + vec1.getY() * vec2.getY()) / (distanceV1 * distanceV2));
   return angle;
}

public double distance(int x1, int y1, int x2, int y2) {
   return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) *(y2 - y1));
}


Might be more efficient ways, or cleaner code. But this is what I managed to get after some thinking around and putting together math formulas. Vector2f is from le lwjgl package, but you can replace it with whatever implementation of a vector you want.

And this code works assuming that the player's sprite is directed to the top of the image. If it's not the case, change
1  
new Vector2f(0,-1)
with the director vector of your sprite.

Btw, if others have tips to improve the efficiency / cleanliness of this code, I'd be grateful !

edit : Should of done this way before, but instead of using the function distance I put up there, I should of used
1  
vec.length();
Going to change that straight away >.<
edit2: should of used Vector2f.dot(vec1,vec2) instead of rewriting it myself too.
Offline Cero
« Reply #2 - Posted 2011-09-12 20:59:43 »

Have done it lately in Arcane Nebula 1.0

1  
2  
3  
float radiansToMouse = FastMath.atan2(sprite.centerY-mouseY, sprite.centerX-mouseX);
float degreesToMouse = 57.2957795f*radiansToMouse;
sprite.setRotation(degreesToMouse);


that angle is the angle you have to set your image's rotation to.
all images should fact LEFT at 0 degrees, in file format... so your png or whatever should face left.

FastMath is just a faster Math Lib provided by Riven amoung others.
You can use any atan2 method; one is provided by the default Math class

Remember that if you have a camera / offset / more than one screen size, you have to offset the mouse/sprite location for the calculation

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline zlandorf

Junior Devvie


Medals: 1
Projects: 1



« Reply #3 - Posted 2011-09-12 21:09:58 »

Have done it lately in Arcane Nebula 1.0

1  
2  
3  
float radiansToMouse = FastMath.atan2(sprite.centerY-mouseY, sprite.centerX-mouseX);
float degreesToMouse = 57.2957795f*radiansToMouse;
sprite.setRotation(degreesToMouse);


that angle is the angle you have to set your image's rotation to.
all images should fact LEFT at 0 degrees, in file format... so your png or whatever should face left.

FastMath is just a faster Math Lib provided by Riven amoung others.
You can use any atan2 method; one is provided by the default Math class

Remember that if you have a camera / offset / more than one screen size, you have to offset the mouse/sprite location for the calculation

I look silly with my complicated stuff Sad Didn't know about the atan2 function, much much simpler this way. I'll replace my code with that, thanks !
Offline duce

Senior Newbie





« Reply #4 - Posted 2011-09-12 21:30:30 »

This is great! Also, I accidentally found a way that you don't need to make your sprites facing left. Here is my code:

1  
2  
3  
4  
5  
6  
7  
8  
float centerX = playerX + player.getWidth() / 2;
float centerY = playerY + player.getHeight() / 2;
     
float radiansToMouse = (float) Math.atan2(centerX - input.getMouseX(), centerY - input.getMouseY());
     
float degreesToMouse = (57.2957795f * radiansToMouse) * -1;
     
player.setRotation(degreesToMouse);
Offline Cero
« Reply #5 - Posted 2011-09-12 22:20:11 »

I look silly with my complicated stuff Sad Didn't know about the atan2 function, much much simpler this way. I'll replace my code with that, thanks !

Actually, I'm just lazy as f**k, especially when there is Math; and I learned this method way back then by just asking =P

Offline zlandorf

Junior Devvie


Medals: 1
Projects: 1



« Reply #6 - Posted 2011-09-12 22:32:17 »

Yeah, actually I just checked, and "angle between two points" returns me the atan2 function in the first hit. The problem is (because I was working with vectors) I typed "angle between two vectors" which game me the formula with the dot product and stuff. Funny how things can change because you're satisfied with the one result and don't think there is anything simpler Smiley
Offline duce

Senior Newbie





« Reply #7 - Posted 2011-09-12 23:23:29 »

I'm glad we could all help eachother out here. Thanks for the replies!  Smiley
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!