Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Sprite to face mouse position  (Read 1899 times)

0 Members and 1 Guest are viewing this topic.

Jeffrey

  • Newbie
  • *
  • Posts: 36
    • View Profile
Sprite to face mouse position
« on: July 28, 2012, 11:34:27 am »
I know, I know, on the internet there are a lot of tutorial on how to get the angle between the center of two objects.
Now I have this code:

//  (180 / PI = 57.3065)
float AngleBetweenVectors(sf::Vector2f, sf::Vector2f);
float AngleBetweenVectors(sf::Vector2f a, sf::Vector2f b){
   return 57.3065f * atan2(b.y - a.y, b.x - a.x);
}
 

that get the angle, and then this code that I use to calculate it:

    float angle = AngleBetweenVectors(player_p, cursor.getPosition());
    player.setRotation(angle);
 

where player_p is the player center position (a 50x50 square sprite) and cursor is the cursor sprite I use.
player is finally the player sprite.

But when I run it it gets this kind of problems (please watch the entire video):
http://www.youtube.com/watch?v=qaS151xUPeg&feature=youtu.be

As you can see on the border of the terrain it gets a little bit tricky. Why is that? I'm using two views like that:

    window.clear(sf::Color(0, 0, 0, 255));
    window.setView(camera); // camera view
    // start drawing
        terrain.draw(); // terrain blocks
        window.draw(player); // player sprite
        window.setView(cursor_camera); // cursor view
            window.draw(cursor); // cursor sprite (the circular one)
    // end drawing logic
    window.display();
    window.setMouseCursorVisible(false); // no mouse pointer please
 

StormWingDelta

  • Sr. Member
  • ****
  • Posts: 365
    • View Profile
Re: Sprite to face mouse position
« Reply #1 on: July 28, 2012, 07:55:20 pm »
Yes this code is in Java but the equations shouldn't be that hard to change up to work for you.
All you have to do is get the Position of the 2 objects you want and after that it's easy.
//These 3 methods should help. Also as you can tell I made my own point class which was unneeded.
    public double TargetAngle(Sprite Tar)
    {
        //
        double XD = CJRPoint2D.DistanceX(this.center().X(),Tar.center().X()); //.center().X() // getXPos()
        double YD = CJRPoint2D.DistanceY(this.center().Y(),Tar.center().Y());
       
        return TargetAngle(XD, YD);
    }
    public double TargetAngle(double TarX, double TarY)
    {
        //
        double PA = Math.toDegrees(Math.atan2(TarY, TarX));
        double TAngle = PA;
        if(TAngle < 0)
        {
            TAngle += 360;
        }
        else if(TAngle > 360)
        {
            TAngle -= 360;
        }

        return TAngle;
    }
    public double TargetAngle(CJRPoint2D Tar)
    {
        //
        double XD = CJRPoint2D.DistanceX(this.center().X(),Tar.X());
        double YD = CJRPoint2D.DistanceY(this.center().Y(),Tar.Y());
       
        return TargetAngle(XD, YD);
    }
 

Here's the point class or at least the useful equations from it anyways.
    public static double Distance(double X1, double Y1, double X2, double Y2)
    {
        double D = Math.sqrt( Math.abs( (X2 - X1) * (X2 - X1) + (Y2 - Y1) * (Y2 - Y1) ) );
        return D;
    }
   
    public double Distance( CJRPoint2D B)
    {
        double D = Math.sqrt( Math.abs( (B.X() - this.X()) * (B.X() - this.X()) + (B.Y() - this.Y()) * (B.Y() - this.Y()) ) );
        return D;
    }
   
    public static double DistanceX(double X1, double X2){return (X2 - X1);}
    public static double DistanceY(double Y1, double Y2){return (Y2 - Y1);}
   
 

This should help you a bit but remember that there are a few things you have to do before you can turn these into C or C++ functions and it has to do with pointers and references.

Also forgot to mention these are meant to be used in classes.
I have many ideas but need the help of others to find way to make use of them.

 

anything