Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (120)
games submitted by our members
Games in WIP (577)
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  
  Rotation following the mouse  (Read 758 times)
0 Members and 1 Guest are viewing this topic.
Offline CTucker1327
« Posted 2013-12-10 11:06:36 »

I'm using LibGDX (And this might actually be the problem, considering the top left of the screen is 0, 0 intead of the bottom right, however I have tried subtracting the MouseY from the Height of the application, this didn't work.

What I'm trying to do: I have a sprite on the screen that I want to rotate towards my mouse at all times.

Here's the thread I used to try to solve my problem http://www.java-gaming.org/topics/solved-rotation-following-mouse/24763/view

Here's my code.

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
package com.chris.spaceshooter.entities;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.math.Vector2;

public class Player extends Entity
{
   // The player Sprite
   private Sprite playerSprite;
   
   // The players movement speed
   private float playerSpeed = 2.0f;
   
   public Player(Texture texture)
   {
      this.playerSprite = new Sprite(texture);
      this.currSpeed = playerSpeed;
   }
   
   public void render(SpriteBatch batch)
   {
      playerSprite.draw(batch);
   }
   
   public void update()
   {
      if(keyPressed(Keys.W)) { moveUp(); }
      if(keyPressed(Keys.A)) { moveLeft(); }
      if(keyPressed(Keys.S)) { moveDown(); }
      if(keyPressed(Keys.D)) { moveRight(); }
     
      float radiansToMouse = (float) Math.atan2(spriteCenter(playerSprite).y - Gdx.input.getY(), spriteCenter(playerSprite).x - Gdx.input.getX());
      float degreesToMouse = 57.2957795f*radiansToMouse;
     
      //Update the sprite position to the Entity position
      playerSprite.setX(getX());
      playerSprite.setY(getY());
      playerSprite.setRotation(degreesToMouse);
   }

   // Boolean to check if a key is pressed
   private boolean keyPressed(int keycode) { return Gdx.input.isKeyPressed(keycode); }
   
   private Vector2 spriteCenter(Sprite sprite) {
      float height = sprite.getHeight();
      float width = sprite.getWidth();
      return new Vector2(width / 2, height / 2);
   }
   
}



This just doesn't rotate properly and I can't figure it out! It's making my balls snarl. Literally.

I have attatched the jar file with my single sprite in a zip file below, so if you can figure anything out, let me know.  If any further information is required, please, ask and you shall recieve.

FileSize: 5.1Mb (I guess this is because of LibGDX)
Download: http://up.ht/Jc6XKx
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #1 - Posted 2013-12-10 11:21:21 »

http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors

Make one vector originating from origin upward at 0 degrees. Get vector from origin -> mouse. Calculate.
Offline CTucker1327
« Reply #2 - Posted 2013-12-10 11:37:22 »

http://www.wikihow.com/Find-the-Angle-Between-Two-Vectors

Make one vector originating from origin upward at 0 degrees. Get vector from origin -> mouse. Calculate.

You know, $5 says you laid that out in simplest terms for me, but even after examining the link provided, I had no idea what I was looking at.

I'm going to go ahead and try to translate what you said.

First: Create a vector2 of the Current Position of the Player
Second: Create a vector2 containing the mouse position
Calculate the difference between the two.

^If this is correct, isn't that what I'm doing?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #3 - Posted 2013-12-10 13:05:29 »

I dunno, google is your friend.

I've never done vector math. I would need to know more about how vectors are defined to be able to provide you an example. There's most definitely an easier way to find out, I just don't know it. I guess you could make a right triangle with your mouse coordinate and the center of the screen, and then do some trig. I haven't taken math in quite awhile. Sorry, best I can do is give you the formula, I can't teach something I don't understand myself.


EDIT: Apparently there's an easier way. Using this formula: Math.toDegrees( Math.atan2(fromLeft - originfromLeft, originFromTop - fromTop) ) + 360.0) % 360.0
You may need to change this a bit, I dunno. I will keep looking around, I might explore how it works later as well, I haven't need angles yet. I'll try setting up an example for myself to see if it works tomorrow if you can't get it to work. I'd do it now, but I need sleep.
Offline CTucker1327
« Reply #4 - Posted 2013-12-10 21:39:50 »

