# Line pointing at mouse (just to start)

• 08-30-2001
IwillRegisterSoon
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.

• 08-30-2001
Unregistered
Oh btw. I dont want any code. Just some ideas would be good.
• 08-30-2001
doubleanti
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! :p ]

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!
• 08-31-2001
IWillRegisterSoon
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.
• 08-31-2001
IWillRegisterSoon
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.
• 08-31-2001
Nick
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
• 09-01-2001
Unregistered
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.
• 09-01-2001
Unregistered
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;

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

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

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

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.
• 09-01-2001
MechanicX
better code
I have finally registered.:cool:

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.:eek: :eek: :eek: