Line pointing at mouse (just to start)

This is a discussion on Line pointing at mouse (just to start) within the Game Programming forums, part of the General Programming Boards category; Hi Guys. I am trying to make a line that always points at where the mouse is. Kind of like ...

  1. #1
    IwillRegisterSoon
    Guest

    Question Line pointing at mouse (just to start)

    Hi Guys.

    I am trying to make a line that always points at where the mouse is. Kind of like a turret following a target.

    The line has a fixed length (so I cant use mouse coords as the line endpoints) so that its motion would be along a circular path.

    I just need something to start with.

    Thanks in advance.

  2. #2
    Unregistered
    Guest
    Oh btw. I dont want any code. Just some ideas would be good.

  3. #3
    Linguistic Engineer... doubleanti's Avatar
    Join Date
    Aug 2001
    Location
    CA
    Posts
    2,459
    no code eh? good! i love speaking about programming in generalizations... [everyone just try and find ONE place where i've done the code tags, or even any code at all!!! can't find it!!! ha! ]

    let's assume the point you want to have the circle sweeping is called point 'P'... and the mouse pointer is point 'M'... and the point which will give you the correct line 'Z'... the line being PZ

    get the slope of PM (ahh!, no math symbols!!!)... and calculate the x and y distances from P to Z using sin and cosine (and the slope)... then draw P to Z and you got it!!!

    i hope that wasn't too confusing! hth!
    hasafraggin shizigishin oppashigger...

  4. #4
    IWillRegisterSoon
    Guest

    Lightbulb Got it

    Thanks for the tip. After extensive trial and error I got it to work nicely.

    After reading your topic, I got the basic idea. What was really the problem was the screen coordinates themselves. Since the origin of the screen is the left-top corner, and my line started in the middle of screen (400,300) for a 800x600 depth, I had to do a lot of trial and error to get it working properly.

    I know I would have spend a lot less time if I had thought it through before coding. Oh well.


    Thanks again.

  5. #5
    IWillRegisterSoon
    Guest

    Question trig stuff

    I am using atan to get the angle in radians and then pluggin in to the sin and cos to get the x and y coords.

    Is there a better way to do this without using any of the inverse trig sutff?

    I could make a table and look it up but since I am using floats and the table is accessible (array) by integersits, not working properly.

  6. #6
    Blank
    Join Date
    Aug 2001
    Posts
    1,034
    Yes you can do it with out using trig functions but I'm not sure it would
    be faster.

    Let r be the circle's radius
    Let O be the origin
    Let P be the point were trying to find.
    Let M be the mouse's point
    Let A be (M.x, O.y)
    Let B be (P.x, O.y)
    Let L be the length of O to M

    Then you can construct two similar triangles:
    T_1 = (O, M, A)
    T_2 = (O, P, B) and by the similar ratio (specifically L/r) you can solve for
    P.x and P.y

  7. #7
    Unregistered
    Guest

    Lightbulb

    Ah another basic math approach. I will try your method, and I do understand it. Its pretty simple. Only problem would be the screen coords conflicting with the line coords, again.

    I think it might be faster. Thanks.

  8. #8
    Unregistered
    Guest

    Cool got it

    Ok I tried the second method. I like it.

    Its really clean. Only slowdown would be the sqrt function. The reason I like this method is that there are no trig stuff, so no look up tables.

    Ofcourse I had to again accomadate for the screen coords issue. Is there a way to permanently set the screen coords to however you want for each object type? Its friggin annoying to do it everytime.



    Here is the code. I didnt post the other method caz its too dirty and inefficient. If you can find me a better solution than using sqrt, I'd be happy to look in to it.

    POINT ori, end, mos;

    float radi, length, ratio;

    mos.x = mousex;
    mos.y = mousey;

    ori.x = 0;
    ori.y = 0;

    radi = 100;

    length = sqrt(mos.x*mos.x + mos.y*mos.y);

    ratio = radi/length;


    end.x = mos.x*ratio + ori.x;
    end.y = mos.y*ratio + ori.y;

    (insert favourite drawline function in here)
    DrawLine(ori.x,ori.y,end.x,end.y)


    This code is when the line starts at the origin. Thanks again Nick.

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    39

    Thumbs up better code

    I have finally registered.

    Also I found a better code than the previous which deals with putting the line anywhere on screen and it would work fine. No more screen coords stuff.

    I also used my paticle engine using linked lists to fire a bullet travelling in the direction of the mouse and explode whereever the mouse was.

    Next thing is get a moving target and destroy it.
    Its all in your mind...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer and Polymorphism help.
    By Skyy in forum C++ Programming
    Replies: 29
    Last Post: 12-18-2008, 08:17 PM
  2. need Help in Mouse Pointer
    By obaid in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2006, 02:33 AM
  3. Move the Caret to a line
    By TheDan in forum Windows Programming
    Replies: 3
    Last Post: 08-07-2005, 12:59 PM
  4. Read only one line using seekg
    By RedZippo in forum C++ Programming
    Replies: 3
    Last Post: 03-31-2004, 10:10 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21