Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 need help on hot to get a line to point to the mouse
Index -> Programming, Turing -> Turing Help
Goto page Previous  1, 2
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Degensquared




PostPosted: Sun May 04, 2008 1:54 pm   Post subject: Re: need help on hot to get a line to point to the mouse

code:

setscreen("offscreenonly")
const LENGTH := 125
var mouse_x, mouse_y, btn, point_x, point_y : int
var angle : real
point_x := 0
point_y := 0
loop
Mouse.Where(mouse_x,mouse_y,btn)
angle := arccosd(abs(mouse_x-point_x)/ Math.Distance(mouse_x,mouse_y,point_x,point_y))
drawline(point_x,point_y,point_x+round(cosd(angle)*LENGTH), point_y+round(sind(angle)*LENGTH), 7)
View.Update
cls
end loop


Essentially what he is doing, is using trigonometry to first find the angle that the line extends from the point at, then to use that angle to draw the line.

arccosd is the trigonometric function in the "angle :=" line. Arccos is pretty much the exact opposite of the cosine function. The program passes in the absolute value (essentially: this makes it always positive) of the distance between the two points on the x axis (by subtracting one from the other). Then the program then divides this number by the total distance in between the two points (distance = sqrt(<distance on x axis>**2 + <distance on y axis>**2)), to get the angle inbetween the two points.

With the angle, we can draw a line extending out at that angle for as far as we want. When given an angle, cosine and sine will tell us the ratios of how far along the point will be on the x and y axises respectively. So, for the x point, we pass in the cosine of the angle (which will return a number between 0 and 1), and multiply it by the total length that we want the line to be to get the x coordinate of the final point. If we do the same on the y axis with the sine function this time, we will get the y coordinate of the final point.

If you are still confused, try searching google for trigonometry tutorials.

Degen.
Sponsor
Sponsor
Sponsor
sponsor
josh_65665




PostPosted: Sun May 04, 2008 6:44 pm   Post subject: good

so now the line will point to the mouse but only if the mouse is to the right of the starting point of the line so how would i go about getting it to point to the mouse if the line starting point is 250,0 and not 0,0 and when i figure that out i need to get the line to shoot bullets out in the direction the line is pointing when the mouse clicks and that will probaly be even harder perhaps someone could show me how to do it
Saad




PostPosted: Sun May 04, 2008 7:19 pm   Post subject: Re: need help on hot to get a line to point to the mouse

I think I can provide a better alternative then to use Trig functions, the idea has already been suggested by gitoxa. The idea is similar triangles. (I shall be using gitoxa's image to refer to the symbols)

The idea is the following.
- Triangle ABC and DEF are similar. Therefore
code:

 A     B     C
--- = --- = ---
 D     E     F


Now lets re-arrange to solve for D in terms of A, C, and F.
A / D = C / F
A = D * (C / F)
D = A * (F / C)
Now lets see what D, A, F and C represent.
C represent the distance between the coordinate of the player and the current mouse coordinate.
A is the delta X (mouse x - player x)
F is the distance we want.
Therefore we can create an equation,
code:

newDeltaX = deltaX * (PROJECTED_LENGTH / ActualLength)
newDeltaY = deltaY * (PROJECTED_LENGTH / ActualLength)

pointX = playerX + newDeltaX
pointY = playerY + newDeltaY
gitoxa




PostPosted: Sun May 04, 2008 8:15 pm   Post subject: RE:need help on hot to get a line to point to the mouse

I got ban voted for that post too. Was it that awesome?
andrew.




PostPosted: Mon May 05, 2008 4:38 pm   Post subject: RE:need help on hot to get a line to point to the mouse

It's basic trigonometry. If you're in grade 10, you'll be learning it later on in the year. You basically use either sine, cosine, or tangent to calculate the angle of any side of the "triangle" (it's imaginary in our case).

Here's a quick run-down on trig.

Remember SOH CAH TOA. This is the key to trig.

Let's call the angle we want to find out ϑ (the Greek letter theta).

SOH = sineϑ = Opposite/Hypotenuse
CAH = cosineϑ = Adjacent/Hypotenuse
TOA = tangentϑ = Opposite/Adjacent

All you have to do is rearrage the equation so that ϑ is by itself and you have your angle. Also, to move a sine, cosine, or tangent to the other side, you must use arc sine, arc cosine, or arc tangent although nowadays it's called sine^-1, etc. on most calculators.

I hope this quick trig lesson helps. Smile
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 2 of 2  [ 20 Posts ]
Goto page Previous  1, 2
Jump to:   


Style:  
Search: