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

Author Topic: Moving from start position to mouse position at varying angles  (Read 2802 times)

0 Members and 1 Guest are viewing this topic.

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Hi,
I'm trying to add a gun that fires in the direction of the mouse from a centre point, this code only fires accurately when my mouse is incredibly far away (but without changing the angle) and I want it to be accurate at all distances. I've been stuck on this for ages, I know my maths is probably wrong but I just don't know why it works at some distances.
Thank you, sorry if this is a repeat question

//where it starts
        int aky = akSprite.getPosition().y;
        int akx = akSprite.getPosition().x;
       
        //checks for mouse position
        int mousey1 = this->mouse.getPosition().y;
        int mousex1 = this->mouse.getPosition().x;

        //how many large or small the movement is
        const int distance = 30;

        std::cout << "\n The mouse position used is x: " << mousex1 << " y: " << mousey1 << "\n The launch position is x: " << akx << " y: " << aky;




       
       
       
        //finds the height and width of that position and lowers it to movable values
         travely = (mousey1 - aky) / distance;
         travelx = (mousex1 - akx) / distance;
         
         //global variable for bullet&#39;s position
        totalx = akx;
        totaly = aky;

       

        //the bullets position updates constantly elsewhere while this is only run when the fire key is pressed
       
        std::cout << "\n The current MOVEMENTS ARE x: " << travelx << " y: " << travely;

sandwich hoop

  • Newbie
  • *
  • Posts: 11
    • View Profile
    • Email
Re: Moving from start position to mouse position at varying angles
« Reply #1 on: July 23, 2024, 09:40:46 pm »
I wasnt using local coordinates, I am so dumb I've spent weeks on this...

Me-Myself-And-I

  • Jr. Member
  • **
  • Posts: 93
    • View Profile
Re: Moving from start position to mouse position at varying angles
« Reply #2 on: July 24, 2024, 05:12:39 am »
Haha! No worries. These things happen. :P

 

anything