Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (753)
Games in Android Showcase (228)
games submitted by our members
Games in WIP (842)
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  
  [LibGDX] Player rotation using mouse position  (Read 5880 times)
0 Members and 1 Guest are viewing this topic.
Offline Otreum

Junior Devvie

Medals: 6

« Posted 2013-07-17 12:10:22 »

[FIXED] UPDATE: I worked out what my whole problem was. It wasn't that my code was incorrect. It was that my sprite and my camera were offset, but my mouse was not. So no matter what I tried, it would not function how I wanted it to. The closest thing I got to getting it to work correctly before was for it to move in a 90 degree arc but not follow the mouse accurately.
Obviously now I have offset the mouse position to half the screen width and height, things work exactly how they should work.

Hopefully people who run into the same problem will see this and realise this COULD be their problem Tongue


Ok, so I have given in and decided to ask for help because I just haven't gotten anywhere with this.

I am experimenting with different general game mechanics, such as vehicle movement using keyboard and while the player is running around, they can use the mouse to aim and interact, keyboard to strafe and move forward and back.

So far, I haven't seemed to have gotten anywhere with it for many hours now, and it's doing my head in, especially since i'm pretty sure I got it working the other day when I did a quick test, but I don't remember the code I typed before I deleted it to try out keyboard movement and rotation Sad

This link explains what I want to do.

However I don't fully understand the solution provided.

I can get the mouse coordinates just fine. That part is pretty straight forward.

But this part to me seems rather ambiguous.
Update a Vector2 that points from the current sprite position to the cursor position. This can be your Sprite's heading. You'll want to rotate the sprite's heading to match this vector.

Use Vector2.angle() to compute the angle of this vector, and set your sprite's rotation to this. (This is relative to the positive X axis,so you may need to add a constant if you want it relative to the Y axis.)

I've tried a bunch of different things, I can get the player to rotate when I move the mouse, but not with the behaviour I would like it to.

I thought that using a dot product might help, and is probably the way to do this, but I'm not entirely sure of how to use the dot product of two vectors in this case.
I know the syntax for it, just not the application for this situation.

Any help would be appreciated.


This is the result of mouse based rotation.

<a href=";hl=en_US&amp;start=" target="_blank">;hl=en_US&amp;start=</a>
Pages: [1]
  ignore  |  Print  

ivj94 (585 views)
2018-03-24 14:47:39

ivj94 (49 views)
2018-03-24 14:46:31

ivj94 (383 views)
2018-03-24 14:43:53

Solater (63 views)
2018-03-17 05:04:08

nelsongames (109 views)
2018-03-05 17:56:34

Gornova (159 views)
2018-03-02 22:15:33

buddyBro (704 views)
2018-02-28 16:59:18

buddyBro (92 views)
2018-02-28 16:45:17

xxMrPHDxx (494 views)
2017-12-31 17:17:51

xxMrPHDxx (734 views)
2017-12-31 17:15:51
Java Gaming Resources
by philfrei
2017-12-05 19:38:37

Java Gaming Resources
by philfrei
2017-12-05 19:37:39

Java Gaming Resources
by philfrei
2017-12-05 19:36:10

Java Gaming Resources
by philfrei
2017-12-05 19:33:10

List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05 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‑
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!