Updated with more information, added my files, and tried a few more things.

Still no luck  Sad
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #5 - Posted 2013-12-10 22:52:11 »

I'll try it then. I'll post up if i figure it out. In the mean time, if anyone around here already knows how to do this... cough cough. No seriously, just post it, that'd be cool.
Offline CTucker1327
« Reply #6 - Posted 2013-12-10 23:27:27 »

I'll try it then. I'll post up if i figure it out. In the mean time, if anyone around here already knows how to do this... cough cough. No seriously, just post it, that'd be cool.

Yeah, I'd really enjoy having this code. Math isn't really my strongest subject and I'm tired of screwing with it. Honestly. I've done plenty of google searches and the only thing I can think of is that LibGdx changing the 0,0 point on the screen is what's causing the issue.
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #7 - Posted 2013-12-10 23:56:52 »

Alright here it is:
setRotation(360 - (float) ((Math.toDegrees( Math.atan2(mouse.x - origin.x, origin.y - mouse.y) ) + 360.0) % 360.0) + 90);

Origin is the middle of your camera. My camera is at 0,0 and it's 800 x 800, so my origin is 400,400. Mouse and origin are both vector2 objects.
Offline CTucker1327
« Reply #8 - Posted 2013-12-11 02:50:00 »

Alright here it is:
setRotation(360 - (float) ((Math.toDegrees( Math.atan2(mouse.x - origin.x, origin.y - mouse.y) ) + 360.0) % 360.0) + 90);

Origin is the middle of your camera. My camera is at 0,0 and it's 800 x 800, so my origin is 400,400. Mouse and origin are both vector2 objects.

This works if the sprite is in the center of the screen, however if it's in another location the rotation is still relevant to the middle of the screen, instead of the center of the sprite.
I set the origin.x/y to the center of the sprite, however that didn't work either.
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #9 - Posted 2013-12-11 03:05:35 »

Well your sprite center is wrong, which might be causing it. You're just giving the sprites dimensions, not it's position. You missed a step in there. You need to return Vector2(playerSprite.x + width / 2, playerSprite.y + height / 2), otherwise you're just returning a vector with coordinates of half your width and height of your sprite, likely 32x32.

both your code used for rotation and mine work for me, assuming the player is in the center of the screen. I'll try it for other positions.

Alright, the issue is the mouse position is relative to the position inside of the window, not based on the sprite location. You have to make the mouse give you it's position relative to the sprite location. I'll try to figure out how to go about this.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #10 - Posted 2013-12-11 03:52:58 »

1  
2  
3  
4  
5  
6  
7  
8  
float xOffset = (Gdx.graphics.getWidth() / 2) - (Player.getX() + (Player.getWidth() / 2));
      float yOffset =  (Gdx.graphics.getHeight() / 2)  - (Player.getY() + (Player.getHeight() / 2));
      float mouseX = Gdx.input.getX() + xOffset;
      float mouseY = (Gdx.graphics.getHeight() - Gdx.input.getY()) + yOffset;
     
      float radiansToMouse = (float) Math.atan2(mouseX -  (Gdx.graphics.getWidth() / 2) ,  (Gdx.graphics.getHeight() / 2)  - mouseY);
         float degreesToMouse = 57.2957795f*radiansToMouse;
         Player.setRotation(degreesToMouse - 90);


This works for wherever the player is.
Offline CTucker1327
« Reply #11 - Posted 2013-12-11 03:59:11 »

Right now, I love you.

I honestly would have never figured this out.

As I said before I hate math, lol.


Thanks!!!
Offline bilznatch

Senior Duke


Medals: 8
Projects: 2
Exp: 1 year


I'm bad, I'm bad, I'm really... really bad T_T


« Reply #12 - Posted 2013-12-11 04:00:30 »

Meh, I just did it on paper and was like oh dur. You're welcome, good luck with your program.
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.

theagentd (9 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (38 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (73 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!