Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (163) games submitted by our members Games in WIP (632) 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 JonjavaJust 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 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
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18 `        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;        else           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]
 theagentd (16 views) 2015-05-27 22:21:17 theagentd (20 views) 2015-05-27 22:21:08 CopyableCougar4 (17 views) 2015-05-27 19:24:50 MrMapcom (24 views) 2015-05-23 20:26:16 MrMapcom (32 views) 2015-05-23 20:23:34 Waterwolf (37 views) 2015-05-20 15:01:45 chrislo27 (44 views) 2015-05-20 03:42:21 BurntPizza (79 views) 2015-05-10 15:53:18 FrozenShade (63 views) 2015-05-07 09:11:21 TheLopais (226 views) 2015-05-06 13:36:48
 Spasi 29x Rayvolution 23x Riven 16x theagentd 15x Drenius 15x BurntPizza 15x ra4king 13x opiop65 12x EgonOlsen 11x princec 11x DavidBVal 11x Husk 11x KevinWorkman 10x scanevaro 8x orangepascal 8x kevglass 8x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 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