Thread: My new project

  1. #1
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Cool My new project

    Hi, my new project is a turret that locks on the target...
    The target is in a 2D space and has 3 coordinates, x and y...
    How to calculate the angle of which turret has to move?

    Is it something like this:

    Code:
    ...
    angle = 90;
    scanf("%d", &x);
    scanf("%d", &y);
    for(i=0;i<x;i++){
             angle = angle - (angle/2);
             }
    for(i=0;i<y;i++){
             angle = angle + (angle/2);
             }
    ...
    I know this isn't the whole thing because what would i do if angle was 0 at the start?
    Arduino rocks!

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This page will help sort it out:
    Mathwords: Equation of a Line

    slope = (y1-y2)/(x1-x2)

    When you know the location of two points along the (imaginary) line (like you and the targets y and x points).

    A line can have 0 slope, that's no problem. Vertical lines are no good for slope of a line though, since you can't divide by zero. Their slope isn't zero, it's just "no slope".

    Once you find the slope of the line, from you to your target, I believe you're set. If the target is moving, and you have to "lead" it a bit, in order to hit the target, then the math increases.
    Last edited by Adak; 09-30-2009 at 06:16 AM.

  3. #3
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    so...ma position is 0, 0, and targets 2, 1...so

    slope = (0-0)/(2-1)... the result is 0!?
    Arduino rocks!

  4. #4
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Unhappy

    This is my code...it doesn't work, i think i need a few "ifs" to see if x<y, x>y and stuff...
    Code:
    #include <stdio.h>
    
    struct pos_s{
        int x;
        int y;
        };
    
    float angle;
    struct pos_s pos;
    int i;
    int main(){
        angle = 45;
        scanf("%d", &pos.x);
        scanf("%d", &pos.y);
        printf("processes:\n");
        for(i=0;i<pos.x;i++){
            angle = angle - (angle/2);
            printf("%f\n", angle);
            }
        for(i=0;i<pos.y;i++){
            angle = angle + (angle/2);
            printf("%f\n", angle);
            }
    
        printf("final angle:");
        printf("%f", angle);
        return 0;
        }
    Arduino rocks!

  5. #5
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by yann View Post
    so...ma position is 0, 0, and targets 2, 1...so

    slope = (0-0)/(2-1)... the result is 0!?
    no... your first position is (x1, y1) = (0, 0)
    and your 2nd position is (x2, y2) = (2, 1)

    so (y1-y2)/(x1-x2) = (0 - 1)/(0 - 2) = 0.5

    This gives you slope...which is change in y over change in x. If you want the angle, you'll take the arctangent of 0.5 (atan() in C) to get the angle in radians...and then multiply by 180/pi to convert to degrees.

    atan(0.5) would give pi/4...times 180/pi gives 45 degrees.

  6. #6
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Thank you, sounds very good, but what is "pi"?

    I did this in my code, it calculates degrees OK but only if starting position is 40 degrees and if number is negative( - ).

    Code:
    #include <stdio.h>
    struct pos_s{
        int x;
        int y;
        };
    
    float angle;
    struct pos_s pos;
    int i;
    int main(){
        angle = 40;
        scanf("%d", &pos.x);
        scanf("%d", &pos.y);
        printf("processes:\n");
        if(pos.x > pos.y){
            angle = angle + ((pos.x - pos.y) * 5);
           }
        else{
            angle = angle - ((pos.x - pos.y) * 5);
           }
        printf("final angle:");
        printf("%f", angle);
        return 0;
        }

    Is pi = 3,14159265?
    Arduino rocks!

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    63
    Pi is a mathematical constant. 3.14159.....

    Just follow what Epy said, and you shouldn't have any trouble getting the angle to your target.

    EDIT: Check this page out for more on PI:

    http://en.wikipedia.org/wiki/Pi

  8. #8
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Pi is a mathematical constant. 3.14159.....

    Just follow what Epy said, and you shouldn't have any trouble getting the angle to your target.

    EDIT: Check this page out for more on PI:

    Pi - Wikipedia, the free encyclopedia
    Oh well, my dad is an phi and pi freak, always talks about it...i know what pi is, just didn't knew it was what Epy said...but thanks!
    Arduino rocks!

  9. #9
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    And what if (y1-y2)/(x1-x2) = (0 - 1)/(0 + 2) = -0.5, that will be negative degrees...or?
    Arduino rocks!

  10. #10
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186

    Cool

    OK... I did it, it works well, but only if both coordinates are positive... How can i fix this?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define pi 3.14159265
    
    struct pos_s{
        int x;
        int y;
        };
    
    float angle;
    struct pos_s pos;
    int i;
    int main(){
        angle = 40;
        scanf("%d", &pos.x);
        scanf("%d", &pos.y);
        printf("processes:\n");
        angle = ((atan((0 - pos.y) / (0 - pos.x)))*(180/pi));
        printf("final angle:");
        printf("%f", angle);
        return 0;
        }
    The piece of code that is red is the angle calculation...
    Arduino rocks!

  11. #11
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    you should declare your variables inside of int main()...

    your program doesn't really do anything but calculate the arctangent for the coordinates collected...you set angle to 40, but it doesn't matter because it's never used, it's just set to a different value

    you don't have to separate printf statements so much...
    printf("processes:\nfinalangle:%f, angle);

  12. #12
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    OK, i fixed these things...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    #define pi 3.14159265
    
    struct pos_s{
        int x;
        int y;
        };
    int main(){
        float angle;
        struct pos_s pos;
        int i;
        scanf("%d", &pos.x);
        scanf("%d", &pos.y);
        printf("processes:\n");
        angle = ((atan((0 - pos.y) / (0 - pos.x)))*(180/pi));
        printf("final angle:%f", angle);
        return 0;
        }
    Arduino rocks!

  13. #13
    Registered User yann's Avatar
    Join Date
    Sep 2009
    Location
    Zagreb, Croatia
    Posts
    186
    Whoops... It still can't do it if x is smaller than y, and if x or y is negative ...
    How to fix this?
    Arduino rocks!

  14. #14
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Try atan2() - it takes two arguments so you don't do the division yourself... saves all that hassle about possibly dividing by zero. Also when one or both arguments are negative, atan2 gives the result in the correct quadrant.

    See http://en.wikipedia.org/wiki/Atan2

  15. #15
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    assume x1, y1 are your coordinates, and x2,y2 are the coordinates of the target

    atan((y2-y1)/(x2/x1)) gives you the angle, the signs of x2-x1 and y2-y1 give you the quadrant.

    as nonoob noted, atan2 may be better, unless you check for a zero in x2-x1, in which case the angle is 90 or 270
    Last edited by abachler; 09-30-2009 at 09:39 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem Displaying a Struct
    By rockstarpirate in forum C++ Programming
    Replies: 16
    Last Post: 05-05-2008, 09:05 AM
  2. added start menu crashes game
    By avgprogamerjoe in forum Game Programming
    Replies: 6
    Last Post: 08-29-2007, 01:30 PM
  3. Dynamic Binding
    By gpr1me in forum C++ Programming
    Replies: 1
    Last Post: 03-24-2006, 09:01 AM
  4. Game Independent Anti-cheat Project Needs Programmers
    By GIA Project Lea in forum Projects and Job Recruitment
    Replies: 3
    Last Post: 09-15-2005, 07:41 PM