Fractal Softworks Forum

Please login or register.

Login with username, password and session length

Author Topic: [programming] rotate to mouse with limited speed - like sf turrets  (Read 3191 times)

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile

hi all, not sure if this is the right place to ask this, but I've seen a share of good programmers around here (not to mention the man himself):

So I'm trying to make a turret (for a project) point towards the mouse, much like how sf does, and give the turret limited speed (so it doesn't simply snap to the mouse, again much like sf)

Current code determines the rotation the turret wants to be at as a function of mouse position and turret position:
Spoiler
return(Math.atan2((stage.mouseY-this.y),(stage.mouseX-this.x))*180/Math.PI);
[close]

and rotates the turret by increments to either side depending on if its rotation is greater than or smaller than the desired rotation
Spoiler
         if (rotation - desired > rotationspeed* -1 && rotation - desired <  rotationspeed)
         {
            return desired;
         }
         else if (desired > rotation)
         {
            return rotation + rotationspeed;
         }
         else if (desired < rotation)
         {
            return rotation - rotationspeed;
         }
[close]


More or less does what I want. However, when the mouse is at the back of the turret, the desired rotation snaps from -180 to +180 and the turret goes all the way around to the mouse, and then around again if the mouse crosses the line again.

if you set the firing arc to 360 on a sf turret that won't be a problem, so there is a way around that. Wondering if anyone here knows this way and is willing to share it.

thanks in advance

gunny
Logged
mmm.... tartiflette

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7233
  • Harpoon Affectionado
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #1 on: December 01, 2012, 01:55:30 PM »

There is probably a more elegant solution, but the easiest way to do this would be to compute the angular distance needed to turn clockwise/counterclockwise explicitly, and go with the lesser.


Code

float counterC = desired - rotation;
counterC = (counterC%360 + 360)%360; // Sorry about this... does Java not have a true modulus operator?

float clockwise = 360 - counterC;

if (counterC < clockwise){
   return rotation + rotationspeed;
} else {
   return rotation - rotationspoeed;
}


One thing to note is that in this code your rotation will not be limited to [-180,180]. The code doesn't care because it uses moduli, but if other parts of the program do you're going to want to clean the rotation.
« Last Edit: December 01, 2012, 03:01:21 PM by Thaago »
Logged

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #2 on: December 01, 2012, 02:32:22 PM »

thanks! never thought of that...

I'm working on actionscript 3, by the way, which is similar enough to java that I can probably adapt this code to work, assuming it doesn't work already.

just to make sure I understand (trying to learn the code, please bear with me):

Code
float counterC = desired - rotation; //Sets counterC to the amount the turret needs to go counterclockwise to reach the target
counterC = (counterC%360 + 360)%360; // Makes sure counterC is between 360 and -360

float clockwise = 360 - counterC; // Sets clockwise to the opposite of counterC, so how much the turret needs to go clockwise


//this actually rotates the turret, very simple if + else statement
if (counterC > clockwise){
   return rotation + rotationspeed;
} else {
   return rotation - rotationspoeed;
}

nice, never saw the practical application of the modulus, thanks again

gunny
Logged
mmm.... tartiflette

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7233
  • Harpoon Affectionado
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #3 on: December 01, 2012, 02:55:36 PM »

No problem, I'm glad to try and help  :)

Your comments are almost what the code does - in the second line it actually makes sure that counterC is between 0 and 360. Just a % sign would restrict to between -360 and 360, but I actually wanted a stricter case so that later the only check needed was a simple >.

I forgot to put in a case in the if/else bit that doesn't rotate the turret if its close enough, but you already figured that out in your code so you should be fine :)
Logged

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7233
  • Harpoon Affectionado
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #4 on: December 01, 2012, 03:00:35 PM »

Whoops! I think I screwed up - the > sign should be a < sign! I changed my original post.
Logged

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #5 on: December 01, 2012, 03:05:18 PM »

are you sure that's the case?

you're looking for the LESSER of the 2, no?

therefore:

Code
if (counterC > clockwise){  //counterC is greater, therefore it's easier to rotate clockwise (greater distance counterclockwise)
   return rotation + rotationspeed;  //rotates clockwise
} else {
   return rotation - rotationspoeed;
}

is that not how it's supposed to be?
Logged
mmm.... tartiflette

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7233
  • Harpoon Affectionado
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #6 on: December 01, 2012, 03:10:31 PM »

Well now I'm confused :P. I thought adding to a rotation makes things move counter clockwise? I guess its just how Flash has their engine set up. Well, whichever way works! :D
Logged

PCCL

  • Admiral
  • *****
  • Posts: 2016
  • still gunnyfreak
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #7 on: December 01, 2012, 03:28:05 PM »

eh, pretty sure rotation ++ is clockwise...

will implement and test it out when I can

EDIT: Implemented and you were right, it was supposed to be the other around, thanks a lot!
« Last Edit: December 01, 2012, 04:00:08 PM by gunnyfreak »
Logged
mmm.... tartiflette

Thaago

  • Global Moderator
  • Admiral
  • *****
  • Posts: 7233
  • Harpoon Affectionado
    • View Profile
Re: [programming] rotate to mouse with limited speed - like sf turrets
« Reply #8 on: December 01, 2012, 04:59:28 PM »

No problem, glad I can help! :: feels good about self ::
Logged