Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
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  
  Rotate player in relation to mouse position  (Read 926 times)
0 Members and 1 Guest are viewing this topic.
Offline Luminem

Senior Newbie





« Posted 2013-04-14 09:30:01 »

Hey guys, today my question is.... How can I rotate the player in relation to mouse position?

I tried to make it, but the player was rotated always without stop :S

Simply Perfect
Offline quew8

JGO Coder


Medals: 30



« Reply #1 - Posted 2013-04-14 09:44:29 »

Your question is not very descriptive. Are you talking about a first person shooter? If so your problem could be that you don't set the mouse position back to the centre of the screen after it moves.
If this is not your problem, or even your question, consider being a little more helpful to people trying to help you. Maybe you could even post some code.
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #2 - Posted 2013-04-14 09:44:59 »

Is this in 3D or 2D? I guess it is 2D. Then I think you have a top down view and the player can rotate by the imaginary z axis coming out of the screen.

In this case it must be fairly simple. Just compute the distance between the player position and the mouse position and store it for example as a Vector2. You can then normalize it and calculate the dot product on the norm vector representing rotation of 0 degrees. You then just have to see if you are above or under the y position of the player to get the angle right.

If my assumptions are right, tell me if you need more explanation. Or maybe there is an even easier way.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Luminem

Senior Newbie





« Reply #3 - Posted 2013-04-14 09:51:23 »

Oh sorry for don't write a bigger description of my problem.

Okay I've a little 2D game. Where the player can shot. So, I need rotate the "bow" in function of the mouse position. Really my problem isn't rotate the player, because I made it more or less. My problem is I can't stop the rotation. My problem is, when I move the mouse, the player rotate yes, but it stops never. So I want that when the player has rotated then stop. I don't know how to make that.

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  
public void rotate(double x, double y, double cx, double cy, double n) {
      double dx = x - cx;
      double dy = y - cy;
      double r = Math.sqrt(dx*dx + dy*dy);
      double a = Math.atan2(dy, dx);
      a -= n / 180 * Math.PI;
      this.x = cx + r * Math.cos(a);
      this.y = cy + r * Math.sin(a);
   }
   
   public void render(Graphics g) {
      //image.draw((float)x, (float)y);
     g.setColor(Color.red);
      g.drawLine((float)x, (float)y, (float)x2, (float)y2);

   }
   
   public void update(GameContainer container) {
      Input input = container.getInput();
      rotation = Math.atan2( input.getMouseY() - 600/2, input.getMouseX() - 800/2 );
     
      rotate(x, y, x2, y2, rotation);
   }

Simply Perfect
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #4 - Posted 2013-04-14 10:55:41 »

I will try to comment your code to see where the problems may be.
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
public void rotate(double x, double y, double cx, double cy, double n) {
      double dx = x - cx; // calculate current angle of drawn line (the bow?)
     double dy = y - cy;
      double r = Math.sqrt(dx*dx + dy*dy);
      double a = Math.atan2(dy, dx); // angle is known now in rad

      a -= n / 180 * Math.PI; // rotation in rad is converted to rad... uhm, yeah. And then subtracted from the computed angle of the line... am I getting this wrong?
     this.x = cx + r * Math.cos(a);
      this.y = cy + r * Math.sin(a);
   }
   
   public void render(Graphics g) {
      //image.draw((float)x, (float)y);
     g.setColor(Color.red);
      g.drawLine((float)x, (float)y, (float)x2, (float)y2);

   }
   
   public void update(GameContainer container) {
      Input input = container.getInput();
      rotation = Math.atan2( input.getMouseY() - 600/2, input.getMouseX() - 800/2 ); // mouse coordinates relative to center of the screen used for getting the angle between normal position without rotation and rotation to current mouse position
     
      rotate(x, y, x2, y2, rotation); // method for rotation. Uses line start and end point and rotation angle as inputs...
  }
Offline dime26

Senior Member


Medals: 2
Projects: 3
Exp: 5 years


Should traffic wardens be armed?


« Reply #5 - Posted 2013-04-14 11:55:34 »

