Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (757)
Games in Android Showcase (229)
games submitted by our members
Games in WIP (844)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
   Home   Help   Search   Login   Register   
  Show Posts
Pages: [1]
1  Game Development / Game Mechanics / Re: Character in center of screen turns toward mouse on: 2012-08-07 22:12:21
Hi Jonjava

Just after I finished writing it I kind of thought of that as an option but your implementation was much better and seems to work perfectly Smiley

Thanks very much!
2  Game Development / Game Mechanics / Character in center of screen turns toward mouse on: 2012-08-06 22:58:05
Hi everyone,

In my current game I intend for the player controlled character to turn to face the mouse and keyboard keys move them. My first implementation was to have them face it at all times which was no problem.

Next I wanted to have it turn at a specified rate toward the mouse.
This came up with some new issues. When the mouse crossed over the 180' to -180' range it would start turning the other way.
I did end up solving this though
        double delta_x = mouse.x - position.x;
        double delta_y = mouse.y - position.y;
        double angle = Math.toDegrees(Math.atan2(delta_y, delta_x));
        double difference = angle - direction;
        while (difference < -180) difference += 360;
        while (difference > 180) difference -= 360;  
        if(difference < 0)
           direction -= turnRate;
           direction += turnRate;

        if(mouseLast.equals(mouse)) {
           if(Math.abs(difference) < turnRate) {
              direction = angle;

This took me a while to find so if anyone else had this issue I hope this helps but my question is to do with the last if statement. Without it, the character would jump around back and forward because its pretty much impossible to 'land' on the angle to face with a specified turn rate.
First I just had the 'if angle difference is less than turn rate, set it to the angle' This was ok and got rid of any jagged movement but on a character with a low turn rate, you could move the mouse slowly and it would keep snapping to that angle and you could turn it WAYYY faster than it could on its own.
Now with the current mouse check against the last to see if its moving it wont 'cheat' but now for fast turning characters on slow mouse movements it will jump back and foward around the target point.

So is this a problem that can be fixed or will it always be a trade off between the two. Or is there any other trigonometric techniques that will help?
Pages: [1]
EgonOlsen (76 views)
2018-06-10 19:43:48

EgonOlsen (56 views)
2018-06-10 19:43:44

EgonOlsen (76 views)
2018-06-10 19:43:20

DesertCoockie (258 views)
2018-05-13 18:23:11

nelsongames (156 views)
2018-04-24 18:15:36

nelsongames (155 views)
2018-04-24 18:14:32

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

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

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

Solater (173 views)
2018-03-17 05:04:08
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!