Tobi PL
Tobi PL

Reputation: 63

Getting angle between two points in 2D game, problem when moving camera

I decided to ask question here because I have problem that I think I can't solve myself. I'm trying to make 2D Game that is RPG, You run around and do stuff.

For that i need to know which direciton player is looking, Player is ofc. looking in direction of mouse, i made function and it's... kinda working but not always, when Camera XY is set to 0,0 angle calculation is made properly. But as soon as i move camera away from 0,0 it's behavior become weird, i cam move Player and Camera separetly ( in case i need cutscenes )

Here is my current code that display 140pixels long line from player position to Mouse position

    qAngle = qGetAngle2( // Angle between 2 points ( Player Position + Camera )
                qVec2d{qMouse.x+Camera.x,qMouse.y+Camera.y}, // Mouse + Camera XY
                qVec2d{Shelly.x+Camera.x,Shelly.y+Camera.y});// Player + Camera XY
    Angle = qAngle; // only converting Int to StringInt for debug display
    
    ShellyWindow.qDraw_Line( // Drawing line
                Shelly.x+Camera.x, // Player X + Camera ( start point X )
                Shelly.y+Camera.y, // Player Y + Camera ( start point Y )
                (sin( 0.01744 * qAngle )*140)+(Camera.x+Shelly.x), // End point X
                (cos( 0.01744 * qAngle )*140)+(Camera.y+Shelly.y), // End point Y
                0x00FF00 ); // color

I have no idea what should i do... from my point of view logic is 100% fine

This code above works for as long as im not moving camera anywhere... This is how im moving things around, XS means "Speed", X/Y is just position

// moving player XY Speed
    if( qKey_a.Push )Shelly.xs -= 0.11;
    if( qKey_d.Push )Shelly.xs += 0.11;
    if( qKey_w.Push )Shelly.ys -= 0.11;
    if( qKey_s.Push )Shelly.ys += 0.11;

// Moving Camera XY position
    if( qKey_f.Push )Camera.x += 5;
    if( qKey_h.Push )Camera.x -= 5;
    if( qKey_t.Push )Camera.y += 5;
    if( qKey_g.Push )Camera.y -= 5;

I'm out of ideas what I can try to make it work, I was just trying feeding functions with different arguments.

//Edit1: I added two pictures here:

When Camera is in its 0,0 Coords When Camera is in its 0,0 Coords

When i move Camera When i move Camera

Upvotes: 1

Views: 174

Answers (1)

Friedrich
Friedrich

Reputation: 4817

The standard library provides atan2. It returns the shortest angle in radians from the positive x-axis to a point in carthesian coordinates.

As the point is relative to the origin, you need to transform your coordinate system. Sounds scary, but in non-rotated 2D coordinates, you only need to subtract your reference point.

The following code will give you the angle from Shelly to the mouse pointer:

constexpr auto r2d = 180. / M_PI;
qAngle = r2d * std::atan2(Shelly.y - qMouse.y, Shelly.x - qMouse.x);

Edit: I did provide a conversion from rad to degrees in my code snippet. However, using radians consistently is IMHO the better approach. Conversion should only take place before printing or logging an angle.

Upvotes: 2

Related Questions