Following the mouse...
Author |
Message |
ZeroPaladn
|
Posted: Thu Sep 14, 2006 12:33 pm Post subject: Following the mouse... |
|
|
I was wondering how to make lets say... a bullet go in the directoin of the mouse when fired? I'm at a loss on how to do it. I've heard of using trig and the x and y coords of the mouse and the player and use the pythagoream theorm, but im still at a loss.
PS. Its been a while everyone.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
Cervantes
![](http://compsci.ca/v3/uploads/user_avatars/1023105758475ab2e040bde.jpg)
|
Posted: Thu Sep 14, 2006 1:13 pm Post subject: (No subject) |
|
|
Yes, trig and Pythagorean theorem will be needed. What have you got so far?
Draw these things out on paper. It's a lot easier to work out the equations there than type them immediately into your code.
|
|
|
|
|
![](images/spacer.gif) |
MysticVegeta
![](http://www.geocities.com/ohsoinsane/my_avatar.JPG)
|
Posted: Thu Sep 14, 2006 1:24 pm Post subject: (No subject) |
|
|
Well you will need Pythagoreans for the shortest distance (hypotneuse) between the bullet and the mouse (x, y). And the trig to figure out what angle it should be shot at. It would be a right triangle so you can use your sin, cos and tan no prob
|
|
|
|
|
![](images/spacer.gif) |
ZeroPaladn
|
Posted: Fri Sep 15, 2006 11:42 am Post subject: (No subject) |
|
|
good idea cervantes, ill draw it out, and see what i come up with then. so let me get this straight, i use the pythagorean therom to find the shortest distance between my char and the mouse coords, then figure out the angle of that using trig, then fire off the bullet in said direction?
|
|
|
|
|
![](images/spacer.gif) |
do_pete
![](http://i38.photobucket.com/albums/e112/do_pete/1943.gif)
|
Posted: Fri Sep 15, 2006 12:23 pm Post subject: (No subject) |
|
|
MysticVegeta wrote: Well you will need Pythagoreans for the shortest distance (hypotneuse) between the bullet and the mouse (x, y).
Why do you need to find the shortest distance? Don't you just need the coordinates of the mouse and character to calculate the angle?
|
|
|
|
|
![](images/spacer.gif) |
BenLi
![](http://compsci.ca/v3/uploads/user_avatars/17704181124608ffcaeb421.gif)
|
|
|
|
![](images/spacer.gif) |
ZeroPaladn
|
Posted: Mon Sep 18, 2006 12:12 pm Post subject: (No subject) |
|
|
if only i understood the code... DAMN I HATE TRIG!
|
|
|
|
|
![](images/spacer.gif) |
Ultrahex
|
Posted: Mon Sep 18, 2006 3:40 pm Post subject: (No subject) |
|
|
There Is Also This Way Using Getting The Angle... Here is an example program I just wrote cause i was bored and am procrastinating other assignments
code: |
View.Set ("offscreenonly")
var mx, my, mb : int %Mouse Variables
var bulletx, bullety : real := 50.0 %Bullet Location
var velx, vely : real := 0 %Bullet Velocities (Split into x and y)
var pointx, pointy : int := 50 %Point Shooting From
var tempx, tempy : int %Used to Get Point End of Line Drawn from Shooting Point
% Function Written By Ultrahex to Determine Angle in Degrees (Also Includes Exceptions)
% This Code is NOT efficiently Written !!!
function getAngle (x1, y1, x2, y2 : int) : real
if ((x2 - x1) ~= 0) then
if ((x2 - x1) <= 0) then
result arctand ((y2 - y1) / (x2 - x1)) + 180
else
if (arctand ((y2 - y1) / (x2 - x1)) < 0) then
result arctand ((y2 - y1) / (x2 - x1)) + 360
else
result arctand ((y2 - y1) / (x2 - x1))
end if
end if
else
if ((y2 - y1) <= 0) then
result 270
end if
if ((y2 - y1) >= 0) then
result 90
end if
end if
end getAngle
loop
cls %Clear Screen
Mouse.Where (mx, my, mb) %Set Mouse Point Variables
/* Calculations for Line */
tempx := round (cosd (getAngle (pointx, pointy, mx, my)) * 100)
tempy := round (sind (getAngle (pointx, pointy, mx, my)) * 100)
/* Bullet Movement Calculations */
bulletx += velx %Move Bullet By Current Velocity Along X-Axis
bullety += vely %Move Bullet By Current Velocity Along Y-Axis
/* OUTPUTS */
put "Angle Between Mouse and Point Is: ", getAngle (pointx, pointy, mx, my) %Display The Angle Between Right and Mouse Location
put "D:[", mx - pointx, ",", my - pointy, "]" %Display Mouse Point Compared to Shooting Point
/* Drawing */
drawfilloval (pointx, pointy, 3, 3, black) %Shooting Point
drawfilloval (round (bulletx), round (bullety), 3, 3, purple) %Bullet
drawline (pointx, pointy, pointx + tempx, pointy + tempy, red) %Line From Shooting Point
if (mb = 1) then %If Mouse Clicked...
%Reset Bullet Location to Point Of Shooter
bulletx := pointx
bullety := pointy
%Get The Velocity dependant on Angle (Much Like Slope)
velx := cosd (getAngle (pointx, pointy, mx, my)) * 5
vely := sind (getAngle (pointx, pointy, mx, my)) * 5
end if
View.Update %Update Screen
delay (20) %Delay
end loop
|
As Long as You Understand "SOH, CAH, TOA" you should understand how i am getting to this (Remeber Sin(theta) = y/x and all that. Remeber Don't just take the code, it provides a guideline even though it probably is more then that for some and i bet i will see my code in a turing program soon but that is how life works i suppose. Hope this clears a few things up? possibly :/ Ask Questions if You Would Like.
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Mon Sep 18, 2006 9:39 pm Post subject: (No subject) |
|
|
all you need is similar triangles. take a look at the attached image... there are two similar triangles. with similar triangles you can do something like height_1 / base_1 = height_2 / base_2 and solve for an unknown quantity if you have the other three. in our case, we have the hypoteneus of the small triangle (which represents the velocity vector), the base, hieght and hypoteneus of the big triangle (ie where the mouse is relative to the charater). we want the find the components of the velocity vector (dx and dy) which are represented by the base and height of the little triangle. so we would use the formulas dx / speed = (mx - x) / hypoteneus and dy / speed = (my - y) / hypoteneus. rearange to isolate dx and dy and solve.
heres a sample code:
code: | setscreen ("offscreenonly")
const x := maxx div 2
const y := maxy div 2
const s := 100
var mx, my, md : int
var d, dx, dy : real
loop
mousewhere (mx, my, md)
d := sqrt ((mx - x) ** 2 + (my - y) ** 2)
dx := s * (mx - x) / d
dy := s * (my - y) / d
drawline (x, y, round (x + dx), round (y + dy), black)
drawoval (x, y, s, s, grey)
View.Update
cls
end loop |
|
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Mon Sep 18, 2006 10:02 pm Post subject: (No subject) |
|
|
sorry, heres the picture:
Description: |
|
Filesize: |
17.46 KB |
Viewed: |
1961 Time(s) |
![similar_triangle.JPG similar_triangle.JPG](uploads/attachments/similar_triangle.jpg)
|
|
|
|
|
|
![](images/spacer.gif) |
ZeroPaladn
|
Posted: Thu Sep 21, 2006 12:09 pm Post subject: (No subject) |
|
|
Thanks to you both, Zylum and Ultrahex. I understand it now! I liked Zylum's more simplistic method, although you can glitch out Zylum's program (just hover the mouse of the exact centre of the circle, Ultrahex's just points it straight down). Bits to you both!
+25 Bits to Zylum and Ultrahex
Thanks again guys!
|
|
|
|
|
![](images/spacer.gif) |
Clayton
![](http://compsci.ca/v3/uploads/user_avatars/1718239683472e5c8d7e617.jpg)
|
Posted: Thu Sep 21, 2006 2:50 pm Post subject: (No subject) |
|
|
My oh my, zylum, people seem to like giving bits to you dont they, maybe it has something to do with that even 1000 bits sitting there that they dont like?
NOTE: zylum is a MOD, his bits are SET
|
|
|
|
|
![](images/spacer.gif) |
zylum
![](http://compsci.ca/v3/uploads/user_avatars/1689129126468091c334ee0.gif)
|
Posted: Thu Sep 21, 2006 4:54 pm Post subject: (No subject) |
|
|
maybe they give me the bits in a gesture of appreciation. and since you're so fond the rules, maybe you should follow them and stop spamming. [Rules]
anyhoo, to fix the division by zero problem, i usually add a very small value to the denominator. technically, if the denominator is equal to the negative of the value i add, it will also be division by zero but that is far less likely than it would otherwise be..
code: | setscreen ("offscreenonly")
const x := maxx div 2
const y := maxy div 2
const s := 100
var mx, my, md : int
var d, dx, dy : real
loop
mousewhere (mx, my, md)
d := sqrt ((mx - x) ** 2 + (my - y) ** 2) + 1e-4
dx := s * (mx - x) / d
dy := s * (my - y) / d
drawline (x, y, round (x + dx), round (y + dy), black)
drawoval (x, y, s, s, grey)
View.Update
delay (50)
cls
end loop |
|
|
|
|
|
![](images/spacer.gif) |
TheOneTrueGod
![](http://www.drmcninja.com/images/mcninjab3.jpg)
|
Posted: Thu Sep 21, 2006 5:26 pm Post subject: (No subject) |
|
|
why don't you just do an "if" check? it'll eliminate any "stupid" things from happening, like the -1e4 value cropping up.. (Though since you used sqrt, this isn't possible in this particular instance, but I have seen a few times where the program you provided COULD result in a division by zero error...)
|
|
|
|
|
![](images/spacer.gif) |
|
|