Java-Gaming.org    
Featured games (79)
games approved by the League of Dukes
Games in Showcase (477)
Games in Android Showcase (107)
games submitted by our members
Games in WIP (536)
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 1557 times)
0 Members and 1 Guest are viewing this topic.
Offline Otreum

Junior Member


Medals: 6



« Posted 2013-07-17 14: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.
http://stackoverflow.com/questions/16381031/get-cursor-position-in-libgdx

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.
Quote
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.

Thanks

This is the result of mouse based rotation.

<a href="http://www.youtube.com/v/HIydNOPrXyY?version=3&amp;hl=en_US&amp;start=" target="_blank">http://www.youtube.com/v/HIydNOPrXyY?version=3&amp;hl=en_US&amp;start=</a>
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.

Riven (18 views)
2014-07-29 18:09:19

Riven (13 views)
2014-07-29 18:08:52

Dwinin (12 views)
2014-07-29 10:59:34

E.R. Fleming (31 views)
2014-07-29 03:07:13

E.R. Fleming (12 views)
2014-07-29 03:06:25

pw (42 views)
2014-07-24 01:59:36

Riven (41 views)
2014-07-23 21:16:32

Riven (28 views)
2014-07-23 21:07:15

Riven (29 views)
2014-07-23 20:56:16

ctomni231 (60 views)
2014-07-18 06:55:21
HotSpot Options
by dleskov
2014-07-08 03:59:08

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:58:24

Java and Game Development Tutorials
by SwordsMiner
2014-06-14 00:47:22

How do I start Java Game Development?
by ra4king
2014-05-17 11:13:37

HotSpot Options
by Roquen
2014-05-15 09:59:54

HotSpot Options
by Roquen
2014-05-06 15:03:10

Escape Analysis
by Roquen
2014-04-29 22:16:43

Experimental Toys
by Roquen
2014-04-28 13:24:22
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!