-1

I've got a problem and I haven't yet found an explained solution to get it drilled into my head:

I want my object to make a basic rotation TOWARDS the mouse cursor, not the lock-on. I know it must be embarrassingly easy since there's not many threads documenting this, yet I'm loosing my mind when seeing (wantedAngle-object.getRotation()) reaching from -180 to 180 degrees.

I've tried adding/subtracting 180 or 360 after calculating wantedAngle and reverse it before calculating the desired rotation direction with if-statements like

if(wantedAngle<-0){//turn left applying turnSpeed etc}

as an example

This is the code that I'm stuck with and build tests upon:

void myObject::stareAtMouse(sf::Vector2i mouseLoc)
{
    float dx = rect.getPosition().x - mouseLoc.x;
    float dy = rect.getPosition().y - mouseLoc.y;
    float wantedAngle = atan2(dy,dx) * 180 / M_PI;
    cout<<wantedAngle-rect.getRotation()<<endl; //Prints -180 to 180
}

Please tell me how to approach this problem cus I'm losing my mind over this! Are there possibly multiple ways to solve/convert this?

Thanks in advance and sorry for the noobish question

EDIT: As I thought, I was so overly focused on it and made it more complicated than it was. I've created an offset before but mistook it for degrees, so obviously it didn't make any sense.

I've now incremented the wantedAngle by 180 after the atan2-function to even the playing-field for offset and getRotation(), then get the offset from wantedAngle-object.getRotation() and then use if/else if-statements to see if offset is less than 0 or greater than 0 to make it rotate respectively.

Thanks for your answers so far, they helped a lot!

--> Only thing to fix now is the offset snapping back into negative once the the mouse is on the object's right. The object's rotation doesn't seem to affect this issue at all.

EDIT_2: Got everything working now and I'll post my code here: void Monster::stareMouse(sf::Vector2i mouseLoc)

{
    float dx = monsterShape.getPosition().x - mouseLoc.x;
    float dy = monsterShape.getPosition().y - mouseLoc.y;
    float currentAngle = monsterShape.getRotation() / 180 * M_PI;
    float wantedAngle = (atan2(dy,dx) * 180 / M_PI)+180;
    float monsterAngle = monsterShape.getRotation();
    float offset = wantedAngle - monsterAngle;
    if(offset<-180)offset+=360;
        else if(offset>180)offset-=360;
    if(offset<0)monsterShape.rotate(-turnSpeed);
        else if(offset>0)monsterShape.rotate(turnSpeed);
    sf::Vector2f mover;
    if(abs(offset)>30)
    {
        mover.x = cos(currentAngle)*(speed/3);
        mover.y = sin(currentAngle)*(speed/3);
    }
    else
    {
        mover.x = cos(currentAngle)*speed;
        mover.y = sin(currentAngle)*speed;
    }
    cout<<offset<<endl;
    monsterShape.move(mover);
}

Not very well formatted yet, but I got it working perfectly.

if(offset<-180)offset+=360;
  else if(offset>180)offset-=360; //Basically going all the way around and stopping
if(offset<0)monsterShape.rotate(-turnSpeed);
  else if(offset>0)monsterShape.rotate(turnSpeed); //Rotate in the direction of need

The result is a moving object that moves normally while the target is inside of a cone (30 deg here) in front of the object and once it's out of 'vision', it will slow down to refocus the target which looks quite realistic. https://youtu.be/zNZmAgDHF8k just to show you how it looks like now :)

  • It sounds like you've got an object that has a current angle. You also have a target angle (which is the angle which points directly at the mouse cursor). You want to adjust the current angle towards the target angle by some amount; you don't want to simply set the current angle to the target angle. Is all of that correct? – Tanner Swett Aug 26 '16 at 5:24
  • Did you draw any schematics of what you want? Geometry problems are best solved by drawing them. – Euphoric Aug 26 '16 at 5:46
  • @Tanner That is correct. I kept it minimal to isolate the problem. – Farrrbi Aug 26 '16 at 9:16
  • @Euphoric I've tried drawing but once I start rotating the object, the rotation changes below or up to -400 and 400 degrees respectively. I'm having a horribly hard time visualizing this, especially as I've tried using while- and if-statements to add/subtract -180/180 degrees to keep the resulting number in a specific range (e.g. range of -180 to 180 or 0 to 360) – Farrrbi Aug 26 '16 at 9:29
  • @Farrrbi I mean drawing on paper with pencil. – Euphoric Aug 26 '16 at 9:30
0

I think the logic you want is:

float angleDifference = wantedAngle-rect.getRotation();
while (angleDifference > 180) angleDifference -= 360;
while (angleDifference < -180) angleDifference += 360;

Next, clamp angleDifference to a certain range so that you're not rotating by a greater angle than you want to. And finally, rotate your object by angleDifference.

  • This helped a lot, thanks man! Made me reconsider my failed offset solution! – Farrrbi Aug 26 '16 at 22:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.