I do something similar in my game but only have 8 directions that the player can be facing, would that be of any use to you?

Watch on of the videos here. Sorry if this is not what you want.

http://www.java-gaming.org/topics/iconified/29239/view.html
Online Roquen
« Reply #6 - Posted 2013-04-14 13:30:54 »

Angles hate you.  Hate them back: http://www.java-gaming.org/topics/calculating-theta-of-what/29174/msg/267308/view.html#msg267308
Offline Luminem

Senior Newbie





« Reply #7 - Posted 2013-04-14 14:58:50 »

Guys, I don't know if you didn't understand me or I don't understand you. My problem isn't to rotate the line, because I can rotate it perfectly, my problem is stop the rotation once it has the correct angle.

Simply Perfect
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #8 - Posted 2013-04-14 16:20:17 »

Because of not having any proper information about what "doesn't stop rotating" really means, we can't really tell what you want to hear apart from where the problems in the code may lie.

There is no screenshot, no captured video, no sketch, no precise information about it. Anything of this could help. All that I see in your code for now is, that you seem to be messing with angles in degrees and radians. But maybe I got this wrong. In the end, you should know best and tell us, if this could actually be the problem or not. So, when you look at the comments I made on your code, is this correct?
Offline Zhon

Junior Member


Medals: 4



« Reply #9 - Posted 2013-04-14 17:20:01 »

It rotates endlessly even though the angle didn't change?
Did you try calculating a delta value between the last angle and the newest before changing the rotation of the player?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Luminem

Senior Newbie





« Reply #10 - Posted 2013-04-15 16:03:06 »

Okay I've recorded a little video
http://screencast-o-matic.com/watch/cIfoDeV3gM

So I hope this is helpful

P.S. Sorry for the video poor quality, That's a bad webpage but I didn't want to upload the problem to youtube. And on my PC, the rotation is faster...

Simply Perfect
Offline Sparky83

Senior Member


Medals: 6
Projects: 1



« Reply #11 - Posted 2013-04-15 17:29:51 »

Thank you for the video, this helps. It doesn't really look like the problem is just that it won't stop rotating.

So there still is this line, that seems wrong:
1  
      a -= n / 180 * Math.PI;

What are you doing there and why? Is the value n supposed to be in degrees? Is it actually in degrees?
Offline Luminem

Senior Newbie





« Reply #12 - Posted 2013-04-15 17:44:21 »

I don't know what is its function, I just was testing Smiley. And Now I've a problem with that line, I deleted it and now The line doesn't rotate..

Simply Perfect
Offline philfrei
« Reply #13 - Posted 2013-04-15 19:43:40 »

If you have a process that starts but does not stop, I can think of two general approaches. Either (1) the method stops itself, or (2) something external to the method tells it to stop.

(1) Create a variable that iterates with each increment of the rotation. Test against a maximum value or another condition that lets you know it is time to stop. For example, perhaps you "know" it takes 20 rotating steps to reach the final position--just have a counter test for 20. Or you know the final angle should be N radians. Again, test for N radians with every iteration.

(2) Create a "state" boolean, such as 'isRotating'. Consult this boolean each increment before deciding whether to rotate or not. An external method communicates with the rotating method by setting isRotating to true or false.

The second idea makes sense if you are using something like mousePressed() to start the rotation. If so, perhaps using mouseReleased() is the desired way to stop it? This is possible if mouseReleased() changes the isRotating boolean to false and the rotating method is consulting this boolean every iteration.

BTW, if you do this second method, it helps to make isRotating volatile.


"Greetings my friends! We are all interested in the future, for that is where you and I are going to spend the rest of our lives!" -- The Amazing Criswell
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.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (19 views)
2014-09-24 16:12:06

Grunnt (45 views)
2014-09-23 14:38:19

radar3301 (27 views)
2014-09-21 23:33:17

BurntPizza (63 views)
2014-09-21 02:42:18

BurntPizza (32 views)
2014-09-21 01:30:30

moogie (40 views)
2014-09-21 00:26:15

UprightPath (50 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
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

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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!